// --------------------------------------------------------------------------------
// Durlinger category menu
// --------------------------------------------------------------------------------
function DurTargetMenu(id) {
	this.menu = $(id);

	// Set the height of collapsed menus to 60
	$(".durTargetMenuBlockCollapsed").each(function(intIndex){ $(this).css('height', '40px'); } );
	
	// Assign click event handler to titles
	var mainInstance = this;
	$(".durTargetMenuTitle").each(function(){
			$(this).click(function(){ mainInstance.toggleMenu(this.parentNode.parentNode); } );
		}
	);

	$(".durTargetMenuBlockExpanded a").not("#durProductType1Selected, #durProductType2Selected, #durProductType3Selected").click(function() { mainInstance.navigateURL($(this).attr("id")) });
}

DurTargetMenu.prototype.toggleMenu = function(submenu) {
        this.expandMenu(submenu);
};

DurTargetMenu.prototype.expandMenu = function(submenu) {
    // Collapse all currently expanded menus
    var mainInstance = this;
    $(".durTargetMenuBlockExpanded").each(function() { mainInstance.collapseMenu($(this)); });

    // Set current menu's class to expanded
    $(submenu).removeClass("durTargetMenuBlockCollapsed");
    $(submenu).addClass("durTargetMenuBlockExpanded");

    // Determine the full heigth summing height of elements
    var fullHeight = 0;
    $.each($(submenu).children(), function(index, child) { fullHeight += parseInt($(child).outerHeight(true)); });

    // Animate expansion
    $(submenu).animate({ height: fullHeight + 'px' }, 350, 'swing', // mainInstance.navigateURL($(submenu).attr("id")));
		function() {
		    mainInstance.navigateURL($(submenu).attr("id"));
		});
};

DurTargetMenu.prototype.navigateURL = function(id) {
    var url = String(document.location).toLowerCase();

    if (url.substring(url.length - 5) != '.aspx')
        url = url + 'Default.aspx';

    $.ajax({
        type: "POST",
        url: url, //"http://localhost/dotnetnuke/test/Default.aspx", //document.location, //"{{SITE_URL}}/Default.aspx",
        contentType: "application/json",
        cache: false,
        data: "{ CurrentURL: '" + document.location + "', BlockId: '" + id + "' }",
        dataType: "json",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("DNN-Service", "true");
            xhr.setRequestHeader("DNN-ServiceMethod", "DetermineURL");
        },
        success: function(msg) {
            window.location = msg.result;
            //alert(msg);
            //alert(msg.result);
        },
        error: function(x, e) {
            if (x.status == 0) {
                alert('You are offline!!\n Please Check Your Network.');
            } else if (x.status == 404) {
                alert('Requested URL not found.');
            } else if (x.status == 500) {
                alert('Internal Server Error.');
            } else if (e == 'parsererror') {
                alert('Parsing JSON Request failed.');
            } else if (e == 'timeout') {
                alert('Request Time out.');
            } else {
                alert('Unknow Error.\n' + x.responseText);
            }
        }
    });
}

DurTargetMenu.prototype.collapseMenu = function(submenu) {
	// Animate to collapsed height
    $(submenu).animate({height: '40px'}, 350, 'swing',
		function() {
			// Set class to collapsed
			$(submenu).removeClass("durTargetMenuBlockExpanded");
			$(submenu).addClass("durTargetMenuBlockCollapsed");
		}
	);
};

// --------------------------------------------------------------------------------
// Durlinger brand menu
// --------------------------------------------------------------------------------
function DurBrandMenu(id) {
	this.menu = $(id);
	
    var mainInstance = this;
	$("#durBrandMenuUpArrow").click( function() { mainInstance.scrollObjects(+1); } );
	$("#durBrandMenuDownArrow").click( function() { mainInstance.scrollObjects(-1); } );
	
	$("#durBrandMenuElementsList").css("position", "absolute");
	
	if ($("#durBrandMenuElementsList li").length > 0)
		this.elementHeight = $("#durBrandMenuElementsList li:first").outerHeight();
	else
	    this.elementHeight = 20;
	
    this.numberOfElementsInView = 15;

    $("#durBrandMenuInnerContainer").css("height", this.numberOfElementsInView * this.elementHeight + "px");
    $("#durBrandMenuMainContainer").css("height", 77 + this.numberOfElementsInView * this.elementHeight + "px");
	
    this.numberOfElements = $("#durBrandMenuElementsList li").length;
    this.minTop = Math.min((this.numberOfElementsInView - this.numberOfElements) * this.elementHeight, 0);

    $("#durBrandMenuElementsList li").not("#durBrandMenuElementSelected").click(function() { mainInstance.navigateURL($(this).attr("id")) });
};

DurBrandMenu.prototype.scrollObjects = function(dir) {
	var newTop = $("#durBrandMenuElementsList").position().top + dir * this.numberOfElementsInView * this.elementHeight;
	
	if (newTop < this.minTop)
		newTop = this.minTop;
		
	if (newTop > 0)
		newTop = 0;
	
    $("#durBrandMenuElementsList").animate({top: newTop + 'px'}, 350, 'swing');
};

DurBrandMenu.prototype.navigateURL = function(id) {
    var url = String(document.location).toLowerCase();

    if (url.substring(url.length - 5) != '.aspx')
        url = url + 'Default.aspx';

    $.ajax({
        type: "POST",
        url: url, //"http://localhost/dotnetnuke/test/Default.aspx", //document.location, //"{{SITE_URL}}/Default.aspx",
        contentType: "application/json",
        cache: false,
        data: "{ CurrentURL: '" + document.location + "', BlockId: '" + id + "' }",
        dataType: "json",
        beforeSend: function(xhr) {
            xhr.setRequestHeader("DNN-Service", "true");
            xhr.setRequestHeader("DNN-ServiceMethod", "DetermineURL");
        },
        success: function(msg) {
            window.location = msg.result;
            //alert(msg);
            //alert(msg.result);
        },
        error: function(x, e) {
            if (x.status == 0) {
                alert('You are offline!!\n Please Check Your Network.');
            } else if (x.status == 404) {
                alert('Requested URL not found.');
            } else if (x.status == 500) {
                alert('Internal Server Error.');
            } else if (e == 'parsererror') {
                alert('Parsing JSON Request failed.');
            } else if (e == 'timeout') {
                alert('Request Time out.');
            } else {
                alert('Unknow Error.\n' + x.responseText);
            }
        }
    });
}


