
function initMap() {

	// set locations
	var myLatLngLija = new google.maps.LatLng(35.905313, 14.441517);
	var myLatLngMgarr = new google.maps.LatLng(35.918778, 14.367711);
	
    var myOptions = {
      zoom: 17,
      center: myLatLngLija,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map-overlay"),myOptions);

    
    var marker = new google.maps.Marker({
        position: myLatLngLija,
        map: map
    });
    
    
    var marker = new google.maps.Marker({
        position: myLatLngMgarr,
        map: map
    });
    
    window.showLocation = function(loc){
		if(loc == 'lija') map.panTo(myLatLngLija);
		if(loc == 'mgarr') map.panTo(myLatLngMgarr);
	}
}
  
function setSaleNames(){
	// workaround to set selected cars for booking
	var forms = jQuery('.book-form-wrap input.sale-item');
	var itemTitle = '';
	
	jQuery.each(forms,function(){
		itemTitle = jQuery(this).parents('.options-wrap').prev().find('.info-head h2').html().split('<span>');		
		jQuery(this).val(itemTitle[0]);
	});
}
  
function bookFormSubmitted(data){
	jQuery(data.into).parents('.option-wrap').find('.book-form-wrap').hide();
	jQuery(data.into).parents('.option-wrap').find('.book-form-submitted').show();
}

function continueBrowsingSale(el){
	jQuery(el).parents('.options-wrap').prev().find('.info-options li a').removeClass('active');
	jQuery(el).parents('.options-wrap').hide();
}

function continueBrowsing(){
	jQuery('.options-wrap').hide();
	jQuery('.book-service a').removeClass('active');
}

function gotoSlide(el,tot){
	var carousel = jQuery(el).parents('.jcarousel-container');
	var li = jQuery(el).parent();
	
	carousel.find('.active').removeClass('active');
	li.addClass('active');
	var id = (li.attr('jcarouselindex')-1) % tot;
	id = id<0 ? tot+id : id;
	carousel.prev().cycle(id);
}

jQuery(document).ready(function(){
	// init homepage banners
	jQuery('#home-banner').cycle({
	    speed:  800,
	    pager:  '#banner-nav',
	    timeout: 8000 
	});
	
	// init brand index banners
	jQuery('#brandindex-banner').cycle({
	    speed:  800,
	    pager:  '#banner-nav',
	    timeout: 8000 
	});
	
	// display only submenus hotfix
	jQuery('.sub-navigation .menu .page-item-<?php echo $curPage; ?> .sub-menu').appendTo('.sub-navigation .wrap');
	
	// add class to last subdiv
	jQuery('.sub-navigation ul li:last').addClass('last-item');
	
	// show load when booking submit is clicked (hotfix)
	var submit = jQuery('.book-submit');
	var loader = '<span class="book-loader" style="display: none;" />';
	var oldLoader = submit.next().hide();
	
	submit.after(loader).click(function(){
		jQuery(this).next().show();
	});
	
	// check for original loader visibility
	setInterval(function(){
		for(var i = 0; i < oldLoader.length; i++){
			if(oldLoader[i].style.visibility == 'hidden')
				jQuery(oldLoader[i]).prev().hide();
		}
	}, 300);
	
	// init sales page menu
	jQuery('.info-options li a').click(function(){
	
		if(jQuery(this).hasClass('active')) {
			jQuery('.options-wrap').hide();
			jQuery(this).removeClass('active');
		} else {
			var curOption = jQuery(this).parent().attr('class');
			var curSale = jQuery(this).parents('.content-sales-wrap').attr('id');
			
			jQuery('.info-options li a').removeClass('active');
			jQuery(this).addClass('active');
			
			jQuery('.options-wrap').hide();
			
			jQuery('#'+curSale+' .options-wrap .option-wrap').hide();
			jQuery('#'+curSale+' .options-wrap').show();
			jQuery('#'+curSale+' .options-wrap #'+curOption+'-wrap-'+curSale.split('content-sales-')[1]).show();
		}
	})
	
	// init image thumbs
	jQuery('.sales-images-large').cycle({
		timeout: 	0,
		fx: 		"scrollHorz",
		speed: 		300
	});
	
	jQuery('.thumbs-nav.next').click(function(){
		var curSale = jQuery(this).parents('.content-sales-wrap').attr('id');
		jQuery('#'+curSale+' .thumbs-img').removeClass('active');
		jQuery('#'+curSale+' .sales-images-large').cycle('next');		
	})
	
	jQuery('.thumbs-nav.prev').click(function(){
		var curSale = jQuery(this).parents('.content-sales-wrap').attr('id');
		jQuery('#'+curSale+' .thumbs-img').removeClass('active');
		jQuery('#'+curSale+' .sales-images-large').cycle('prev');
	})
	
	// init book service banner
	jQuery('.book-service a').click(function(){
		if(jQuery(this).hasClass('active')){
			jQuery(this).removeClass('active');
			jQuery(this).parents('.book-service').next().hide();
		} else {
			jQuery(this).addClass('active');
			jQuery(this).parents('.book-service').next().show();
			jQuery('#option-book-wrap').show();
		}
	});
	
	// apply sale item fix
	setSaleNames();
	
	// init carousel
	jQuery('.sales-images-thumbs').jcarousel({
		scroll: 1,
        initCallback: function(carousel){
        	var prev = jQuery('<div class="jcarousel-prev jcarousel-prev-horizontal" style="display: block;"></div>');
        	var next = jQuery('<div class="jcarousel-next jcarousel-next-horizontal" style="display: block;"></div>');
        	var tot = carousel.container.find('li').length;
        	
			carousel.container.append(prev,next);
        	
        	next.bind('click', function() {
		        carousel.next();
        		carousel.clip.find('.active').next().find('a').click();
		        return false;
		    });
		
		    prev.bind('click', function() {
		        carousel.prev();
        		carousel.clip.find('.active').prev().find('a').click();
		        return false;
		    });
        },
        buttonNextHTML: null,
        buttonPrevHTML: null,
        wrap: 'circular'
	});
	
	// init shadowbox 
	Shadowbox.init({
		flashVars: {
			lightcolor: '0x#5593CD'
		},
		overlayOpacity: 0.8
	});
});
