/* --------------------------------------------------------------------------------------------
	Javascript for WRAP special lightbox for 12days of xmas template
	- VictoriaChan 08/12/06
	- victoria.chan@torchbox.com

	Dependent on: YUI/yahoo-min.js, YUI/dom-min.js, YUI/event-min.js, YUI/animation-min.js
-------------------------------------------------------------------------------------------- */

// shortcuts for convenience
var $D = YAHOO.util.Dom;
var $E = YAHOO.util.Event;
var $ = $D.get; // get element by ID

// Vars for this file
var this_cssPath = '/css/front/templates/photogalleryImported.css';// CSS filename, with extension and path
var this_button_link_class = 'imagepopup'; //class name of the <a> tag that opens the popup
var this_popup_element_class = 'expandedphoto'; //class name of the element to popup
var this_buttons_root_id = 'galleryContainer'; //id of the button parents - link parents
var this_popup_element_root_id = 'expandedContainer'; // id of container of expaneded divs
var this_root = 'wrapper';

/*--------------------------------------------------------------------------------------------
	Actions
----------------------------------------------------------------------------------------------*/


//init function
function initLightbox(e){
	//add actions to links
	var arr_pops = $D.getElementsByClassName(this_button_link_class, 'A', this_buttons_root_id);

	//generate id for the answers if none given
	$D.generateId(arr_pops, 'lightbutt_');

	//process each button to add button actions
	for (i=0; i< arr_pops.length; i++){
		$E.addListener($(arr_pops[i].id), 'click', showPop, $(arr_pops[i].id), true);
	}
	//move the expanded div out to root level
	$(this_root).appendChild($(this_popup_element_root_id));

	// for the expanded section add link actions
	var arrayOfpopups = $D.getElementsByClassName(this_popup_element_class);
	for(i=0; i< arrayOfpopups.length;i++) {
		$E.addListener($('next_'+i), 'click', showPop, $('next_'+i), true);
		$E.addListener($('prev_'+i), 'click', showPop, $('prev_'+i), true);
		$E.addListener($('close_'+i), 'click', closePop, $('close_'+i), true);
		$('photo_'+i).id='container_'+i;
	}

	// if this is permalinked then show that image
	if(document.location.hash.length > 1){
		//pop specified in url
		showPop( e, document.location);
	}
}

function showPop(e, obj){
	document.location.href = obj.href;
	//show overlay
	if($('overlay') == null){
		var objOverlay = document.createElement("div");
		objOverlay.id='overlay';

		//$('content').insertBefore(objOverlay, $('primary_content'));
		$(this_popup_element_root_id).appendChild(objOverlay);

		//set position
		$D.setStyle( $('overlay') , 'height' , $D.getDocumentHeight()+'px');
		$D.setStyle( $('overlay') , 'width' , $D.getDocumentWidth()+'px' );

	}else{
		var arr_pops = $D.getElementsByClassName('showpop', 'DIV', this_popup_element_root_id);
		for (i=0; i< arr_pops.length; i++){
			$D.addClass(arr_pops[i].id, 'hidepop');
			$D.removeClass(arr_pops[i].id, 'showpop');
		}
	}

	//show div
	var temparr = obj.href.split('#');
	var theDivId = temparr[1].replace('photo_', 'container_');

	if($(theDivId) != null) {
		var theDayCount = theDivId.replace('container_', '');
		$D.removeClass($(theDivId), 'hidepop');
		$D.addClass($(theDivId), 'showpop');
		document.getElementById(this_popup_element_root_id).style.display = "block";
		window.scrollTo(0,0);
	} else {
		// our popup doesnt exist so remove the overlay
		if($('overlay') != null){
			$(this_popup_element_root_id).removeChild($('overlay'));
			document.getElementById(this_popup_element_root_id).style.display = "none";
		}
	}
	
	return false;
	$E.stopEvent(e); //so that the browser window won't move, equiv of 'return false'
}


function closePop(e, obj){
	var temparr = obj.href.split('#');
	var theDivId = temparr[1].replace('photo_', 'container_');
	$D.addClass($(theDivId), 'hidepop');
	$D.removeClass($(theDivId), 'showpop');
	document.getElementById(this_popup_element_root_id).style.display = "none";
	if($('overlay') != null){
		$(this_popup_element_root_id).removeChild($('overlay'));
	}
	window.scrollTo(0,0);
	document.location.hash = '#';
	//document.location.href = document.location.href.replace('#', '');
	$E.stopEvent(e); //so that the browser window won't move, equiv of 'return false'
}

/*------------------------------------------------------------------
	Load CSS for this JS
-----------------------------------------------------------------*/
function setCSS(css) {
	try {
		// append stylesheet to alter
		document.getElementsByTagName("head")[0].appendChild(css);
	} catch (e) {
		setTimeout(function(){setCSS(css)}, 100);
	}
}
function unsetCSS(css) {
	try {
		// append stylesheet to alter
		document.getElementsByTagName("head")[0].removeChild(css);
	} catch (e) {
		setTimeout(function(){unsetCSS(css)}, 100);
	}
}

function unloadCSS(cssName){
	// attempt to add the css and then keep trying till we do
	unsetCSS($('importedLightBox'));
}

// on load
$E.addListener(window,'load',initLightbox);


// create CSS element to set up the page
var js_css = document.createElement("link");
js_css.setAttribute("href",this_cssPath);
js_css.setAttribute("rel","stylesheet");
js_css.setAttribute("type","text/css");
js_css.id = 'importedLightBox';

// attempt to add the css and then keep trying till we do - IMPORTANT TO DO BEFORE PAGE LOAD!
// http://notes.natbat.net/2006/12/06/trycatchjavascript/
setCSS(js_css);
js_css = null;