var XMLCONTENT			= null;
var ajaxFrontEnd        = 'layout/xml/static_frontend.xml';
var BlockHeight         = 0;
var state               = 'stoped';
var hrefState			= null;
var title 				= null;
 
$(window).ready(function(){
	$.get(
		ajaxFrontEnd,
		{},
		function(xml){	
			XMLCONTENT = xml; 
			init();  
			
			hrefState = $('startdepartment', XMLCONTENT).attr('id'); 
			startState = '';
			RenderText($('startdepartment', XMLCONTENT).attr('id'));
			 
			setInterval(function(){
				if (typeof title != 'undefined' && document.title != title){
					document.title = title;	
				}
            }, 200);
			
			setTimeout(function(){
				checkForNewAnchor = setInterval(function(){
                	if (hrefState != getAnchor()){
                 		RenderText(getAnchor());
                	}
            	}, 350);
			}, 1000);
			
				
		}
	);
	
});

var firstrun = null;
function switchMenu(department){
	
 	
	if (hrefState == department && typeof firstrun == 'string') {
		return true;
	} else { 
		 
		if (typeof ExternalInterface('main').Switch == 'function'){
			if (department == 'MOTORSHOW'){
				ExternalInterface('main').Switch('MotorShow');
			} else {
				ExternalInterface('main').Switch('TextDepartment');
			}
		}
		
		firstrun = 'yes';
		
		hrefState = department;
		$('.slogan h3 a').removeClass('act').removeClass('not');
    	$('.slogan h3 a[@rel="'+department+'"]').addClass('act');
        $('.slogan h3 a').not('.act').addClass('not');
        
        $('.slogan h3 a.act').sifr({unsifr: true});
        $('.slogan h3 a.act').sifr({
            font: 'layout/swf/klavika-font',
            color:'#00000',
            height:15
        });

        $('.slogan h3 a.not').sifr({unsifr: true});
        $('.slogan h3 a.not').sifr({
            font: 'layout/swf/klavika-font',
            color:'#9A9A9A',
            hover:'#000000',
            height:15
        });
			
	}
}

function RenderText(section){
	if (startState == section || typeof section == 'undefined'){
		return false;
	}
	startState = section;
	
	title =  $('sitetitle', XMLCONTENT).attr('title') + ' | ' + $('item[@id='+section+']', XMLCONTENT).attr('title');
 
 	
	switchMenu($('item#'+section, XMLCONTENT).parent().attr('id')); 
	
	
	
	data = $('item[@id='+section+']', XMLCONTENT);
	$('.slogan h2').sifr({ unsifr: true});
	$('.slogan h2').html('');
	$('.slogan h2').html($('item[@id='+section+']', XMLCONTENT).attr('title'));
		    
	$('.slogan h2').sifr({
        font: 'layout/swf/klavika-font',
        color:'#4E4E4E',
        beforeEach: function () {
		    prevheight = parseInt($('#parent').css('height'));
		
		    $('#nested').html('');
		    $('#parent').css({height:'auto' , overflow:'visible'});
		    $('#nested').css('marginTop', '0px').html($.trim($(data).text()));
		    BlockHeight = parseInt($('#parent').height());
			
		    $('#parent').css({height:prevheight , overflow:'hidden'});
		    $('#arrows').css({visibility:((BlockHeight > prevheight) ? 'visible':'hidden') });
        },
        afterEach:  function () { 
        
        }
    });
	 
}

function init(){
	
	/* menu rendering */
	var menu = new Array(); var i = 0;
	$('department', XMLCONTENT).each(function(){
    	menu[i] = '<a href="#'+$(this).attr('id')+'" class="dep'+$(this).attr('id')+'" rel="'+$(this).attr('id')+'" >'+$(this).attr('title')+'</a>'+"\n";
        i++;
    });
    
    $('.slogan h3').html(menu.join(' <span class="spec">|</span> ', menu));
    
    
    /* arrows navigation */
    $('#arrows span').each(function(){
    	$(this).bind('click', function(){
        	move($(this).attr('class'))
        });

           
        $(this).bind('mouseover', function(){movedyn('start', $(this).attr('class'), 'fast')});
        $(this).bind('mouseout', function(){movedyn('stop')});
    });
    
}
function getAnchor(){
    if(typeof location.hash != undefined) {
	var name = location.hash.split("#");
	return name[1];
    }
    return false;
}
function ExternalInterface(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
    } else {
		return document[movieName];
    }
}
function move(type){
    if (state == 'stoped' && typeof startScroll != 'undefined'){
        clearInterval(startScroll); return false;
    }

    var parrentDivHeight = parseInt($('#parent').css('height'));
    var parrentDivLineHeight = parseInt($('#parent').css('lineHeight'));
    var nestedDivMarginTop = parseInt($('#nested').css('marginTop'));

    switch(type){
        case 'up':
            if (nestedDivMarginTop != '0'){
                $('#nested').css('marginTop', (nestedDivMarginTop + parrentDivLineHeight) + 'px');
            }
        break;
        case 'down':
           if ( ((0-nestedDivMarginTop) + parrentDivHeight) < BlockHeight){
                $('#nested').css('marginTop', (nestedDivMarginTop - parrentDivLineHeight) + 'px');
           }
        break;
    }
}
function movedyn(start, direction, speed){
    if (start == 'stop'){
        state = 'stoped';
        clearInterval(startScroll);
        return false;
    }

    state = 'go fast dude!';
    speed = (speed == 'fast') ? 300 : ((speed == 'slow') ? 750 : ((speed == 'normal') ? 500 : 500 ));
    startScroll = setInterval(function(){
        move(direction)
    }, speed);
}

function getme(lang){
	var a = (Math.random()+"") * 10000000000000;
	switch(lang){
		case 'TR':
			src = 'http://ad.doubleclick.net/activity;src=1655491;type=esubs735;cat=ecruz867;ord=1;num='+ a + '?';
		break;
		case 'FR':
			src = 'http://ad.doubleclick.net/activity;src=1655526;type=esubs142;cat=ecruz998;ord=1;num='+ a + '?';
		break;
		default:
			src = 'http://fls.doubleclick.net/activityi;src=1139856;type=x10ms449;cat=cruze825;ord='+ a + '?';
		break;
	}

	if (lang == 'EN'){
		$('#iframe').attr('src', src);
	} else {
		$('#invisibleimage').attr('src', src);
	}
}
