$(document).ready(function() {
//start
var is_touch_device = ('ontouchstart' in document.documentElement)?true:false;

/* Tabs Activiation
================================================== */
    var tabs = $('ul.tabs');

    tabs.each(function(i) {
        //Get all tabs
        var tab = $(this).find('> li > a');
        tab.click(function(e) {

            //Get Location of tab's content
            var contentLocation = $(this).attr('href') + "Tab";

            //Let go if not a hashed one
            if (contentLocation.charAt(0) == "#") {

                e.preventDefault();

                //Make Tab Active
                tab.removeClass('active');
                $(this).addClass('active');

                //Show Tab Content & add active class
                $(contentLocation).show().addClass('active').siblings().hide().removeClass('active');

            }
        });
    }); /* ================================================== */

if ($(".home").length) { // implies *not* zero

/*
$(function(){
	$('#slides').slides({
		preload: true,
		preloadImage: 'media/images/loading.gif',
		generateNextPrev: false,
		generatePagination: true,
		play: 4000,
		pause: 2000,
		hoverPause: true
	});
});
*/

}

    if ($(".verti").length) { // implies *not* zero
/* ZOOM
================================================== */
if (is_touch_device == false) {
        $('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
}

/* Slideshow
================================================== */
        var nb = $('.slides_container').children().size();
        //alert(nb);
        if (nb > 1) {
            $('#view').slides({
                preload: false,
                preloadImage: 'media/images/loading.gif',
                effect: 'slide, fade',
                crossfade: true,
                slideSpeed: 350,
                fadeSpeed: 500,
                generateNextPrev: false,
                generatePagination: false,
                megaPagination: true,
                animationComplete: function(current) {
                    total = $('.slides_control').children().size();
                    var caption = (current) + ' / ' + total;
                    $('#caption').html(caption);
                    //alert(total);
                }
            });
        } else {
            // Hide elements that we don't want
            $('#control, .pagination').css("display", "none");
            $('#view .slides_container').show();

        }

        var price1 = $('.pagination > li:eq(0) img').attr("title");
        var priceholder = $('#info p.price');
        var thumb = $('.pagination').find('> li > a');
/*
thumb.click(function(e) {
var price = $(this).children('img').attr('alt');
priceholder.html(price);
});
*/
        // ON PAGE LOAD ACTIONS
        var total = $('.slides_control').children().size();
        var caption = '1 / ' + total;
        $('#caption').html(caption);
        priceholder.text(price1);
        //alert(price1);

        /* ================================================== */

    }


    $('#sidemenu h3').click(function() {
        var obj = $(this).parent().find('ul');

        if ($(obj).css("display") == 'none') { /* your code here*/
            $(obj).addClass('expanded');
            $(obj).show();
        }
        else { /*  alternate logic   */
            $(obj).removeClass('expanded');
            $(obj).hide();
        }




        return false;
    });


	$('#newsletter_form').submit(function() {
		newsletter_inscription();
		return false;
	});


/* Open _blank target
================================================== */
    $('a[rel=external]').attr('target', '_blank');

/* scrollTo
================================================== */
$("a.goInfolettre").click(function(e){
	var t = $('#infolettre');
	$.scrollTo(t, 100);
	$(t).fadeTo("slow", 0.2).delay(100).fadeTo("slow", 1);
	e.preventDefault();
});
/* FADE IN FADE OUT
	================================================== */
	$(".fademe").hover(function(){
		$(this).stop(true, true).fadeTo("slow", 0.6);
	},function(){
   		$(this).stop(true, true).fadeTo("slow", 1);
	});
/* Empty email value on focus
================================================== */
    $('#email, #emailpop').focus(function() {
        if ($(this).val() == 'Votre courriel') {
            $(this).val('');
        }
    }).blur(function() {
        if ($(this).attr('value') == '') {
            $(this).val('Votre courriel');
        }
    })

/* Fancy Box
================================================== */
    // SHOW VIDEO
    $(".popVideo").fancybox({
        'autoScale': false,
        'width': 640,
        'height': 360,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'type': 'iframe'
    });
    // SHOW INFOLETTRE
	$(".popInfolettre").fancybox({
		'transitionIn'	: 'fade',
		'transitionOut'	: 'fade',
		'speedIn'		: '600'
	});
	//auto popup
	//$(".popVideo").trigger('click');
	
    //end
});


//FACEBOOK
function fbs_click() {
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

//GENERAL FUNCTIONS

function find_city_index(city_id) {
	if (city_id != '') {
		document.location = 'boutiques.php?city=' + city_id + '&search=city';
	}
}


function submit_login() {
    e = window.event;
    if (e.keyCode == 13) {
        document.login.submit();
    }
    return false;
}

function check_day() {
    if (document.getElementById('disponibilite_1').checked == true) {
        document.getElementById('div_day_1').style.display = 'block';
        document.getElementById('div_day_2').style.display = 'block';
    } else {
        document.getElementById('div_day_1').style.display = 'none';
        document.getElementById('div_day_2').style.display = 'none';
        document.getElementById('disponibilite_4').checked = false;
        document.getElementById('disponibilite_7').checked = false;
        document.getElementById('disponibilite_1').checked = false;
    }
}

function check_evening() {
    if (document.getElementById('disponibilite_2').checked == true) {
        document.getElementById('div_evening_1').style.display = 'block';
        document.getElementById('div_evening_2').style.display = 'block';
    } else {
        document.getElementById('div_evening_1').style.display = 'none';
        document.getElementById('div_evening_2').style.display = 'none';
        document.getElementById('disponibilite_5').checked = false;
        document.getElementById('disponibilite_8').checked = false;
    }
}

function check_night() {
    if (document.getElementById('disponibilite_3').checked == true) {
        document.getElementById('div_night_1').style.display = 'block';
        document.getElementById('div_night_2').style.display = 'block';
    } else {
        document.getElementById('div_night_1').style.display = 'none';
        document.getElementById('div_night_2').style.display = 'none';
        document.getElementById('disponibilite_6').checked = false;
        document.getElementById('disponibilite_9').checked = false;
    }
}

function newsletter_inscription() {
    var email = $('#email, #emailpop').val();
    var email_filter = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.) {2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

    if (email == '' || !email_filter.test(email)) {
        $('.email_exists').hide();
        $('.email_success').hide();
        $('.email_error').fadeIn();
    }
    else {
        var data_string = 'email=' + email;
        $.ajax({
            type: 'GET',
            url: 'infolettre_inscription.php',
            data: data_string,

            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
            },

            success: function(msg) {
                //$('#debug').html(msg);
                if (msg != '') {
                    if (msg == 'WRONG_EMAIL') {
                        $('.email_success').hide();
                        $('.email_exists').hide();
                        $('.email_error').fadeIn();
                    }
                    else if (msg == 'EMAIL_EXISTS') {
                        $('.email_success').hide();
                        $('.email_error').hide();
                        $('.email_exists').fadeIn();
                    }
                    else if (msg == 'OK') {
                        $('.email_error').hide();
                        $('.email_exists').hide();
                        $('.email_success').fadeIn();
                    }
                }
            }
        });
    }
}

