/**
 * @author katewykes
 */
var childArray = new Array(0);
//var $.lozengeCollection;
var hideDivs = new Array(0);
var removeDivs;
var boolComplete = false;

/* lozenge container object*/
function flipBoxContainer(){
	// Class properties.
	this.childArray = childArray;
	this.name = "flipBoxcont";
	this.x = 1;
}

// Method returns a class-bound vesion of the passed-in
// function; this will execute in the context of the
// originating object (this).	
//var arr1 = ['/system/media/images/0000/0464/LAC-10_hmthumb.jpg','/system/media/images/0000/0464/LAC-10_hmthumb.jpg','/system/media/images/0000/0457/LAC-01_hmthumb.jpg','/system/media/images/0000/0458/LAC-03_hmthumb.jpg','/system/media/images/0000/0461/LAC-07_hmthumb.jpg','/system/media/images/0000/0462/LAC-09_hmthumb.jpg','/system/media/images/0000/0463/LAC-11_hmthumb.jpg','/system/media/images/0000/0458/LAC-03_hmthumb.jpg',];
//setTimeout("delay()", 1000);
//var newobject1 = new AddFlipBox("1", arr1, "/work/show/55", "Sportswear Campaign SS09", "Integrated campaign includes press and digital advertising, look book and point of sale material.", "47", "31"); 
function AddFlipBox(flipboxID, imageArray, link, projectName, headline, clientid, categoryid){
	this.name = projectName;
	this.headline = headline;
	this.link = link;
	this.firstimage = imageArray[0];
	this.imagearray = imageArray;
	this.clientid = clientid;
	this.categoryid = categoryid;
	this.ID = flipboxID;
	
	//var t = setTimeout(function(){
		this.htmlelem = AddflipBoxDiv(this);
	//}, 500);
    //$(this).data('timeout', t);
	var objDivJQ = $(this.htmlelem);
	var obj = this;
	

	//push this object into the main array
	this.childArray.push(this);

	//operate these as independent units
	objDivJQ.mouseout(function(){
		var pic = $(this.getElementsByTagName("img")[0]);
		pic.attr("src",imageArray[0]);
		//$(this).children().find("div.flipbut").hide();
	});		
	objDivJQ.mouseover(function(){
		/*show flip button*/
		//$(this).children().find("div.flipbut").show();
		objDivJQ.iskip({images:imageArray, method:'mousemove', 'cycle':1});
	});
	/*this is actioned first - even if flipbut is clicked*/
	objDivJQ.click(function(event){
		//alert(event.target.className);
		var href = $(this).children().find("a.noborder").attr("href");
		if (event.target.className != "flipbutimg" && event.target.className != "whitelink") {
			flipLoz(obj, $(this));
		}else{
			top.location.href = link;
		}
	});


}
AddFlipBox.prototype = new flipBoxContainer;
			

function sImageSide(nCount, sURL, sImage){
//	sContent += "<div class=\"flipbut\"><a class=\"noborder\" href=\"" + sURL + "\"><img class=\"flipbutimg\" src=\"/images/homebox/ibtn.png\"/></a></div>";
	sContent = "";
	sContent = "<div class=\"wrap1 imageside\" id=\"mousemove" + nCount + "\">";
	sContent += "<div class=\"wrap2\"><img class=\"mainimage\" src=\"" + sImage + "\" /></div>";
	sContent += "<div class=\"mask_loz\"><img src=\"/images/new/img_mask.gif\" width=\"188\" height=\"135\" /></div>";
	sContent += "</div>";
	return sContent;
}
function sTextSide(sURL, sName, sHeadline){
//	<div class='flipbut'><a class=\"noborder\" href=\"" + sURL + "\"><img class=\"flipbutimg\" src=\"/images/homebox/ibtn.png\"/></a></div>" 
	sContent = "";
	sContent += "<div class='wrap1'>";
	sContent += "<div class='textdiv'><h4>" + sName + "</h4><p>" + sHeadline + "</p><p><em><a class=\"whitelink\" href=\"" + sURL + "\">View Project</a></em></p></div>"
	sContent += "</div>"
	return sContent;
}
/*
 * Just adds the initial flipbox div to the page
 * via the array population in helper/home_helper.rb
 * @param {Object} objDivJQ
 * @param {Object} sSpeed
 * sImage, nCount, sURL, clientID, categoryID
 * This is the one that adds the flipbox to the innerHTML
 */
function AddflipBoxDiv(flipboxObj){
	var objFlipCont = document.getElementById("flipBox");
	var divContent = "";

	var newDiv = document.createElement("div");
	newDiv.id = "flipBox" + flipboxObj.ID;
	newDiv.className = "flipBox client" + flipboxObj.clientid + "| category" + flipboxObj.categoryid + "|";
	newDiv.style.visibility = 'hidden';
	///add flipbox divs to the page
	sContent = sImageSide(flipboxObj.ID, flipboxObj.link, flipboxObj.imagearray[0]);
	//
	newDiv.innerHTML = sContent;
	//need to do this bit by bit (ie, call ajax add)
	objFlipCont.appendChild(newDiv);

return newDiv;
}

//lozengeCollection - is the Jquery collection for all .flipboxes
function flipallLozenges(loopInteger){
	//lozengeCollection.each(function(i) {
	//	flipLoz(flipBoxCont1.childArray[i], $(this))	
	//});		
	try {
		flipLoz(flipBoxCont1.childArray[loopInteger], $(lozengeCollection[loopInteger]));
		loopInteger += 1;
		//if (loopInteger < 5)
			setTimeout("flipallLozenges(" + loopInteger + ")", 30);
	}
	catch(err)
  	{
		//alert(err);
  	//Handle errors here
  	}
}

//flip function	
function flip() {
	if (flashMovie) {
    	flashMovie.flip();
	}
}

/*Loop through divs and hide as going through*/
function loopShow(loopInteger, pause){
	$("div.flipBox:hidden").show();
	try {
		lozengeCollection[loopInteger].style.visibility = 'visible';
		loopInteger += 1;
		setTimeout("loopShow(" + loopInteger + ")", pause);
	}
	catch(err)
  	{
	//alert(err);
  	//Handle errors here
  	}
}
/*
 * Actually performs the flip
 * @param {Object} flipbutton - jquery object
 * @param {Object} flipboxDiv - jquery object
 * set up objects - 
 * flipboxObj => called from AddFlipBox - class
 * flipboxDiv => div itself - html element
 */
function flipLoz(flipboxObj, flipboxDiv, bText){
	//coming from flipall or the flipbutton
	if (!bText) {
		var wrap1 = flipboxDiv.children() //.parent(); //find wrap1 div
		if ($(wrap1).attr("class") == "wrap1 imageside") {
			sContent = sTextSide(flipboxObj.link, flipboxObj.name, flipboxObj.headline);
			divColour = '#333';
		}
		else {
			sContent = sImageSide(flipboxObj.ID, flipboxObj.link, flipboxObj.firstimage);
			divColour = '#999';
		};
		var test = ($(wrap1).attr("class") == "wrap1 imageside") ? "lr" : "rl";
	}
	else {
		sContent = sTextSide(flipboxObj.link, flipboxObj.name, flipboxObj.headline);
		divColour = '#333';
		var test = "lr";
	}
	//maybe add something in here
	$(flipboxDiv).flip({
		direction: test,
		color: divColour,
		bg: divColour,
		speed: 200,
		content: sContent
	});
	
	$(flipboxDiv).children().find("div.flipbut").show();		
}

/*GENERAL SUPPORTING FUNCTIONS*/
function hideshowcategories(categoryid){
	$("#categorysubnav").hide();
	//
	$("div.flipBox").show();   
	loopHide(0, categoryid, "category");
	//Old Code	
	//$('div.flipBox').hide(500);
	//$('div.category' + categoryid).each(function(i) {
	//	$(this).show(500);
	//});			
}

/*(hide and show (display:none)
////$(this).hide(500);  //previous function call
	//$("div.flipBox:visible").css("visibility", "hidden");  
	//setInterval("highlightit1(theobject1)",50) 
	//$("div.flipBox:visible").fadeOut('slow');
*/
function hideshowclients(clientid){
	/*new work*/
	$("#clientsubnav").hide();
	//
	$("div.flipBox").show();
	loopHide(0, clientid, "client");
	//Old Code
	//setTimeout("loopHide(0, " + clientid + ")", 1000)
	//$('div.client' + clientid).each(function(i) {
	//	$(this).show(500);
	//});		
}

/*Loop through divs and hide as going through*/
/*don't hide divs that satisfy criterea*/
//removeDivs = $("div.flipBox:hidden");
//removeWithDelay(0);
function loopHide(loopInteger, criterea, prefix){
	//try {
		if (lozengeCollection[loopInteger]) {
			if (lozengeCollection[loopInteger].className.indexOf(prefix + criterea + "|") == -1) {
				lozengeCollection[loopInteger].style.visibility = 'hidden';
			}else{
				lozengeCollection[loopInteger].style.visibility = 'visible';				
			}
			loopInteger += 1;
			setTimeout("loopHide(" + loopInteger + "," + criterea + ",'" + prefix + "')", 30);
		}else{
			//At the end of the loop
			$("div.flipBox:hidden").hide(); //this hides (display:none) the divs, so the lozenge moves up
			$("div.flipBox:visible").fadeIn('slow');
		}
	//}
	//catch(err)
  	//{
  		//Handle errors here
  	//}
}



function reset(flipBoxCont1){
	$('div.flipBox').show();	
	//turn to imageside****
	//$('div.flipBox').each(function(i) {
	//	flipLoz(flipBoxCont1.childArray[i], $(this), 1)	
	//});		
}
//****Flip should try to take majority rule

/*
 * AJAX call for textside content
 */
function textside(){
	var sHTML = $.ajax({
      	url: "/images/dialogpopup",
      	data: ({id : $(this).parent().attr("id")}),
		async: false
	}).responseText;
	$('#dialogpopup').html(sHTML);

}
/*
 * 
 * @param {Object} objDivJQ
 * @param {Object} sSpeed
 */
function increaseSpeed(objDivJQ,sSpeed){
	objDivJQ.flip({speed:sSpeed});
}

