$(document).ready(function() {
	bindings();
	checkWidth();
	pathNow = $.address.value();
	urlToShowNow = '';
	
	if($.address.value() != '/'){
		var btnToClick = $('a[href="/'+lang+$.address.value()+'/"]'); 
		
		btnToClick.click();
	}
	
	$('#transBG').hide();
	
	
					
})

$.address.change(function(event) {  
	if(pathNow != $.address.value()){
		$('a[href="/'+lang+$.address.value()+'/"]').click();
	}
    
});

function bindings(){
	$(window).bind('resize', checkWidth);
		
	$('.tab a').bind('click', showPreview);
	$('#home, #logo a').bind('click', showPreview);
	$('#navigation a').bind('click', hidePreview);
	
	//$('.box a').bind('click', showDetails);
	$('#detailclose').bind('click', hideDetails);
	
	//$('#navigation a, #footer a, a.meta').bind('click', loadContent);	
	$('#navigation a, #footer a, a.meta, #logo a').bind('click', loadContent);	
	$('.tab a, .box a').bind('click', loadTip);
	
	
}


/// Load Function
function loadContent() {
	$('.current').removeClass('current');
	$(this).addClass('current');
	
	var url = $(this).attr("href").replace("/"+lang+"/", "/");
	pathNow = url;
	$.address.value(url);
	
	
	var urlToShow = $(this).attr("href")+'?d=1';
			
	$('#main').load(urlToShow+' #data_main', function() { checkWidth(); });
	$('#sidebar').load(urlToShow+' #data_sidebar');
	
			
	hideDetails();
	return false;
	
	
}

function loadTip() {
	if(urlToShowNow != urlToShow){
		$('.current').removeClass('current');
		$(this).addClass('current');
		$('#detailcontent').empty();
		$('#detailbox').css("top",-700);
	}
	
	var urlToShow = $(this).attr("href")+'?d=1';
	
	var url = $(this).attr("href").replace("/"+lang+"/", "/");
	pathNow = url;
	$.address.value(url);
		
	var urlToShowHome = $('#home').attr("href")+'?d=1';
	$('#main').load(urlToShowHome+' #data_main');
	$('#sidebar').load(urlToShowHome+' #data_sidebar');
	
	
	if(urlToShowNow != urlToShow){
		urlToShowNow = urlToShow;
		$.ajax({
			url: urlToShow,
			cache: false,
			success: function(html){
				$('#detailcontent').html(html);
				showDetails();
			}
		});	
	}
	
	return false;
}



// Resize
var paramHeight = 530;
function checkWidth() {
   	if($('body').width() < 990){
		$('#topBG').width('990');
		$('#header').width('990');
		$('#gradientBG').width('990');
		$('#content').width('990');
	} else {
		$('#topBG').width('100%');
		$('#header').width('100%');
		$('#gradientBG').width('100%');
		$('#content').width('100%');
	}
	
	$('#main').css({ 'min-height' : $('body').height()-paramHeight });	
	$('#navigation').css({ 'min-height' : $('#main').height()});	
}





// Scroll Preview		
var direction = 'center';		
$(document).mousemove(function(e){
	if(e.pageY > 200 && e.pageY < 400 && $('body').width() < $('#previewContainer').width()){	
		if(e.pageX <= 400){
			if(direction != 'left'){
				$('#previewContainer').stop(true);
				$('#previewContainer').animate({ marginLeft: '-440' }, 1000  );
			}						
			direction = 'left';
		} else if(e.pageX >= $('body').width()-400){
			if(direction != 'right') {
				$('#previewContainer').stop(true);
				$('#previewContainer').animate({ marginLeft: '-740' }, 1000  );
			}						
			direction = 'right';
		} else {
			if(direction != 'center'){
				$('#previewContainer').stop(true);
				$('#previewContainer').animate({ marginLeft: '-590' }, 1000  );
			} 						
			direction = 'center';
		}
		
	} else {
		if(direction != 'center'){
			$('#previewContainer').stop(true);
			$('#previewContainer').animate({ marginLeft: '-590' }, 1000  );
		}
		direction = 'center';
	}		      
}); 





// Box Scale
$('.box img').mouseover(function(e){
	$(this).animate({width: '300'}, {duration: 300});
});

$('.box img').mouseout(function(e){
	$(this).animate({width: '280'}, {duration: 300});			
});





// Preview Show/Hide

function showPreview(){
	$('#header').animate({"margin-bottom": '0'}, 700  );
	$('#container').animate({ "margin-top": '0' }, 700 , function() {
		paramHeight = 530;
		checkWidth();
	});
	$('#tabcontainer').animate({ "margin-top": '0' }, 600 );
}



function hidePreview(){
	$('#header').animate({"margin-bottom": '-285'}, 900  );
	$('#tabcontainer').animate({ "margin-top": '-24' }, 700  );
	paramHeight = 245;
	checkWidth();
	hideDetails();
}






// Detail Box Show/Hide

function showDetails(){
	$('#detailbox').animate({ "top": '0' }, {duration:500,  easing: 'easeOutQuint'} );
	$('#transBG').fadeIn('slow'); 
	$('#transBG').bind('click', hideDetails);
	
	$('#videotxt > p a').each(function() {
	    if($(this).attr('target') == ''){
			if($('#navigation a[href="'+$(this).attr('href')+'"]').length){
				$(this).click(function(){
					$('#navigation a[href="'+$(this).attr('href')+'"]').click();
					return false;
				})
			}
			
			if($('.tab a[href="'+$(this).attr('href')+'"]').length){
				$(this).click(function(){
					$('.tab a[href="'+$(this).attr('href')+'"]').click();
					return false;
				})
			}
			
		}
	});
		
}



function hideDetails(){
	//$('#detailbox').animate({ "top": '-500' }, 1200 );
	$('#detailbox').css("top",-700);
	$('#detailcontent').empty();
	$('#transBG').fadeOut('slow'); 
	return false;
}

