var XMLCONTENT		= null;
var ajaxFrontEnd        = 'xml/static_frontend.xml';
var BlockHeight         = 0;
var state               = 'stoped';
var hrefState		= null;
var title 		= null;
var shown 		= false;

function getXmlHttp() {
    var xmlhttp;
    if (window.XMLHttpRequest) {
	xmlhttp = new XMLHttpRequest();
	if (xmlhttp.overrideMimeType) {
	    xmlhttp.overrideMimeType('text/xml') };
    } else if (window.ActiveXObject) {
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xmlhttp;
}

function init_from_xml(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())
					{
						if(getAnchor()!='offer') RenderText(getAnchor());
						else if(!shown && (typeof ExternalInterface('main').ShowOrderForm != undefined))
						{
							ExternalInterface('main').ShowOrderForm();
							hideTeaser();
							shown = true;
						}
                	}
            	}, 350);
			}, 1000);
			
}

$(window).ready(function(){
	
	var xmlhttp = getXmlHttp();
	xmlhttp.open("GET", ajaxFrontEnd, false);
	xmlhttp.send(null);
	init_from_xml(xmlhttp.responseXML);
	
	$('#teaser').bind('click',function() 
	{
		window.open('http://www.chevrolet-hr.com/cruze/cruze-index.html');
/*		if (typeof ExternalInterface('main').ShowOrderForm != undefined)
		{
			hideTeaser();
			ExternalInterface('main').ShowOrderForm(); 
		}*/
	});
	
});

function showTeaser()
{
	$('#teaser').show();
}

function hideTeaser()
{
	$('#teaser').hide();
}

var firstrun = null;
function switchMenu(department){	
}

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: 'swf/fonts/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(){	
    /* 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);
}