/*
4:02 PM 4/5/2010
*/
/*
Copyright © 2010 Eckx Media Group, LLC. All rights reserved.
Eckx Media Group respects the intellectual property of others, and we ask our users to do the same.
*/
/*<script>*/
//replace str: anchors topLeft ie allSel rand blinds closeLayer contentInner contentLayer pageDim rand selects thisIndex
//usage, form[class="curtain-(same, skip-rewrite)"] or a[rel="curtain-(same, skip-rewrite)"]
var curtain = {
	index: -1,
	expandLink: false, // show/hide expand link
	expandView: false, //memorize expand view
	hideSelects: new Array(),
	urls: new Array(), //memorize urls to allow refresh
	init: function(){
		curtain.index = -1;
		var blinds = document.createElement('div');
		blinds.id = 'curtain_blinds';
		document.body.appendChild(blinds);
		curtain.initLinks($$('body').first());
		Event.stopObserving(window, 'load', curtain.init); 
	},
	
	initLinks: function(element){ //set all alinks to use curtain
		//change all anchors related to curtain
		var anchors = element.select('a[rel*="curtain"]');
		for(var i = 0; i < anchors.length; i++){
			var classes = anchors[i].readAttribute('rel').split(' ');
			for(var j = 0; j < classes.length; j++){
				if(classes[j].indexOf('curtain') != -1 ){
					var same = classes[j].indexOf('same') != -1 ? 'true' : 'false';
					var skiRewrite = classes[j].indexOf('skip-rewrite') != -1 ? 'true' : 'false';
				}
			}
			anchors[i].href.replace('%27', '\\%27'); //prevent js error from single quote
			anchors[i].href.replace(document.location.protocol+'//'+document.domain, ''); //creae absolute domain url
			anchors[i].href  = 'javascript:curtain.ajax(\''+anchors[i].href+'\', '+same+', '+skiRewrite+')';
		}
		//set all forms to use curtain
		var forms =  element.select('form[class*="curtain"]');
		for(var i = 0;  i< forms.length; i++){
			var classes = forms[i].readAttribute('class').split(' ');
			for(var j = 0; j < classes.length; j++){
				if(classes[j].indexOf('curtain') != -1 ){
					var same = classes[j].indexOf('same') != -1 ? true : false;
					var skiRewrite = classes[j].indexOf('skip-rewrite') != -1 ? true : false;
				}
			}
			forms[i].onsubmit  = function(){
				curtain.ajax(this.action, same, skiRewrite, this);
				return false
			};
		}
	},
	
	urlToClassStr: function (url) {
		var cr = (window.CR == '/') ? window.CR : window.CR + '/';
		// Remove query string, replace ajax|action, space separated classes
		return url.split('?')[0].replace(cr, '').replace('ajax/', '').replace('action/', '').replace(/\//g, ' ');
	},
	
	load: function(){
		curtain.openBlinds();
		var loadPop = document.createElement('div');
		loadPop.id = 'curtain_load';
		document.body.appendChild(loadPop); 
		
		loadPop = $('curtain_load');
		loadPop.addClassName('curtain_load'); 
		topLeft = getTopLeft(loadPop.getWidth(), loadPop.getHeight());
		loadPop.style.top = topLeft[0]+'%';
		loadPop.style.left = topLeft[1]+'%';
		loadPop.style.zIndex = curtain.index + 1;
		loadPop.innerHTML = '<img src="'+window.CR+'/images/library/loading.gif" />';
	},
	
	content: function(html, classStr, sameLayer, width, height, noPadding){
		
		if(sameLayer == true){ // same layer dont create another layer
			if(!$('curtain_popUp'+curtain.index)){
				alert('curtain error: samelayer is set, but theres no curtain layer yet');	
			}
			document.body.removeChild($('curtain_popUp'+curtain.index));
			curtain.content(html, '', false, width, height, noPadding);
			return;
		}
		
		if($('curtain_load')){ //remove load screen
			document.body.removeChild($('curtain_load')); 
		}
		
		//create layers
		var popUp = document.createElement('div'); //the main floating one
		var closeLayer = document.createElement('div'); //close and expand images on top
		var contentLayer = document.createElement('div'); //container with scroll bar
		var bodyLayer = document.createElement('div'); //holds the html stuff
		popUp.id = 'curtain_popUp'+curtain.index;
		closeLayer.id = 'curtain_closeLayer'+curtain.index;
		contentLayer.id = 'curtain_contentLayer'+curtain.index;
		bodyLayer.id = 'curtain_bodyLayer'+curtain.index;
		
		//append layers
		popUp.appendChild(closeLayer); 
		popUp.appendChild(contentLayer);
		contentLayer.appendChild(bodyLayer);
		document.body.appendChild(popUp);
		
		//get objects since dosnt work on IE
		popUp = $('curtain_popUp'+curtain.index);
		closeLayer = $('curtain_closeLayer'+curtain.index);
		contentLayer = $('curtain_contentLayer'+curtain.index);
		bodyLayer = $('curtain_bodyLayer'+curtain.index);
		popUp.style.visibility = 'hidden'; //make invisble right now while we work with it
		popUp.addClassName('curtain_popUp' + (classStr ? ' ' + classStr : ''));
		closeLayer.addClassName('curtain_close');
		bodyLayer.addClassName('curtain_body');
		
		if (noPadding) {
			contentLayer.addClassName('no-padding');	
		}
		
		//populate layers
		if (curtain.expandLink) {
			closeLayerHtml = '<a href="javascript:curtain.expand()" id="curtain_expand'+curtain.index+'" class="expand" title="Expand / Contract"></a>';
			closeLayerHtml += '<a href="javascript:curtain.close()" class="close" title="Close"></a>';
		}
		else {
			closeLayerHtml = '<a href="javascript:curtain.close()" class="close" title="Close"></a>';
		}
		closeLayer.innerHTML = closeLayerHtml;
		
		bodyLayer.innerHTML = html;
		
		//handle javascript 
		var scripts = bodyLayer.select('script');
		var headTag = document.getElementsByTagName("head")[0];
		for(var i = 0; i < scripts.length; i++){
			var newScript = document.createElement("script");
			newScript.type = 'text/javascript';
			headTag.appendChild(newScript);
			newScript.text = scripts[i].text;
		}
		
		contentLayer.addClassName('curtain_content');
		
		if(curtain.expandView){ //memorize expand view
			curtain.expand();
		}
		else if(parseFloat(width) != width || parseFloat(height) != height){ //w x h not given, auto adjust to fit content
			curtain.autoSize();
		}
		else{ // w x h given
			curtain.resizeContent(width, height);
		}
		
		popUp.style.zIndex = curtain.index + 1;
		popUp.style.visibility = 'visible';
	},
	
	autoSize: function(){ 
		//get content dim
		var popUp = $('curtain_popUp'+curtain.index);
		var contentLayer = $('curtain_contentLayer'+curtain.index);
		var contentInner = contentLayer.firstDescendant();
		var closeLayer = $('curtain_closeLayer'+curtain.index);
		width = contentInner.getWidth();
		height = contentInner.getHeight(); // + 20; //the addition is to disable the scroll bar, dont know what causes it.
		
		maxW = document.viewport.getWidth();
		maxH = document.viewport.getHeight();
		
		if(height >= (maxH - 25)){
			height = maxH - 50;
		}
		if(width >= (maxW - 25)){
			width = maxW - 25;
		}
		
		contentLayer.style.height = height+'px';
		height += closeLayer.getHeight(); //add the height of the close layer
		width += 18; //compensate for overflow scroll bar
		var topLeft = getTopLeft(width, height);
		popUp.style.width = width+'px';
		popUp.style.height = height+'px';
		popUp.style.top = topLeft[0]+'%';
		popUp.style.left = topLeft[1]+'%';
	},
	
	resizeContent: function(width, height){
		var popUp = $('curtain_popUp'+curtain.index);
		var contentLayer = $('curtain_contentLayer'+curtain.index);
		var closeLayer = $('curtain_closeLayer'+curtain.index);
		
		var topLeft = getTopLeft(width, height);
	
		popUp.style.width = width+'px';
		popUp.style.height = height+'px';
		popUp.style.top = topLeft[0]+'%';
		popUp.style.left = topLeft[1]+'%';

		contentLayer.style.height = (height - closeLayer.getHeight())+'px';
	},
	
	close: function(){
		document.body.removeChild($('curtain_popUp'+curtain.index));
		curtain.closeBlinds();
	},
	
	resize: function(){
		var curtain_blinds = $('curtain_blinds');
		var pageDim = getPageDim();
		curtain_blinds.style.width = pageDim[0] + 'px';
		curtain_blinds.style.height = pageDim[1] + 'px';
	},
	
	expand: function(){
		curtain.expandView = true; //memorize expand view
		var width = document.viewport.getWidth() - 25;
		var height = document.viewport.getHeight() - 50;
		curtain.resizeContent(width, height);
		$('curtain_expand'+curtain.index).href = 'javascript:curtain.shrink()';
		
	},
	
	shrink: function(){
		curtain.expandView = false; //memorize expand view
		$('curtain_expand'+curtain.index).href = 'javascript:curtain.expand()';
		curtain.autoSize();
	},
	
	openBlinds: function(){
		if( getIEVerNum() == 6){
			curtain.hideSelects(); // fix the selects showing up
		}
		var curtain_blinds = $('curtain_blinds');
		curtain.index +=2;
		curtain_blinds.style.zIndex = curtain.index;
		curtain_blinds.style.display='block';
		var pageDim = getPageDim();
		curtain_blinds.style.width = pageDim[0] + 'px';
		curtain_blinds.style.height = pageDim[1] + 'px';
		Event.observe(window, 'resize', curtain.resize); 
	},
	
	closeBlinds: function(){
		var curtain_blinds = $('curtain_blinds');
		curtain.index-=2;
		curtain_blinds.style.zIndex = curtain.index;
		if(curtain.index == -1){
			curtain_blinds.style.display='none';
			Event.stopObserving(window, 'resize', curtain.resize); 
		}
		
		if( getIEVerNum() == 6){
			curtain.showSelects(); // fix the selects showing up
		}
	},
	
	hideSelects: function(){
		curtain.hideSelects[curtain.index] = new Array(); //hold new selects that will hide
		if(curtain.index != -1){ //get previous curtain selects
			var allSel = $('curtain_popUp'+curtain.index).select('select');
		}
		else{ //get all body selects
			var allSel = $$('select');
		}
		for(var i=0; i < allSel.length; i++){
			if(allSel[i].style.visibility != 'hidden'){
				allSel[i].style.visibility = 'hidden';
				curtain.hideSelects[curtain.index][i] = allSel[i];
			}
		}
	},
	
	showSelects: function(){
		var selects = curtain.hideSelects[curtain.index];
		for(var i=0; i < selects.length; i++){
			selects[i].style.visibility = 'visible';
		}
	},
	
	ajax: function(url, sameLayer, skipRewrite, form, post){
		if(!sameLayer){
			curtain.load(); // has to be before using curtain.index
		}
		
		curtain.urls[curtain.index] = url; //save the urls for refresh, have to before adding ajax to it
		
		if(!skipRewrite){
			//add ajax to url
			var urlParts = url.split(window.CR);
			url = urlParts[0]+ window.CR + '/ajax' + urlParts[1];
	
			var rand = Math.round(100000*Math.random()); //to refresh content
			if(url.include('?')){
				url += '&k='+rand;
			}
			else{
				url += '?k='+rand;
			}
		}
		//alert(url);
		var callBackSuccess =  function(ajaxReturn) {
			if(ajaxReturn.responseText == 'died'){ //in case something died in ajax
				window.location = window.CR+'/action/died';
				return;
			}
			var classStr = curtain.urlToClassStr(url);
			if(!sameLayer){
				curtain.content(ajaxReturn.responseText, classStr);
			}
			else{
				curtain.content(ajaxReturn.responseText, classStr, true);
			}
			var newCurtain = $('curtain_contentLayer'+curtain.index);
			//if(customTitle){ //initialize custom title if exist
			//	customTitle.init('curtain_contentLayer'+curtain.index);
			//}
			curtain.initLinks(newCurtain); //curtain reference
			externalLinks(newCurtain); //external reference
			if(valForm){
				var forms = $(newCurtain).select('form[class*="val-form"]');
				if(forms.length > 0){
					valForm.init(forms[0].id);
				}
			}
		};
		
		var callBackFailed = function(){
			curtain.content('');
			alert2('Request failed, please try again.');
			curtain.close();
		};
		
		if(form){
			form.action = url; //need to reset form aciton so it has ajax in it
			form.request({onSuccess: callBackSuccess, onFailure: callBackFailed}); 
		}
		else{
			var method = post ? 'post' : 'get';
			var postBody = post ? post : '';
			new Ajax.Request(url, { method: method, postBody: postBody, onSuccess: callBackSuccess, onFailure: callBackFailed}); 
		}
		
	},
	refresh: function(prev){
		if(prev){
			var thisIndex = curtain.index - 2;
		}
		else{
			var thisIndex = curtain.index;
		}
		curtain.ajax(curtain.urls[thisIndex], true);	
	}

};

Event.observe(window, 'load', curtain.init); 
