// JavaScript Document
var page_loaded = false;
window.unload = page_unload();
//---------------------------------PORTFOLIO FUNCTIONS-----------------------------------------
imageNum = 1;
imageOldNum = 1;
function new_large(imageNumNew){
	if(imageNumNew == "open") imageNumNew = imageOldNum;
	imageNum = imageNumNew;
	document.getElementById("story").style.display = "none";
	document.getElementById("portfolio_large"+imageOldNum).style.display = "none";
	document.getElementById("portfolio_large"+imageNum).style.display = "block";
	if(imageNum != "open") imageOldNum = imageNum;
}
function open_story(){
	document.getElementById("story").style.display = "block";
	document.getElementById("portfolio_large"+imageNum).style.display = "none";
}
//--------------------------------NEXT/PREVIOUS MONTH------------------------------------------

function new_month(more,divtag) {
	if (page_loaded) {
		if (document.getElementById(divtag)) document.getElementById(divtag).innerHTML = "Loading...";
		ajax_json_call('/filebin/template/portfolio_page.php', 'GET', more);
	}
}


//------------------------------------OPEN EVENT----------------------------------------------

function portfolio_json(category, category_id, project) {
	imageNum = 1;
	imageOldNum = 1;
	var more = "category="+category+"&category_id="+category_id+"&project="+project;
	document.getElementById("portfolio_layer").innerHTML = '<div style="padding:10px 10px 10px 10px;"><img src="/filebin/images/loading.gif" /> Loading...';
	ajax_json_call('/filebin/template/portfolio_page.php', 'GET', more);
	//alert(category);
}
function openPortfolio(category, category_id, project) {
	//objOverlay.style.display = 'none';
	if (document.getElementById("client_menu")) {
		document.getElementById("client_menu").style.display = 'none';
		if (document.getElementById("displayMenu")) {
			var button = document.getElementById("displayMenu");
			button.innerHTML = "Full Client List";
			document.getElementById("client_list").style.zIndex = "4000";
		}
	}
	
	if (document.getElementById('portfolio_layer')) {
		
		try {
		
		//CENTER EVENT DETAIL
	    var intH = 0;
	    var intW = 0;
    
		if (self.innerHeight) {
		   intH = window.innerHeight;
		   intW = window.innerWidth;
		} else {
        	if (document.documentElement && document.documentElement.clientHeight) {
            	intH = document.documentElement.clientHeight;
            	intW = document.documentElement.clientWidth;
        	} else {
            	if (document.body) {
                	intH = document.body.clientHeight;
                	intW = document.body.clientWidth;
            	}
        	}
    	}
    	
    	//GET SCROLL POSITION FOR POP-UP Y-POSITION
    	var spos = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
    	
    	var ypos = parseInt(spos + 111, 10); //CODED TO BE 20px below top edge of screen
    	var xpos = parseInt((intW-20-926) / 2);
		 //HARD CODED TO WIDTH OF EVENT POP-UP: 926px


		portfolio_json(category, category_id, project);
		document.getElementById('portfolio_layer').style.left = xpos+"px";
		document.getElementById('portfolio_layer').style.top = ypos+"px";
		showOverlay();		
		document.getElementById("portfolio_layer").style.display = "block";
			//alert(intW);
		}
		catch(e) {
			alert(e);
		}
	
	}
}	

window.onresize = function(){
	//CENTER EVENT DETAIL
	    var intH = 0;
	    var intW = 0;
    
		if (self.innerHeight) {
		   intH = window.innerHeight;
		   intW = window.innerWidth;
		} else {
        	if (document.documentElement && document.documentElement.clientHeight) {
            	intH = document.documentElement.clientHeight;
            	intW = document.documentElement.clientWidth;
        	} else {
            	if (document.body) {
                	intH = document.body.clientHeight;
                	intW = document.body.clientWidth;
            	}
        	}
    	}
    	
    	//GET SCROLL POSITION FOR POP-UP Y-POSITION
    	var spos = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
    	
    	var ypos = parseInt(spos + 111, 10); //CODED TO BE 20px below top edge of screen
    	var xpos = parseInt((intW-20-926) / 2);
		 //HARD CODED TO WIDTH OF EVENT POP-UP: 926px


		//portfolio_json(category, category_id, project);
		document.getElementById('portfolio_layer').style.left = xpos+"px";
		document.getElementById('portfolio_layer').style.top = ypos+"px";
}

function close_div() {
	 //alert(div);
	//if(!div) div = "event";
	hideOverlay();
	 if(document.getElementById("portfolio_layer").style.display == "block") document.getElementById("portfolio_layer").style.display = "none";
	 //if (div=="event") document.getElementById("portfolio_layer").style.display = "none";
}
function reg_event(id) {
	var name =  "reg_form_"+id;
	document.getElementById(name).submit();	
}
function page_onload() {
	page_loaded = true;	
}
function page_unload() {
	page_loaded = false;	
}
addOnloadEvent(page_onload);

function addOnloadEvent(fnc){
  if ( typeof window.addEventListener != "undefined" ) {
	window.addEventListener( "load", fnc, false );
  }
  else if ( typeof window.attachEvent != "undefined" ) {
    window.attachEvent( "onload", fnc );
  }
  else {
    if ( window.onload != null ) {
      var oldOnload = window.onload;
      window.onload = function ( e ) {
        oldOnload( e );
        window[fnc]();
      };
    }
    else
      window.onload = fnc;
  }
}

//------------------------------------------------JSON-------------------------------------------------------

function ajax_json_call(callUrl, callType, callVars, onCompleteFunc) {
	try {
		$.ajax({
			type: callType,
			url: callUrl,
			data: callVars,
			dataType: "json",
			success: function(data) {
				if (data) {
					process_success_ajax_json_call(data);
				} else {
					//error
				}
		 	},
			complete: function() {
				
				//if we need to run something discard if we get data or not
				if (eval("typeof " + onCompleteFunc + " == 'function'")) {
					eval(onCompleteFunc+'()');
				}
			}
		});
	} catch (e) {
		//nothing for now
	}
}
function process_success_ajax_json_call(data) {
	//var formElements = {formType:['input', 'type', 'radio', 'password', 'textarea', 'checkbox', 'select']}
	//alert("success");
	if (data) {
		var alert_content="";
		$.each(data, function(id, dataHolder) {
			if ($("#"+id) && id!="alert_msg") {
				//dataHolder is array (jAction,contentReturn,insertId)
				if (dataHolder.jAction.indexOf('insert')>-1) {
					//insertAfter, insertBefore
					$(dataHolder.jContentReturn)[dataHolder.jAction]('#'+dataHolder.jHelpId);
				} else {
					//replace/set new value
					$("#"+id)[dataHolder.jAction](dataHolder.jContentReturn);
				}
			}
			//display js alert
			if (id=="alert_msg") alert_content += content+"\n";
		});
		if (alert_content != "") alert(alert_content);
	}
	else {
		//process error here...
	}
}
