//Scripts
$(document).ready(function() {

    //Commons
    $('.colorbox').colorbox({
        opacity: 0.5
    });
	
    //Single functions
	if ($.isPage('dove_siamo.php') || $.isPage('dove_siamo_eng.php')) {
        initializeMap();
	}
	
	slider(1, 'h', '.home-slider', '#wrapper_hg_button', 4000);

})

/*-------------------- FUNCTIONS ---------------------------*/

// @info 	Check current pag
// @usage	if($.isPage('pageName'))
jQuery.isPage = function(checkFor) {
	
    var url = window.location.pathname;
    var page = url.split('/');
    page.reverse();

    if (checkFor == page[0]) {

        return true;
    } else {
        return false;
    }
}

function slider(offset, style, container, container_btn, speed) {

    var offset = offset;
    var $container = $(container);
	var $container_btn = $(container_btn);
    var timer = null;
    var timeout = 7000;

    var $buttons = $('.stop');
    var $next = $container_btn.find('.next').children('a');
    var $prev = $container_btn.find('.prev').children('a');
    var i = 0;
    var pos = 0;
    var activity = true;

    var $slider = $container.find('ul');
    var numbli = $slider.find('li').length;
    var liwidth = $slider.find('li').width();
    var liheight = $slider.find('li').height();
    var lisize = null;
    var ulsize = null;
	var direction = '';

    if (style == 'h'){
		//Set ul size
        lisize = liwidth;
        ulsize = numbli * lisize;
        //Set ul height/width 
        $slider.css({ width: ulsize });
    } else {
		lisize = liheight;
        ulsize = numbli * lisize;
        $slider.css({ height: ulsize });
    }
	
	function animate(margin){
		var options = {};
		if( style == 'h'){
			options = { marginLeft : margin };
		} else {
			options = { marginTop: margin };
		}
		$slider.stop().animate(options, speed);
    }
	

    //Set timer
    setTimeout(function() {

        var effect = function() {
            i++;
            if (i <= numbli - offset) {
                pos = -lisize * i;
				animate( pos );
            } else {
                i = 0;
                pos = 0;
                animate( pos );
            }
            //start Timer
           // timer = setTimeout(effect, timeout);

        };

        //start Timer
        // timer = setTimeout(effect, timeout);

        //If out set Timer
        $buttons.hover(function() {
            //Clear
            clearTimeout(timer);

        }, function() {

            //Set Interval
           // timer = setTimeout(effect, timeout);

        })

        //Animate slider
        $next.click(function() {
            clearTimeout(timer);
            i++;
            if (i <= numbli - offset) {
                pos = -lisize * i;
                animate( pos );
            } else {
				i = 0;
                pos = 0;
				animate( pos );
            }
            return false;

        });

        $prev.click(function() {
            clearTimeout(timer);
            if (0 < i && i <= numbli - offset) {
                i--;
                pos = -lisize * i;
                animate( pos );

            }
            return false;
        });

    })
}


// @info Initialize map
function initializeMap() {
	
	// coordinate Casa Clelia
    var casaclelia = new google.maps.LatLng(45.711418, 9.494583);
	
    var myOptions = {
        zoom: 15,
        center: casaclelia,
        mapTypeId: google.maps.MapTypeId.HYBRID
    }
	
    var map = new google.maps.Map(document.getElementById("mappa"), myOptions);

    var marker = new google.maps.Marker({
        position: casaclelia,
        map: map,
		title: 'Agriturismo Casa Clelia',  
  		clickable: false,
		icon: 'images/icone/gmap_icon.png',
		shadow: 'images/icone/gmap_icon_shadow.png'
    });
	  	
}



