
function clearAll()
{
	$('.logo .title').html('');
	$('.page .inner-page').fadeOut();
	$('.page').fadeOut("slow");
	$('.lmenu').fadeOut("slow");
}

function clearInner()
{
	$('.page .inner-page').fadeOut("slow");
	$('.page .inner-page').html(' ');
}

$(document).ready(function(){
	
	current = 'home';
	
	$('.menu a').click(function(){
		
		if ($(this).attr('rel') == current)
			return;
		
		// Handle logo
		if (current == 'home') {
			$('.logo').toggle();
			$('.logo-home').toggle();
		}

		clearAll();
		
		$('.' + $(this).attr('rel')).show();
		$('.' + $(this).attr('rel')).find('.inner-page').hide();
		$('.' + $(this).attr('rel')).find('.lmenu').show("slide", { direction: "right" }, 1000);
		
		obj = $(this);
		setTimeout(function(){ 
			$('.logo .title').html($('.' + obj.attr('rel')).find('.lmenu .title').html()); }, 1000);
		
		current = $(this).attr('rel');

		if (current == 'contacts')
			$('.page .inner-page').show("slide", { direction: "right" }, 1000);

	});

	$('.showmap').click(function(){
		clearInner();
		$.get(URLlg + 'texts/view/4', function(data){
			$('.page .inner-page').html(data);
			$('.page .inner-page').show("slide", { direction: "right" }, 1000);
		});
	});

	$('.logo a').click(function(){
		clearAll();
		$('.logo').hide();
		$('.logo-home').toggle();
		$('.page.home').show("slide", { direction: "right" }, 1000);
		current = 'home';
	});
	var intervalup = '';
	var intervaldn = '';
	
	$('.scrolls-menu .down').mouseover(function(){
		var what = $(this).parent().attr('rel');
		intervalup = setInterval(function(){
			$(what).scrollTop($(what).scrollTop() + 4);
		}, 50);	
	});
	$('.scrolls-menu .down').mouseleave(function(){
		clearInterval(intervalup);
	});

	$('.scrolls-menu .up').mouseover(function(){
		var what = $(this).parent().attr('rel');
		intervaldn = setInterval(function(){
			$(what).scrollTop($(what).scrollTop() - 4);
		}, 50);
	});
	$('.scrolls-menu .up').mouseleave(function(){
		clearInterval(intervaldn);
	});
	

	$.history.init(loadContent);
});

function showBrand(brand)
{
	clearInner();
	$.get(URLlg + 'brands/view/' + $(brand).attr('data-brand_id'), function(data){
		$('.page.brands .inner-page').html(data);			
		$('.page.brands .inner-page').show("slide", { direction: "right" }, 1000);
		$('.menu-brands a').removeClass('selected');
		$(brand).addClass('selected');
		$("#jcontroller").jFlow({
			slides: "#slides",
			slideWrapper : "#jFlowSlide",
			width: "613px",  
			height: "340px",  
			duration: 600,
			auto: false
		});
	});
}

function showService(service)
{
	clearInner();
	$.get(URLlg + 'services/view/' + $(service).attr('data-service_id'), function(data){
		$('.page.services .inner-page').html(data);			
		$('.page.services .inner-page').show("slide", { direction: "right" }, 1000);
		$('.menu-services a').removeClass('selected');
		$(service).addClass('selected');
		$("#jcontroller").jFlow({  
			slides: "#slides",
			slideWrapper : "#jFlowSlide",
			width: "613px",  
			height: "340px",  
			duration: 600,
			auto: false
		});
	});
}

function loadContent(hash) {
	if  (hash != '')
		$('.intro').hide();

	if (hash == 'services' || hash == 'brands' || hash == 'contacts') {
		$('.menu a[rel=' + hash + ']').click();
	}

	if (hash.search('brand/') == 0) {
		if (current != 'brands')
			$('.menu a[href="#brands"]').click();
		showBrand($('.menu-brands a[href="#' + hash + '"]'));
	}

	if (hash.search('service/') == 0) {
		if (current != 'services')
			$('.menu a[href="#services"]').click();
		showService($('.menu-services a[href="#' + hash + '"]'));
	}
}

function hideIntro()
{
	$('.intro').hide();
	$('.logo a').click();
}
