function getAlias(obj) {
    separator = obj.attr('rel').indexOf('_') + 1;
    alias = obj.attr('rel').substring(separator);
    /*alias_tmp = obj.attr('href').replace('.html', '').split('/');
    if (alias_tmp[alias_tmp.length - 1] == '') alias_tmp.pop();
    alias = alias_tmp[alias_tmp.length - 1];*/
    return alias;
}

function updatePanel(obj, url,link) {

    $.ajax({
        url: '/ajax-backend/get-panel-content.html?alias=' + obj + '&langid=' + langid,
        dataType: 'json',
        success: function(data, textStatus, xhr) {

            trackVisit(url);

            if(data.navigation!="" && link.parent().find('ul').length==0){
            	link.after(data.navigation);	
            	link.parent().find('ul').show("blind", '', 500);
            }
            else{
            	link.parent().find('ul').hide("blind", '', 500);
            	setTimeout(function(){link.parent().find('ul').detach();},500);
            }
            
           	
           	if($('#contentCorporate').html()!="" || data.content!=""){

           	if($('#contentCorporate').html()!=""){

           		$('#contentCorporate').animate( {opacity: 0}, 500, function(){} );
            
            
            	setTimeout(function(){
            		$('#contentCorporate').hide("blind",{ direction: "vertical" }, 900);
            	 },500);
				setTimeout(function(){
            
            	if(data.content!="") $('#contentCorporate').html(data.content);
            	else $('#contentCorporate').html('');
            	
            	$('#contentCorporate').show("blind", { direction: "vertical" }, 900);
            	 setTimeout(function(){$('#contentCorporate').animate( {opacity: 1}, 900, function(){} );  },800);
            },900);
            
            }
            else{

            	$('#contentCorporate').hide("blind",{ direction: "vertical" }, 0);
            	$('#contentCorporate').animate( {opacity: 0}, 0, function(){} );
            	
            
            	if(data.content!="") $('#contentCorporate').html(data.content);
            	else $('#contentCorporate').html('');
            	
            	$('#contentCorporate').show("blind", { direction: "vertical" }, 900);
            	 setTimeout(function(){$('#contentCorporate').animate( {opacity: 1}, 900, function(){} );  },800);
            	
            	
            }
            
            
            
            }
            
            
            
            
            $('#menuContent A').removeClass('active');
            link.addClass('active');
        }
    });
}


jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}


function showShowcase(obj, url,link) { 
    $.ajax({
        url: '/ajax-backend/get-page.html?alias=' + obj + '&langid=' + langid,
        success: function(data, textStatus, xhr) {
           trackVisit(url);
            	
            if(data.content!="") $('#mainContentShowcase').html(data);
            
            var docHeight = $(document).height();

            $("body").append("<div id='overlay'></div>");
			//$("body").css('height','100%');
            $("#overlay")
               .height(docHeight)
               .css({
                  'opacity' : 1,
                  'position': 'absolute',
                  'top': 0,
                  'left': 0,
                  'background-color': 'white',
                  'width': '100%',
                  'display': 'none',
                  'z-index': 1000
               });
            
            $("#overlay").fadeIn(800);
            
			$('#contentShowcase').center();
            setTimeout(function(){$('#contentShowcase').fadeIn(800);},700);
            
        }
    });
}

function closePopup(){
	$('#contentShowcase').fadeOut(500);
	$("#overlay").fadeOut('slow', function() {
	    $("body").css('overflow','auto');
	  });
}


function trackVisit(url) {
    //_gaq.push(['_trackPageview', url]);
}



$(document).ready(function() {


	 $('#menuContent ul > li a').live('click',
			    function() {
			        alias = getAlias($(this));
			        url = '/' + $(this).attr('href');
			        updatePanel(alias, url,$(this));
			        //$.address.value($(this).attr('href').replace('http://' + document.location.hostname + '/', ''));
			        return false;
			    });
	 
	 $('#showcase a').live('click',
			    function() {
			        alias = getAlias($(this));
			        url = '/' + $(this).attr('href');
			        showShowcase(alias, url,$(this));
			        //$.address.value($(this).attr('href').replace('http://' + document.location.hostname + '/', ''));
			        return false;
			    });
			    

});
