$(document).ready(
    function() {	
        setListenerMainMenu();
		$('iframe').attr('allowTransparency',true);
		var url = $(location).attr('href');		
		if (url == 'http://www.fiatprofessional.de/de/galerie'){
		var currentIdx = 1;
		$('#thumDucato1').bind('click',function() {
			showBigDucato(1,currentIdx);
			currentIdx = 1;
		});		
		$('#thumDucato2').bind('click',function() {
			showBigDucato(2,currentIdx);
			currentIdx = 2;
		});		
		$('#thumDucato3').bind('click',function() {
			showBigDucato(3,currentIdx);
			currentIdx = 3;
		});
		$('#thumDucato4').bind('click',function() {
			showBigDucato(4,currentIdx);
			currentIdx = 4;
		});
		}
		else if (url == 'http://www.fiatprofessional.de/de/Scudo-Modular-Galerie')
		{
		var currentIdx = 1;
		$('#thumScudo1').bind('click',function() {
			showBigScudo(1,currentIdx);
			currentIdx = 1;
		});		
		$('#thumScudo2').bind('click',function() {
			showBigScudo(2,currentIdx);
			currentIdx = 2;
		});		
		$('#thumScudo3').bind('click',function() {
			showBigScudo(3,currentIdx);
			currentIdx = 3;
		});
		$('#thumScudo4').bind('click',function() {
			showBigScudo(4,currentIdx);
			currentIdx = 4;
		});
		}
    else if (url == 'http://www.fiatprofessional.de/de/Strada-Galerie') {
    var currentIdx = 1;
    $('#thumStrada1').bind('click', function () {
        showBigStrada(1, currentIdx);
        currentIdx = 1;
    });
    $('#thumStrada2').bind('click', function () {
        showBigStrada(2, currentIdx);
        currentIdx = 2;
    });
    $('#thumStrada3').bind('click', function () {
        showBigStrada(3, currentIdx);
        currentIdx = 3;
    });
    $('#thumStrada4').bind('click', function () {
        showBigStrada(4, currentIdx);
        currentIdx = 4;
    });
    }			
		else if (url=="http://www.fiatprofessional.de/de/Angebote-Doblo-Cargo")
		{
			$('body').append('<iframe src="http://gmads.net/pct?gmtevt=8a8386853127a824013127e57a9100b4&gmtcl=1&gmtr=' + Math.floor(Math.random() * 9999999999) + '" style="overflow:hidden" frameborder="0" width="0" height="0"></iframe>');
		}
		else if (url=="http://www.fiatprofessional.de/de/Modelle/Doblo-Cargo")
		{
		$('body').append('<iframe src="http://gmads.net/pct?gmtevt=8a8386853127a824013127e57a9100b4&gmtcl=1&gmtr=' + Math.floor(Math.random() * 9999999999) + '" style="overflow:hidden" frameborder="0" width="0" height="0"></iframe>');
		}
		else if (url=="http://www.fiatprofessional.de/")
		{		
		$('body').append('<iframe src="http://gmads.net/pct?gmtevt=8a838685314173b60131430c36e203ee&gmtcl=1&gmtr=' + Math.floor(Math.random() * 9999999999) + '" style="overflow:hidden" frameborder="0" width="0" height="0"></iframe>');
		}
		else if (url=="http://www.fiatprofessional.de/de/Modelle/Ducato-Warentransport")
		{
		$('body').append('<iframe src="http://gmads.net/pct?gmtevt=8a838685314173b60131430c36e203ee&gmtcl=1&gmtr=' + Math.floor(Math.random() * 9999999999) + '" style="overflow:hidden" frameborder="0" width="0" height="0"></iframe>');
		}
		else if (url=="http://www.fiatprofessional.de/de/Angebote-Ducato")
		{
		$('body').append('<iframe src="http://gmads.net/pct?gmtevt=8a838685314173b60131430c36e203ee&gmtcl=1&gmtr=' + Math.floor(Math.random() * 9999999999) + '" style="overflow:hidden" frameborder="0" width="0" height="0"></iframe>');
		}
		else if (url=="http://www.fiatprofessional.de/de/Doblo-Work-Up")
		{
		$('body').append('<iframe src="http://gmads.net/pct?gmtevt=8a838685310a1a0401311d1a6340063b&gmtcl=1&gmtr=' + Math.floor(Math.random() * 9999999999) + '" style="overflow:hidden" frameborder="0" width="0" height="0"></iframe>');
		}
		else if (url=="http://www.fiatprofessional.de/de/Modelle/Doblo-Work-Up")
		{
		$('body').append('<iframe src="http://gmads.net/pct?gmtevt=8a838685310a1a0401311d1a6340063b&gmtcl=1&gmtr=' + Math.floor(Math.random() * 9999999999) + '" style="overflow:hidden" frameborder="0" width="0" height="0"></iframe>');
		}
		else if (url.indexOf("Konfigurator") != -1)
		{
		$('body').append('<iframe src="https://tracking.quisma.com/l.cfs?qupid=33504&CID=501&OrderID=&Price=&CurrencySymbol=EUR&Note=" width="1" height="1"'+ 
        ' frameborder="0"></iframe>');
		}
		else if (url=="http://www.fiatprofessional.de/de/Prospekte")
		{
		$('body').append('<iframe width = "1" height = "1" frameborder = "0" src="https://tracking.quisma.com/l.cfs?action=counter&qupid=33504&CID=302"></iframe>');
		}
		
		if ((url=="http://www.fiatprofessional.de/de/")|| (url=="http://www.fiatprofessional.de/de/Angebote-Ducato") ||(url=="http://www.fiatprofessional.de/de/Prospekte") || (url=="http://www.fiatprofessional.de/de/Angebote") || (url=="http://www.fiatprofessional.de/de/Modelle/Ducato-Warentransport") || (url=="http://www.fiatprofessional.de/de/Reisemobile/Einleitung") || (url=="http://www.fiatprofessional.de/de/Modelle/Ducato-Warentransport/Konfigurator") || (url=="http://www.fiatprofessional.de/de/Aufbauten-Uebersicht") || (url=="http://www.fiatprofessional.de/de/Haendlersuche") || (url=="http://www.fiatprofessional.de/de/Modelle/Scudo-Warentransport") || (url=="http://www.fiatprofessional.de/de/Modelle/Scudo-Personentransport") || (url=="http://www.fiatprofessional.de/de/Modelle/Ducato-Personentransport") || (url=="http://www.fiatprofessional.de/de/Modelle/Ducato-Warentransport/Konfigurator") || (url=="http://www.fiatprofessional.de/de/Modelle/Neuer-Doblo-Cargo/Konfigurator") || (url=="http://www.fiatprofessional.de/de/wohnmobile/Wohnmobil-Hersteller") || (url=="http://www.fiatprofessional.de/de/Angebote-Doblo-Cargo") || (url=="http://www.fiatprofessional.de/de/Modelle/Ducato-Personentransport/Konfigurator") || (url=="http://www.fiatprofessional.de/de/Modelle/Scudo-Personentransport/Konfigurator") || (url=="http://www.fiatprofessional.de/de/Modelle/Fiorino-Warentransport") || (url=="http://www.fiatprofessional.de/de/Modelle/Strada-Pick-Up"))
		{
			addJavascript('http://scripts.psyma.com/layer.php?xmlPath=nfzbm/nfzbm_fiat','body');
		}
    });
	
function addJavascript(jsname,pos) {
	var th = document.getElementsByTagName(pos)[0];
	var s = document.createElement('script');
	s.setAttribute('type','text/javascript');
	s.setAttribute('src',jsname);
	th.appendChild(s);
} 

function showBigDucato(index,currentIdx){
	var src = 'http://www.fiatprofessional.de/de/CMSDE/PublishingImages/imagesUpload/Branchenmobile/big_ducato_'+index+'.jpg';	
	$('.imageBottom').children('img').attr('src',src);		
	$('#thumbsGallery').children().eq(currentIdx-1).removeClass('classPressed');
	$('#thumbsGallery').children().eq(index-1).addClass('classPressed');
}	

function showBigScudo(index,currentIdx){
	var src = 'http://www.fiatprofessional.de/de/CMSDE/PublishingImages/imagesUpload/Branchenmobile/ScudoModular/Einstellung_0'+index+'_big.jpg';	
	$('.imageBottom').children('img').attr('src',src);		
	$('#thumbsGallery').children().eq(currentIdx-1).removeClass('classPressedScudo');
	$('#thumbsGallery').children().eq(index-1).addClass('classPressedScudo');
}
function showBigStrada(index, currentIdx) {
    var src = 'http://www.fiatprofessional.de/de/CMSDE/PublishingImages/imagesUpload/Models/Strada/big_strada_' + index + '.jpg';
    $('.imageBottom').children('img').attr('src', src);
    $('#thumbsGallery').children().eq(currentIdx - 1).removeClass('classPressed');
    $('#thumbsGallery').children().eq(index - 1).addClass('classPressed');
}

function setListenerMainMenu() {

    // Main Menu
    var nVoiceMainMenu = $('.firstLevel > li').length;
    
    //
    $('.firstLevel > li').hoverIntent(function() {

        // MouseOver
        var sClass = $(this).attr('class');
        sClass = '.' + sClass.substring(0, 3);
        var basePath = '.firstLevel > ' + sClass;
        if ($(basePath + ' > .menuSecond').attr('id') != undefined) {

            // recupero l'id del div che contiene le voci del sottomenu
            var sSecondMenuId = $(basePath + ' > .menuSecond').attr('id');
            sIdSecondMenu = sSecondMenuId.replace('menuSecond', '');

            // carico il contenuto del div
            $('#' + sSecondMenuId).html($('#content_menuSecond' + sIdSecondMenu).html());            

            setListenerSecondMenu(sSecondMenuId);

        }
    }, function() {
        // MouseOut
        var sClass = $(this).attr('class');
        sClass = '.' + sClass.substring(0, 3);
        var basePath = '.firstLevel > ' + sClass;
        if ($(basePath + ' > .menuSecond').attr('id') != undefined) {

            // recupero l'id del div che contiene le voci del sottomenu
            var sSecondMenuId = $(basePath + ' > .menuSecond').attr('id');
            sId = sSecondMenuId.replace('menuSecond', '');

            // carico il contenuto del div
            $('#' + sSecondMenuId).html('');
        }
    });
}

function setListenerSecondMenu(sSecondMenuId) {
    //Aggiungo i listener per le sottovoci
    $('#' + sSecondMenuId + ' > .secondLevel > li').hoverIntent(function() {
        var sIdSecondLi = $(this).attr('id');
        if ($('#' + sIdSecondLi + ' > .menuThird').attr('id') != undefined) {
            
            // recupero l'id del div che contiene le voci del sottomenu
            var sThirdMenuId = $('#' + sIdSecondLi + ' > .menuThird').attr('id');
            sIdThirdMenu = sThirdMenuId.replace('menuThird', '');

            // Cambio classe per sottomenu aperto
            $('#' + sSecondMenuId + ' > .secondLevel > #' + sIdSecondLi + ' > a').attr({ 'class': 'submenu active' });
            $('#menuThird' + sIdThirdMenu).html($('#content_menuThird' + sIdThirdMenu).html());

            // Setto i listener delle voci del sottomenu
            setListenerThirdMenu(sThirdMenuId);

        }
    }, function() {
        var sIdSecondLi = $(this).attr('id');
        if ($('#' + sIdSecondLi + ' > .menuThird').attr('id') != undefined) {
            
            // recupero l'id del div che contiene le voci del sottomenu
            var sThirdMenuId = $('#' + sIdSecondLi + ' > .menuThird').attr('id');
            sIdThirdMenu = sThirdMenuId.replace('menuThird', '');
            
            // Cambio classe per sottomenu aperto
            $('#' + sSecondMenuId + ' > .secondLevel > #' + sIdSecondLi + ' > a').attr({ 'class': 'submenu' });
            $('#menuThird' + sIdThirdMenu).html('');
        }
    });
}

function setListenerThirdMenu(sThirdMenuId) {
    $('#' + sThirdMenuId + ' > .thirdLevel > li').hoverIntent(function() {
        var sIdThirdLi = $(this).attr('id');
        if ($('#' + sIdThirdLi + ' > .menuFourth').attr('id') != undefined) {

            // recupero l'id del div che contiene le voci del sottomenu
            var sFourtMenuId = $('#' + sIdThirdLi + ' > .menuFourth').attr('id');
            sIdFourtMenuId = sFourtMenuId.replace('menuFourth', '');

            // Cambio classe per sottomenu aperto
            $('#' + sThirdMenuId + ' > .thirdLevel > #' + sIdThirdLi + ' > a').attr({ 'class': 'submenu active' });            
            $('#menuFourth' + sIdFourtMenuId).html($('#content_menuFourth' + sIdFourtMenuId).html());
        }
    }, function() {
        var sIdThirdLi = $(this).attr('id');
        if ($('#' + sIdThirdLi + ' > .menuFourth').attr('id') != undefined) {            

            // recupero l'id del div che contiene le voci del sottomenu
            var sFourtMenuId = $('#' + sIdThirdLi + ' > .menuFourth').attr('id');
            sIdFourtMenuId = sFourtMenuId.replace('menuFourth', '');

            // Cambio classe per sottomenu aperto
            $('#' + sThirdMenuId + ' > .thirdLevel > #' + sIdThirdLi + ' > a').attr({ 'class': 'submenu' });
            $('#menuFourth' + sIdFourtMenuId).html('');
        }
    });
}
