$(window).load(
    function() {
        positionFooter();
    }
);
$(document).ready(function () {


    var accordionIndex = $("#activeAccordion").val();
    if (accordionIndex != -1) {
        accordionIndex = parseInt(accordionIndex, 10);  // convert a string to a number
        $("#categories").accordion();
        $("#categories").accordion('activate', accordionIndex);
    }
    else {
        $("#categories").accordion({ collapsible: true, active: false });
    }

    $(window)
       .scroll(positionFooter)
       .resize(positionFooter);

    //Category selected event
    $('#categories ul li a').click(function () {

        //        //Clear main content
        //        clearMainContent();

        //        //Load products for the selected category
        //        getPartialView('/Products/ByCategory', null, '#maincontent');

        //        $('#maincontent').fadeIn();

        //        return false;
    });
});

function clearMainContent() {
    $('#maincontent').html('');
}

function positionFooter() {
		/*
		var nonMainContent = $('.header').height() + $('.headermenu').height() + $('.footer').height();
	    var calcHeight = $(window).scrollTop() + $(window).height();
	    var mainContentHeight = $('.master-wrapper-content-below-header').height() + nonMainContent;
	    if (calcHeight > mainContentHeight) {
			calcHeight = calcHeight - nonMainContent;
			calcHeight += "px" 
			$(".master-wrapper-content-below-header").css({ height:  calcHeight});
	    }*/
		var calcHeight = ($(window).scrollTop() + $(window).height() - $(".footer").height() - 169);
		var mainContentHeight = $('.mainContent').height() + 20;
		var isFooterOverlappingContent = findIntersectors('.footer', '.mainContent').length > 0;
		if ((calcHeight > mainContentHeight)) {
		    if (isFooterOverlappingContent) {
		        calcHeight = $(document).height() - $("body").offset().top;
		    }
		    
			calcHeight += "px";
			$(".master-wrapper-content-below-header").css({ height: calcHeight });
		}
}

function findIntersectors(targetSelector, intersectorsSelector) {
    var intersectors = [];

    var $target = $(targetSelector);
    var tAxis = $target.offset();
    var t_x = [tAxis.left, tAxis.left + $target.outerWidth()];
    var t_y = [tAxis.top, tAxis.top + $target.outerHeight()];

    $(intersectorsSelector).each(function() {
          var $this = $(this);
          var thisPos = $this.offset();
          var i_x = [thisPos.left, thisPos.left + $this.outerWidth()]
          var i_y = [thisPos.top, thisPos.top + $this.outerHeight()];

          if ( t_x[0] < i_x[1] && t_x[1] > i_x[0] &&
               t_y[0] < i_y[1] && t_y[1] > i_y[0]) {
              intersectors.push($this);
          }

    });
    return intersectors;
}

function getPartialView(methodUrl, params, partialViewForm) {
    $(partialViewForm).html("Loading...");
    $.ajax({
        url: methodUrl,
        data: params,
        type: 'GET',
        dataType: 'html', // <-- to expect an html response
        success: function (result) { doSubmitSuccess(result, partialViewForm); }
    });
}

function doSubmitSuccess(result, view) {
    $(view).html(result);
}
