/*
Cassis scripts
Copyright 2008 Inpix.ca / @inpix.ca
Last edit: 2008-11-07
*/

/* ########################################################### */

/* vars */
var collection = {};
collection.showing = "0"; /* no. of item showing */
collection.animTimer = 0;

var store = {};
store.cityNum = "null";

var inmedia = {};
inmedia.showing = 0; /* no of page showing */
inmedia.anim = false; /* if anim is going on */

var fullS = {};
var leFlash = {};

var isIe = false;
var isOp = false;
var isSaf = false;

/* variable to know if IE and give simpler animations */
if($.browser.msie) { isIe = true; }
/* variable to know if Opera and give simpler animations */
if($.browser.opera) { isOp = true; }
/* variable to know if Safari */
if($.browser.safari) { isSaf = true; }

/* ########################################################### */
/* update */

/* update size of flash */
function updateFlash()
{
	var bodyH = $("body").height();
   	var footH = $("#footer_inner").height();
   	var newsH = $("#bar_news").height();
   	
   	var bodyW = $("body").width();
   	var headW = $("#header").width();
   	
   	var containerH = $("#container").height();
   	var contentH = $("#content").height();
   	
   	$("#flashcontent").height(bodyH - footH - newsH);
   	
   	/* for pg collection */
   	if(document.getElementById("pg_coll"))
   	{
   		$("#coll").height(bodyH - footH - newsH);
   	}
   	
   	/* for pg with flash full screen */
	if($("body").hasClass("pg_flash"))
	{
	   	$("#flashcontent").width(bodyW - headW);
	   	$("#full").height(bodyH - footH - newsH);
	   	$("#full").width(bodyW - headW);
	}
	
	/* for pg width landing flash */
	if($("body").hasClass("pg_landing"))
	{
		if(isIe === true && $.browser.version < 7)
		{
			$("#container").height(contentH - footH - newsH);
		}
		$("#flashcontent").height(containerH - footH - newsH);
		$("#landing").height($("#flashcontent").height());
	}
	
   	/* stop anim collection menu */
	clearInterval(collection.animTimer);
}

function updateFlashLanding()
{
	$("#flashcontent").height(1);
	setTimeout("updateFlash()", 1);
}

/* ########################################################### */
/* collection */

/* anim for the collection menu */
collection.anim = function()
{
	clearInterval(collection.animTimer);
   	$("#menu_coll ul").slideToggle("slow", updateFlash);
   	
	if($("#menu_coll ul").height() > 5)
	{
		if(isOp !== true) {collection.animTimer = setInterval(collection.updateFlashHeight, 25);}
	}

	return false;
};

collection.updateFlashHeight = function()
{
	var bodyH = $("body").height();
   	var footH = $("#footer_inner").height();
   	var newsH = $("#bar_news").height();
   	
   	$("#flashcontent").height(bodyH - footH - newsH);
   	$("#coll").height(bodyH - footH - newsH);
};

/* anim before the zoom of collection item */
collection.zoom = function()
{
	clearInterval(collection.animTimer);
	if(isIe !== true)
	{
		$("#menu_coll ul").slideUp("slow", updateFlash);
		$("#coll_items").fadeOut();
		if($("#menu_coll ul").height() > 5)
		{
			if(isOp !== true) {collection.animTimer = setInterval(collection.updateFlashHeight, 25);}
		}
	}
	else
	{
		$("#menu_coll ul").slideUp("slow", updateFlash);
		$("#coll_items").hide();
		if($("#menu_coll ul").height() > 5)
		{
			if(isOp !== true) {collection.animTimer = setInterval(collection.updateFlashHeight, 25);}
		}
	}
	$("#flashcontent").addClass("zoom");
	/*$("#flashcontent").width($("body").width() - 340);
	$("#col").width($("#flashcontent").width());*/
};

/* anim on the zoomout of collection item */
collection.zoomOut = function()
{
	clearInterval(collection.animTimer);
	if(isIe !== true)
	{
		$("#menu_coll ul").slideDown("slow", updateFlash);
		$("#coll_items").fadeIn();
		if($("#menu_coll ul").height() > 5)
		{
			if(isOp !== true) {collection.animTimer = setInterval(collection.updateFlashHeight, 25);}
		}
	}
	else
	{
		$("#menu_coll ul").slideDown("slow", updateFlash);
		$("#coll_items").show();
		if($("#menu_coll ul").height() > 5)
		{
			if(isOp !== true) {collection.animTimer = setInterval(collection.updateFlashHeight, 25);}
		}
	}
	$("#flashcontent").removeClass("zoom");
	
	/*$("#flashcontent").remove();
	$('<div id="flashcontent"></div>').insertAfter("#header");
	
	var so = new SWFObject("/flash/collection.swf", "coll", "100%", "100%", "9");
	so.addParam("quality", "best");
	so.addParam("wmode", "transparent");
	so.addVariable("image", "http://content.cassis.ca/galleries/CASSI/collections/images/Photo-buste-20-juin-058.png");
	so.addVariable("imagebig", "http://content.cassis.ca/galleries/CASSI/collections/images/Photo-buste-20-juin-058.png");
	so.addVariable("index", "1");
	so.addVariable("total", "20");
	so.addVariable("zoommode","in");
	so.write("flashcontent");*/
};

collection.update = function(showMe, total, image, imagebig, flashPath)
{
	if(showMe !== collection.showing)
	{
		if($("#flashcontent").hasClass("zoom"))
		{
			collection.zoomOut();
		}
		
		if(collection.showing == "0")
		{
			$("#txt_landing").hide();
			$("#coll_txt").show();
			$("#coll_items").show();
			$("body").addClass("pg_gen");
			$("#flashcontent").removeClass("landing");
			$("#flashcontent").width("310px");
		}
		
		/* thumb */
		$("#menu_coll #" + "thumb" + collection.showing).removeClass("selected");
		$("#menu_coll #" + "thumb" + showMe).addClass("selected");
		
		/* items desc */
		$("#coll_items #coll_items_desc" + collection.showing).hide();
		if(isIe !== true)
		{$("#coll_items #coll_items_desc" + showMe).fadeIn("slow");} 
		else 
		{$("#coll_items #coll_items_desc" + showMe).show();}
		
		/* stylist */
		$("#coll_txt #stylist" + collection.showing).hide();
		if(isIe !== true)
		{$("#coll_txt #stylist" + showMe).fadeIn("slow");}
		else
		{$("#coll_txt #stylist" + showMe).show();}
		
		/* flash */
		$("#flashcontent").removeClass("zoom");
		
		flashToLoad = flashPath + "/flash/collection.swf";
		
		var so = new SWFObject(flashToLoad, "coll", "100%", "100%", "9");
		so.addParam("quality", "best");
		so.addParam("wmode", "transparent");
		so.addParam("allowScriptAccess", "always");
		so.addVariable("image", image);
		so.addVariable("imagebig", imagebig);
		so.addVariable("index", showMe);
		so.addVariable("total", total);
		so.addVariable("zoommode","in");
		so.write("flashcontent");
		
		collection.showing = showMe;
		return false;
	}
	else
	{
		return false;
	}
};


/* update img in flash */
collection.updateThb = function(showMe)
{
	$("#menu_coll #" + "thumb" + showMe + " a").click();
};
/* ########################################################### */
/* store */

/* show the clicked city stores */
store.city = function(city)
{
	if(store.cityNum == "null")
	{
		$("#store_cities #stores>div").hide();
		$("#store_landing").hide();
		$("#flashcontent").removeClass("landing");
		$("#store_cities .menu li:eq(" + city + ")").addClass("selected");
		if(isIe !== true)
		{
			$("#store_cities").fadeIn();
			$("#store_cities #store_city" + city).fadeIn("normal", updateFlashLanding);
		}
		else
		{
			$("#store_cities").show();
			$("#store_cities #store_city" + city).show("normal", updateFlashLanding);
		}
		store.cityNum = city;
	}
	else
	{
		$("#store_cities .menu li:eq(" + store.cityNum + ")").removeClass("selected");
		$("#store_cities #store_city" + store.cityNum).hide();
		
		$("#store_cities .menu li:eq(" + city + ")").addClass("selected");
		if(isIe !== true)
		{$("#store_cities #store_city" + city).fadeIn("normal", updateFlashLanding);}
		else { $("#store_cities #store_city" + city).show("normal", updateFlashLanding);}
		
		store.cityNum = city;
	}
};

/* show and hide the clicked store hours */
store.hours = function(lethis)
{
	if($(lethis).parent().next().css("display") == "none")
	{
		$(lethis).addClass("open");
		$(lethis).parent().next().fadeIn("normal", updateFlashLanding);
	}
	else
	{
		$(lethis).removeClass("open");
		$(lethis).parent().next().fadeOut("normal", updateFlashLanding);
	}
};

/* show and hide terms for gift cards */
function terms(lethis)
{
	if($(lethis).parent().next().css("display") == "none")
	{
		$(lethis).addClass("open");
		$(lethis).parent().next().slideDown("normal", updateFlashLanding);
	}
	else
	{
		$(lethis).removeClass("open");
		$(lethis).parent().next().slideUp("normal", updateFlashLanding);
	}
}


/* ########################################################### */
/* media */

/* number of total pages */
inmedia.total = $("#media_cover ul li").length - 1;

/* show clicked page */
inmedia.show = function(pageNum)
{
	
	if(pageNum !== inmedia.showing && pageNum <= inmedia.total && inmedia.anim === false)
	{
		inmedia.anim = true; /* animation is in progress */
		
		$("#magazine_ls_pages #magazine_ls_page" + inmedia.showing).removeClass("selected");
		$("#magazine_ls_pages #magazine_ls_page" + pageNum).addClass("selected");
		
		$("#media_cover ul li:eq(" + inmedia.showing + ")").hide();
		$("#media_cover ul li:eq(" + pageNum + ")").fadeIn();
		
		$("#page_infos #page_info" + inmedia.showing).hide();
		
		if(isSaf === true && $.browser.version < 500)
		{ $("#page_infos #page_info" + pageNum).css("display","block");}
		
		$("#page_infos #page_info" + pageNum).slideDown("normal", inmedia.animEnd);
		
		inmedia.showing = pageNum;
	}
};

/* show next page */
inmedia.nxt = function()
{
	if(inmedia.showing < inmedia.total)
	{
		var nextValue = parseInt(inmedia.showing + 1);
		inmedia.show(nextValue);
	}
	else
	{
		inmedia.show(0);
	}
};

/* show previous page */
inmedia.prev = function()
{
	if(inmedia.showing > 0)
	{
		var prevValue = parseInt(inmedia.showing - 1);
		inmedia.show(prevValue);
	}
	else
	{
		inmedia.show(inmedia.total);
	}
};

/* hide arrows if only 1 cover */
inmedia.hideArrow = function()
{
	$("#media_cover p.bt_cover_nav").hide();
};

/* tell animation is over */
inmedia.animEnd = function() { inmedia.anim = false; };

/* ########################################################### */
/* ready fonction */
$(document).ready(function(){
   	
   	/* add anim on title bar of menu of collection */
   	$("#menu_coll h3 a").click(collection.anim);
   	
   	/* update flash height if in collection pg */
	if(document.getElementById("pg_coll") || $("body").hasClass("pg_flash"))
	{
		setTimeout("updateFlash()", 1);
		$(window).resize(updateFlash);
	}
	
	/* update flash page with landing flash */
	if($("body").hasClass("pg_landing"))
	{
		setTimeout("updateFlash()", 1);
		$(window).resize(updateFlashLanding);
	}
	
	/* hide and anim menu for page collection */
	if(document.getElementById("pg_coll"))
	{
		$("#menu_coll ul").css("display", "none");
		setTimeout("collection.anim()", 700);
	}
	
	/* hide cities for page store locator */
	if(document.getElementById("store_cities"))
	{ $("#store_cities").css("display", "none");}
	
	/* hide arrows if only 1 cover in the media */
	if(document.getElementById("media_cover") && $("#media_cover ul li").length == 1)
	{ inmedia.hideArrow();}
	
});

/* ########################################################### */
/* fonction called by flash player */
function collZoom() {collection.zoom(); return false;}
function collZoomOut() {collection.zoomOut(); return false;}
function collUpdate(show) {setTimeout("collection.updateThb("+ show +")", 1); return false;}
function getH() {return $("body").height();}
function getH2() {return $("body").height() - $("#bar_news").height() - 147;}
function lookbook() { $(".menu li").removeClass("selected"); $(".menu:eq(0) li:eq(0)").addClass("selected"); }
