// JavaScript Document
$(document).ready(function(){  
	repositionPage();
	
	$(window).bind("resize", function(){
		repositionPage(); 
	});
   
    //initialize slideshow if any
    if ($('#top-left').length != 0) {
        var $length = $("#slideshow img").length;
        var $random = Math.floor(Math.random()*$length) + 1;
        $("#top-left img:nth-child(" + $random + ")").addClass("active");
        setInterval( function() { slideSwitch("top-left"); }, 4000 );
    }
    if ($('#top-right').length != 0) {
        var $length = $("#top-right img").length;
        var $random = Math.floor(Math.random()*$length) + 1;
        $("#top-right img:nth-child(" + $random + ")").addClass("active");
        setInterval( function() { slideSwitch("top-right"); }, 5000 );
    }
	
});

function repositionPage() {
	if ($(window).width() > 1000) {
		var $newDelta = ($(window).width() - 655 - $('#page').width()) / 2;
		var $newLeft = 520 + $newDelta;
		$('#page').css('left', $newLeft);
	}
}

function slideSwitch(id) {
    var $active = $('#' + id + ' img.active');

    if ( $active.length == 0 ) $active = $('#' + id + ' img:last');

    var $next =  $active.next().length ? $active.next()
        : $('#' + id + ' img:first');

    $active.addClass('last-active');

    
	if($.browser.msie){
		$next.addClass('active').show();
		$active.removeClass('active last-active');
	}
	else{
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
			});
	};
}


function switchContent(trigger, newContent, campContent) {
	//manages the presence of the search box
	if (newContent != "content-camps") {
		$('#input_search_box').val("Rechercher");
		$('#search_box_container').fadeOut(200);
	}
	else {
		if ($("#search_box_container").is(':hidden')) {
			$('#search_box_container').fadeIn(200);
		}
	}
	
	//manages the camp button
	var needToReset = false;
	if (trigger == "bt_camps") {
		if ($('#ul-camps').is(':hidden')) {
			switchSelected(trigger);
			$('#ul-camps').slideDown("slow");
			if (campContent == "content-search-holder" ) {
				needToReset = true;
			}
		}
		else {
			switchSelected(trigger);
			resetSearchBox();
			showAll();
		}
		
		$("#content-camps-wrapper div.holder").hide();
	}
	else {
		if ($('#ul-camps').is(':visible')) {
			$('#ul-camps').slideUp("slow", function() {
				switchSelected(trigger);
			});
		}
		else {
			switchSelected(trigger);
		}
	}
	
	if ($("#content-box-middle div.parent").is(':visible')) {
		$("#content-box-middle div.parent:visible").fadeOut(200, function() {
			$("#" + newContent).fadeIn(200, function() {
				if (needToReset == true) {
					showAll();
				}										 
			});
		});	
	}
	
	if (campContent != "") {
		switchCampContent("", campContent, false);
	}
	
	
	return false;
}

function switchCampContent(trigger, newContent, needToReset) {
	if (trigger != "") {
		switchCampSelected(trigger);
	};
		
	if ($("#content-camps-wrapper div.holder").is(':visible')) {
		$("#content-camps-wrapper div.holder:visible").fadeOut(200, function() {
			$("#" + newContent).fadeIn(200, function(){
				if (newContent == "content-search-holder") {
					$('#content-search').jScrollPane({scrollbarWidth:11, dragMaxHeight:30});
					if (needToReset == true) {
						showAll();
					}
				}									 
			});
		});	
	}
	else {
		$("#" + newContent).fadeIn(200, function(){
			if (newContent == "content-search-holder") {
				//$('#content-search').jScrollPane({scrollbarWidth:11, dragMaxHeight:30});
			}									 
		});
	}
	
	if (newContent != "content-search-holder") {
		resetSearchBox("input_search_box");
	}
	
	return false;
}

function switchSelected(trigger) {
	$("#ul-nav:first li a").removeClass("selected"); //removes the current selected item
	if (trigger != "") {
		$("#" + trigger).addClass("selected"); //adds the selected class to the new element
	}
}

function switchCampSelected(trigger) {
	$("#ul-camps:first li a").removeClass("selected"); //removes the current selected item
	$("#" + trigger).addClass("selected"); //adds the selected class to the new element
}

function clearSearchBox(trigger) {
	if ($('#' + trigger).val() == "Rechercher") {
		$('#' + trigger).val("");	
	}
}

function initializeSearchBox(trigger) {
	if ($('#' + trigger).val() == "") {
		$('#' + trigger).val("Rechercher");	
	}
}

function resetSearchBox(trigger) {
	$('#' + trigger).val("Rechercher");	
}


function showAll() {
	$("ul#search-list li").show();
	if ($('#input_search_box').val() != "Rechercher") {
		$('#input_search_box').val("Rechercher");
	}
		$('#content-search').jScrollPane({scrollbarWidth:11, dragMaxHeight:30});

	$("#ul-camps:first li a").removeClass("selected");

	if ($('#content-search-holder').is(":hidden")) {
		switchCampContent("", "content-search-holder", true);
	}
	
	return false;
}

function disableEnterKey(e){
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}


function showNumber(trigger) {
	console.log(trigger);
}

function sendToAFriend() {
	if ($.client.os == "Mac") {
		location.href = "mailto:?subject=Salon%20camps%20de%20vacances%20aux%20Galeries%20de%20Hull%20!&body=La%2014e%20%20%C3%A9dition%20du%20Salon%20camps%20de%20vacances%20aura%20lieu%20les%2012%20et%2013%20mars%20prochains%20aux%20Galeries%20de%20Hull.%0APour%20obtenir%20plus%20d'information%20sur%20les%20exposants%20ainsi%20que%20les%20prix%20%C3%A0%20gagner,%20visitez%20le%20site%20Internet!%20%0Ahttp%3A%2F%2Fwww.saloncampsdevacances.ca";
	}
	else {
		location.href = "mailto:?subject=Salon camps de vacances aux Galeries de Hull !&body=La 14e %E9dition du Salon camps de vacances aura lieu les 12 et 13 mars prochain aux Galeries de Hull.%0APour obtenir plus d'information sur les exposants ainsi que les prix %E0 gagner !%0Ahttp://www.saloncampsdevacances.ca";
	}
	
	return false;
	
}

