/**
 * Shared JS logic.
 */

var Toggler = {
    /**
	* Hide show element
	* 
	* @param check
	* @param display
	*/
    displayCheckedField : function(check, display) {
        if ($(check).attr('checked')) {
            $(display).show();
        } else {
            $(display).hide();
        }
    },

    /**
	 * Generic toggle displaying/hiding specific element when some toggle
	 * specific is checked
	 */
    toggleSpecific : function(selector, hide) {
        Toggler.displayCheckedField(selector, hide);
        $(selector).click(function() {
            Toggler.displayCheckedField(selector, hide);
        });
    },
    specialToggle : function() {
        var element = '.toggle-special';
        $(element).each(function(index, value) {
            var toHide = "." + $(this).attr('id').split('-').pop();
            Toggler.toggleSpecific(value, toHide);
        });
    },

    bindTogler : function() {
        Toggler.specialToggle();
        Toggler.toggleSpecific('.toggle-specific', '.to-toggle');
    },
	
	navToggler: function() {
		$("#show-more").click(function() {
			$(".hide").fadeToggle("fast", function() {
				if ($(this).is(":visible")) {
					$(".hide").css("display", "block");
					$("#show-more").html("&laquo;");
				} else {
					$("#show-more").html("&raquo;");
				}
			});
		})
	}
};

/**
 * Hide - show next row in table
 */
function showNextRow(){
    $(".show-next-row").click(function() {
        if($(this).text() == "Hide"){
            $(this).closest("tr").next().hide();
            $(this).text("Display") ;
        }else{
            $(this).closest("tr").next().show();
            $(this).text("Hide");
        }
    });
}
/**
 * Generic toggle displaying/hiding the following element.
 */
function toggleNext() {
    $(".toggler").click(function() {
        $(this).next().slideToggle("slow");
    });
}

/**
 * Toggles filters form display with animation.
 */
function filtersFormDisplay() {
    var formFiltersId = 'filters';
    var formFiltersAnchor = '#' + formFiltersId;
    var formTogglerId = 'filter-toggle';
    var formTogglerAnchor = '#' + formTogglerId;
    var toggleSpeed = 'slow';
    var formTogglerAnchorClass = 'action filter';
	
    if ($(formFiltersAnchor) == null) {
        return;
    }
	
    if ($(formFiltersAnchor).attr("class") == 'noToggle') {
        return;
    }
	
    $('<p><a id="' + formTogglerId + '" class="' + formTogglerAnchorClass + '" href="">Toggle Advanced Filter</a></p>').insertBefore(formFiltersAnchor);
    $(formFiltersAnchor).css('display', 'none');
    $(formTogglerAnchor).toggle(
        function () {
            $(formFiltersAnchor).slideToggle(toggleSpeed);
        },
        function () {
            $(formFiltersAnchor).slideToggle(toggleSpeed);
        }
    );
}

/**
 * Hook up "check all" checkboxes.
 */
function hookCheckboxes() {
    $('.checkall input').click(function() {
        $(this).parent().next().find(':checkbox').attr('checked', this.checked);
    });
}


function selectAllTable(checkbox, target) {

    var targetId = target;
    var targetAnchor = '#' + targetId;
    var checkboxId = checkbox;
    var checkboxAnchor = '#' + checkboxId;
	
    $(checkboxAnchor).click(function() {
        $(targetAnchor+" tr :checkbox").attr('checked', this.checked);
    });
	
	
}

function givc_web_search() {
    var UserGivcSearchAddress = $('#UserGivcSearchAddress').val();
    if (UserGivcSearchAddress == 'agency') $('#UserGivcSearchWebsite').parent().hide('fast');
    if (UserGivcSearchAddress == 'address') $('#UserGivcSearchWebsite').parent().hide('fast');
    if (UserGivcSearchAddress == 'website') $('#UserGivcSearchWebsite').parent().show('fast');
    $('#UserGivcSearchAddress').change(function() {
        var UserGivcSearchAddress = $('#UserGivcSearchAddress').val();
        if (UserGivcSearchAddress == 'agency') $('#UserGivcSearchWebsite').parent().hide('fast');
        if (UserGivcSearchAddress == 'address') $('#UserGivcSearchWebsite').parent().hide('fast');
        if (UserGivcSearchAddress == 'website') $('#UserGivcSearchWebsite').parent().show('fast');
    });
}

function userConfirmPoints () {
    if ($('#UserPaymentType').val() == 'points') {
        $('#UserConfirmPoints').parent().show('fast');
    } else {
        $('#UserConfirmPoints').parent().hide('fast');
    }
    $('#UserPaymentType').change(function() {
        if ($('#UserPaymentType').val() == 'points') {
            $('#UserConfirmPoints').parent().show('fast');
        } else {
            $('#UserConfirmPoints').parent().hide('fast');
        }
    });
}

function closeTabsForGIReward() {
    if($('#closeTabsForGIReward').val() == 0) {
        $('#nav ul li a').each(function(index) {
            var href_val = $(this).attr("href");
            if (href_val == "/membership") {
                $(this).css("display","none");
            } else if (href_val == "/my_certificates") {
                $(this).css("display","none");
            }
        });
    }
}

$(document).ready(function() {
    Toggler.bindTogler();
    Toggler.navToggler();
    filtersFormDisplay();
    toggleNext();
    hookCheckboxes();
    showNextRow();
    givc_web_search();
    userConfirmPoints();
    closeTabsForGIReward();
});
