var searchLink = "";

$(document).ready(function () {
	initLink();
    initSlider();
    initOverlay();
    initForm();
    initAccordeon();
    initRemoveArrows();
		initScrollPanes();
		initPage();
	initAjax();
	initSearch();
});
function initAjax(){
	
}
function initLink() {
	if(!searchLink){
	  searchLink = $('.btn-search').attr("href");
	}
	$('.btn-search').attr("href", searchLink + '?id=' + $('.products-entry').first().find('input').val());
	
	$('form.search a#btn-search').click(function(){
		$('form.search').submit();
		
		return false;
	});
}
function footerPush (bodyElement) {
    /* Title:       Footer Push v3.01
     * Modified:    October 16th, 2008
     *
     * Description: This function will push page's footer down to the bottom of the browser 
     *              on pages where content does not fill the page.  This prevents the footer 
     *              from 'floating' in the middle of the page.  Requires jQuery 1.2.6
     *              
     *              bodyElement = ID of element you want to extend to full length of the page.
     *
     * Tested on:   Microsoft IE v6.0.2900.5512.xpsp.080413-2111 w/ SP3
     *              Mozilla's Firefox v3.0.3 (Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3)
     *              Apple's Safari Windows v3.1.2 (525.21)
     *              Opera v9.60 Build 10447 (Opera/9.60 (Windows NT 5.1; U; en) Presto/2.1.1)
     *              Google's Chrome v0.2.149.30 (Official Build 2200 Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.2.149.30 Safari/525.13)
     *
     * NOT TESTED:  Microsoft IE 7
     *              Microsoft IE 8 beta 2
     *              Apple's Safari Mac 
     *
     * Notes:       jQuery 1.2.6 identifies Google's Chrome browser as Safari. This footer push currently works for both
     *              Firefox *may* render the footer too short/long -- unable to pinpoint.  When doing a hard refresh (Shift + Refresh) in Firefox, the footer is sometimes pushed down too far.
     *              Opera requires a different method to determine browser/viewport height
     *
     * Resources:   jQuery Browser Detection: http://docs.jquery.com/Utilities/jQuery.browser
     *              Browser detection for Google's Chrome: http://javascriptly.com/2008/09/javascript-to-detect-google-chrome/
     *              Opera/IE unique way of finding viewport size: http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/
     *
     *Todo:         Hide / minimize the footer 'jump' effect
     *              Test on more current IE browsers
     */
    
    if ( $(window).height() > $(document.body).height() ) {
        if ( $.browser.msie ) {             //If Microsoft's IE
            $(bodyElement).height( $(bodyElement).height() + ( $(window).height() - $(document.body).height()) - 0);
        } else if ($.browser.mozilla) {     //If Mozilla's Firefox
            $(bodyElement).height( $(bodyElement).height() + ( $(window).height() - $(document.body).height()) - 17);
        } else if ($.browser.safari) {      //If Apple's Safari *or* Google's Chrome
            $(bodyElement).height( $(bodyElement).height() + ( $(window).height() - $(document.body).height()) - 88);
        } else {                            //Any other browser, default to 'standard' implementation
            $(bodyElement).height( $(bodyElement).height() + ( $(window).height() - $(document.body).height()) - 0);
        };
    };
    if ( $.browser.opera ) {     //If Opera
        if ( window.innerHeight > $(document.body).height() ) {
            $(bodyElement).height( $(bodyElement).height() + ( window.innerHeight - $(document.body).height()) - 0 );
        };
    };
};
function initAccordeon() {
    if ($('#sidebar ul li ul.children').find('li.current-cat')) {
        $('#sidebar ul li ul.children').find('li.current-cat').parents('ul.children').slideDown();
    }
	
    $('#sidebar ul li a').click(function (e) {
        if ($(this).parent('li').find('ul.children').size()) {
			e.preventDefault();
			$(this).parent().addClass("processing");
			$(this).parent('li').find('ul.children').slideToggle();
			$('#sidebar ul:first li:not(.processing) ul.children').slideUp();
			$(this).parent().removeClass("processing");
        }
    });
};

function initSlider() {
    $('.slideshow').cycle({
        fx: 'scrollHorz',
        prev: '#prev',
        next: '#next',
        speed: 700,
        timeout: 0
    });
    $('#products').cycle({
        fx: 'scrollHorz',
        prev: '#prev',
        next: '#next',
        speed: 700,
        timeout: 0,
				after: function(currSlideElement, nextSlideElement, options, forwardFlag){	
					$('.btn-search').attr("href", searchLink + '?id=' + $(nextSlideElement).find('input').val());
					onAfter(currSlideElement,nextSlideElement,options);
				}
    });
		function onAfter(curr,next,opts) {
		 var caption = '<p class="count">' + '<span class="current">' + (opts.currSlide + 1) + '</span>' + ' / ' + opts.slideCount + '</p>';
		 $('#products p.count').remove();
		 $('#products').prepend(caption);
		
		} 
    $('#product-imgs').cycle({
        fx: 'fade',
        speed: 1000,
        timeout: 7000
    });
	
	var baseSearchLink = $("div.catalog-list a:first").attr("href");
	if(baseSearchLink){
	  baseSearchLink = baseSearchLink.replace(/\?id=[\d]+/,'');
	}
	
    $('#product-content').cycle({
        fx: 'fade',
        speed: 1000,
        timeout: 7000,
		after: function(currSlideElement, nextSlideElement, options, forwardFlag){
			var catId = $(nextSlideElement).attr("id").replace("cat-id-","");
			$("div.catalog-list a:first").attr("href",baseSearchLink+"?id="+catId);
		}
    });
}

function initOverlay() {
    $('#myoverlay').click(function () {
        $('#shadow').css({
            'opacity': 0.5
        }).show();
        $('#overlay').fadeIn('fast');
        var windowHeight = $(window).height();
        var overlayHeight = $('#overlay').height();
		var documentTop = $(document).scrollTop(); 
        var topOffset = documentTop + windowHeight - overlayHeight;
        $('#overlay').css({
        	top: topOffset + 20 + "px"
				});
        
            footerPush("#overlay");
        return false;
    });
    $('#overlay a.close-overlay').click(function (e) {
        e.preventDefault();
        $('#overlay').fadeOut('fast', function () {});
        $('#shadow').hide();
        return false;
    });
    $(document).click(function (e) {
        if ($(e.target).parents('#overlay').size() == 0 && !($(e.target).hasClass('overlay'))) {
            $('#overlay').fadeOut('fast');
            $('#shadow').hide();
        }
    });
}

function initForm() {
	var old_name = $('#send-user-files input:file').attr('name');
	$('#send-user-files input:file').attr('name',old_name+'[]');
    $('.wpcf7-form input#name').focus(function () {
        if (($(this).val() == 'Votre nom') || ($(this).val() == 'Your name')) {
			$temp = $(this).val();
            $(this).val('');
        }
    });
    $('.wpcf7-form input#name').blur(function () {
        if ($(this).val() == '') {
            $(this).val($temp);
        }
    });
    $('.wpcf7-form input#theme').focus(function () {
        if (($(this).val() == 'Sujet') || ($(this).val() == 'Subject')) {
					$temp = $(this).val();

            $(this).val('');
        }
    });
    $('.wpcf7-form input#theme').blur(function () {
        if ($(this).val() == '') {
            $(this).val($temp);
        }
    });
    $('.wpcf7-form input#mail').focus(function () {
        if (($(this).val() == 'Votre courriel') || ($(this).val() == 'Email')) {
					$temp = $(this).val();

            $(this).val('');
        }
    });
    $('.wpcf7-form input#mail').blur(function () {
        if ($(this).val() == '') {
            $(this).val($temp);
        }
    });
    $('.wpcf7-form #message').focus(function () {
        if (($(this).val() == 'Objet de votre requête') || ($(this).val() == 'Your message')) {
					$temp = $(this).val();

            $(this).val('');
        }
    });
    $('#search-field').focus(function () {
        if (this.value == this.defaultValue){
			this.value = '';
		}
    });
	$('#search-field').blur(function () {
        if (this.value == ''){
			this.value = this.defaultValue;
		}
    });
    $('.wpcf7-form #message').blur(function () {
        if ($(this).val() == '') {
            $(this).val($temp);
        }
    })
	$('#loginform #user_login').focus(function () {
        if (this.value == 'Username' || this.value == "Nom d'utilisateur"){
			this.value = '';
		}
    });
	$('#loginform #user_pass').focus(function () {
        if (this.value == "Password"){
			this.value = '';
		}
    });
	
}
function initRemoveArrows() {
	var num = $("#products > div.entry").length;
	if (num =="1")
	$('div.slider a#prev,div.slider a#next,div.slider a#prev:hover,div.slider a#next:hover').css({background:"none", cursor:"default" });
}
function initScrollPanes(){
	$('.result-positive').jScrollPane({'scrollbarWidth':9});
}

function initPage(){
	var contentHeight = $("#main-top").height();
	var windowHeight = $(window).height();
	if(contentHeight > windowHeight){
		$("#wrapper").height( $("#main-top").height() );
	}
}

function initSearch(){
	if($("#search-check-all").length!=0){
		$("#search-check-all").click(function(){
			$("#sale-search").submit();
		})
	}
}
