//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function togglePriceChart(chart, anchor) {
	var theChart = document.getElementById(chart);
	var theAnchor = document.getElementById(anchor);
	
	if(theChart.style.display == 'none') {
		new Effect.Appear(theChart);
		theAnchor.innerHTML = '<img src="/img/magnifyingglass.jpg" /> Hide Details';
	} else {
		new Effect.Fade(theChart);
		theAnchor.innerHTML = '<img src="/img/magnifyingglass.jpg" /> See Details';
	}
}

function toggleFadeLink(el) {
	var chart = document.getElementById(el);
	if(chart.style.display == 'none') {
		new Effect.Appear(el);
	} else {
		new Effect.Fade(el);
	}
}

function toggleFadeInput(chkbox, el) {
	var chart = document.getElementById(el);
	var chk = document.getElementById(chkbox);
	if(chk.checked == true) {
		new Effect.Appear(el);
	} else {
		new Effect.Fade(el);
	}
}

function checkFade(chkbox, el) {
	var chk = document.getElementById(chkbox);
	var chart = document.getElementById(el);
	if(chk.checked == true) {
		new Effect.Appear(el);
	} else {
		new Effect.Fade(el);
	}
}

function toggleProjectDetails(id, image) {
	var theDiv = document.getElementById('projectDiv_' + id);
	var theDetails = document.getElementById('projectDetails_' + id);
	var theDetailsImage = document.getElementById('projectDetailsImage_' + id);
	var theImage = document.getElementById('projectImage_' + id);

	if(theDetails.style.display == '') {
		new Effect.Fade(theDetails);
		theDetailsImage.src = '/img/newproj_seedetails.gif';
		theDiv.style.backgroundImage = '';
		theImage.src = '/img/themeTypes/newproj_' + image + '.jpg';
	} else {
		new Effect.Appear(theDetails);
		theDetailsImage.src = '/img/newproj_hidedetails.gif';
		theDiv.style.backgroundImage = 'url(/img/newproj_active.jpg)';
		theImage.src = '/img/themeTypes/newproj_' + image + '_over.jpg';
	}
}

function highlightProject(id, image) {
	var curDiv = document.getElementById('showcaseDiv_' + id);
	var curDivInner = document.getElementById('showcaseDetails_' + id);
	var curDivImage = document.getElementById('showcaseImage_' + id);
	//var curDivImgSrc = curDivImage.src;
	if(curDivInner.style.display == '') {
		new Effect.Fade('showcaseDetails_' + id, {afterFinish: function() {curDiv.className = 'showcase_Normal'; curDivImage.src = '/img/' + image + '.jpg'; curDiv.style.backgroundImage = '';}});
		//curDivInner.style.display = 'none';
	} else {
		curDiv.className = 'showcase_Active';
		curDiv.style.backgroundImage = 'url(/img/newproj_active.jpg)';
		//curDivInner.style.display = '';
		new Effect.Appear('showcaseDetails_' + id);
		curDivImage.src = '/img/' + image + '_over.jpg';
	}
}


/******************** START COMMON JS *****************/

function changeAction(actionid) {
	alert('You selected ' + actionid);
	/*
	if(actionid == 'New') {
	document.getElementById('navBar_New').src = '/img/navbar_new_over.jpg';
	} else {
	document.getElementById('navBar_New').src = '/img/navbar_new.jpg';
	}
	if(actionid == 'Projects') {
	document.getElementById('navBar_Projects').src = '/img/navbar_projects_over.jpg';
	} else {
	document.getElementById('navBar_Projects').src = '/img/navbar_projects.jpg';
	}
	if(actionid == '/img') {
	document.getElementById('navBar_/img').src = '/img/navbar_/img_over.jpg';
	} else {
	document.getElementById('navBar_/img').src = '/img/navbar_/img.jpg';
	}
	if(actionid == 'Orders') {
	document.getElementById('navBar_Orders').src = '/img/navbar_orders_over.jpg';
	} else {
	document.getElementById('navBar_Orders').src = '/img/navbar_orders.jpg';
	}*/
}


/******************** END COMMON JS *****************/

/******************** START PROJECTS JS *****************/

// Get all using Prototype
function projectsGetAll(sort, afterfinishid) {
	if(sort == '' || sort == undefined || sort == null) {
		sort = document.getElementById('Hidden_sortBy').value;
	}
	
	new Ajax.Request('/projects/all/' + sort + '/' + new Date().getTime(), {
		method: 'get',
		onSuccess: function(transport) {
			document.getElementById('MainBox_Content_Left').innerHTML = transport.responseText;
			
			// Checks afterfinishid to see whether or not it should select a project after its done loading all of them
			if(afterfinishid != '') {
				projectSelect(afterfinishid);
			}
		}
	});
}

// This sorts the projects by setting a hidden input to the desired value - Runs via onclick function
function projectSort(sortid) {
	var obj = document.getElementById('Hidden_sortBy');
	if(sortid == 'lastSaved') {
		if(obj.value == 'saved_a') {
			obj.value = 'saved_b';
			projectsGetAll('saved_b');
		} else {
			obj.value = 'saved_a';
			projectsGetAll('saved_a');
		}
		document.getElementById('MainBox_HeaderLink_ProjectName').className = 'MainBox_HeaderLink';
		document.getElementById('MainBox_HeaderLink_LastSaved').className = 'MainBox_HeaderLink_Selected';
	} else {
		if(obj.value == 'name_a') {
			obj.value = 'name_b';
			projectsGetAll('name_b');
		} else {
			obj.value = 'name_a';
			projectsGetAll('name_a');
		}
		document.getElementById('MainBox_HeaderLink_ProjectName').className = 'MainBox_HeaderLink_Selected';
		document.getElementById('MainBox_HeaderLink_LastSaved').className = 'MainBox_HeaderLink';
	}
}

function projectHighlight(el) {
	document.getElementById(el).className = 'projectBlock_Container_Selected';
}

function projectUnhighlight(el) {
	document.getElementById(el).className = 'projectBlock_Container';
}

// Deselect all projects - This loops through each div that has selected class and unselects
function projectDeselectAll() {
	var rowsArray = getElementsByClassName('projectBlock_Container_Selected', 'div');

	for(i = 0; i < rowsArray.length; i++) {
		projectUnhighlight(rowsArray[i].id);
	}
}

// Select a project
function projectSelect(id) {
	projectDeselectAll();

	projectHighlight('projectBlock_' + id);

	new Ajax.Request('/projects/detail/' + id, {
		method: 'get',
		onSuccess: function(transport) {
			new Effect.Fade(document.getElementById('MainBox_CRight_Details'), {
				duration:0.1,
				queue: 'front', afterFinish: function(obj) {
					document.getElementById('MainBox_CRight_Details').innerHTML = transport.responseText;
					new Effect.Appear(document.getElementById('MainBox_CRight_Details'));
				}
			}
			);
		}
	}
	);
}

/********* END SELECT ********/

/********* START RENAME ********/

function projectRename() {
	if(document.getElementById('common_Duplicate_Table').style.display == '') {
		new Effect.Fade(document.getElementById('common_Duplicate_Table'), {queue: 'front'});
	} else {
		new Effect.Fade(document.getElementById('common_Detail_Header'), {queue: 'front'});
	}
	new Effect.Appear(document.getElementById('common_Rename_Table'), {queue: 'end'});
}

function projectRenameAJAX(id){
	var ajaxRequest;  // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			projectsGetAll(document.getElementById('Hidden_sortBy').value);
			document.getElementById('common_Detail_Header').innerHTML = document.getElementById('common_Rename_Input').value;
			new Effect.Fade(document.getElementById('common_Rename_Table'), {queue: 'front'});
			new Effect.Appear(document.getElementById('common_Detail_Header'), {queue: 'end'});
			//common_Detail_Header

			//projectSelect(id);
		}
	}
	var params = "name=" + encodeURI(document.getElementById('common_Rename_Input').value);
	ajaxRequest.open("POST", "/projects/rename/" + id, true);
	ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	ajaxRequest.setRequestHeader("Content-length", params.length);
	ajaxRequest.setRequestHeader("Connection", "close");
	ajaxRequest.send(params);
}

/********* END RENAME ********/

/********* START DUPLICATE ********/

function projectDuplicate() {
	if(document.getElementById('common_Rename_Table').style.display == '') {
		new Effect.Fade(document.getElementById('common_Rename_Table'), {queue: 'front'});
	} else {
		new Effect.Fade(document.getElementById('common_Detail_Header'), {queue: 'front'});
	}
	new Effect.Appear(document.getElementById('common_Duplicate_Table'), {queue: 'end'});

}

function projectDuplicateAJAX(id) {
	var ajaxRequest;  // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			projectsGetAll('', id);
			//projectSelect(id);
		}
	}
	ajaxRequest.open("GET", "/projects/duplicate/" + id + "/" + encodeURI(document.getElementById('common_Duplicate_Input').value), true);
	ajaxRequest.send(null);
}

/********* END DUPLICATE ********/

/********* START DELETE ********/

function projectDelete(id) {
	var answer = confirm("Are you sure you want to delete this project?");
	if(answer) {
		var ajaxRequest;  // The variable that makes Ajax possible!

		try{
			// Opera 8.0+, Firefox, Safari
			ajaxRequest = new XMLHttpRequest();
		} catch (e){
			// Internet Explorer Browsers
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
					// Something went wrong
					alert("Your browser broke!");
					return false;
				}
			}
		}
		// Create a function that will receive data sent from the server
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				//selectProjectAJAX('');
				new Effect.Fade(document.getElementById('MainBox_CRight_Details'));
				projectsGetAll(document.getElementById('Hidden_sortBy').value);
			}
		}
		ajaxRequest.open("GET", "/projects/delete/" + id, true);
		ajaxRequest.send(null);
	}
}

/********* END DELETE ********/


/******************** END PROJECTS JS *****************/



/************************ START IMAGES AREA ***********************/

function imageSort(sortid) {
	var obj = document.getElementById('Hidden_sortBy').value;
	if(sortid == 'importDate') {
		if(obj == 'date_a') {
			obj = 'date_b';
			imagesGetAll('date_b');
		} else {
			obj = 'date_a';
			imagesGetAll('date_a');
		}
		document.getElementById('MainBox_HeaderLink_ImageName').className = 'MainBox_HeaderLink';
		document.getElementById('MainBox_HeaderLink_ImportDate').className = 'MainBox_HeaderLink_Selected';
	} else {
		if(obj == 'name_a') {
			obj = 'name_b';
			imagesGetAll('name_b');
		} else {
			obj = 'name_a';
			imagesGetAll('name_a');
		}
		document.getElementById('MainBox_HeaderLink_ImageName').className = 'MainBox_HeaderLink_Selected';
		document.getElementById('MainBox_HeaderLink_ImportDate').className = 'MainBox_HeaderLink';
	}
}


function imageSelect(projectid, rowid) {
	for(i = 0;i <= document.getElementById('maxRows').value - 1;i++) {
		if(rowid == i) {
			document.getElementById('imageBlock_' + i).className = 'imageBlock_Container_Selected';
		} else {
			document.getElementById('imageBlock_' + i).className = 'imageBlock_Container';
		}
	}
	if(document.getElementById('MainBox_CRight_Details_Outer').style.display != 'block') {
		document.getElementById('MainBox_CRight_Details_Outer').style.display = 'block';
	}
	imageSelectAJAX(projectid, '');
}

function imageSelectAJAX(projectid, fade) {
	var ajaxRequest;  // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			result = ajaxRequest.responseText;
			new Effect.Fade(document.getElementById('MainBox_CRight_Details'), {queue: 'front', afterFinish: function(obj) {document.getElementById('MainBox_CRight_Details').innerHTML = result; new Effect.Appear(document.getElementById('MainBox_CRight_Details')); initLightbox();} });

		}
	}
	ajaxRequest.open("GET", "/images/detail/" + projectid, true);
	ajaxRequest.send(null);
}

function imageRename() {
	if(document.getElementById('common_Duplicate_Table').style.display == '') {
		new Effect.Fade(document.getElementById('common_Duplicate_Table'), {queue: 'front'});
	} else {
		new Effect.Fade(document.getElementById('common_Detail_Header'), {queue: 'front'});
	}
	new Effect.Appear(document.getElementById('common_Rename_Table'), {queue: 'end'});
}

function imageRenameAJAX(id){
	var ajaxRequest;  // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			imageSelect(id, 'rename');
			imagesGetAll(document.getElementById('Hidden_sortBy').value);
		}
	}
	var params = "title=" + encodeURI(document.getElementById('common_Rename_Input').value);
	ajaxRequest.open("POST", "/images/rename/" + id, true);
	ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	ajaxRequest.setRequestHeader("Content-length", params.length);
	ajaxRequest.setRequestHeader("Connection", "close");
	ajaxRequest.send(params);
}

function imageDuplicate() {
	if(document.getElementById('common_Rename_Table').style.display == '') {
		new Effect.Fade(document.getElementById('common_Rename_Table'), {queue: 'front'});
	} else {
		new Effect.Fade(document.getElementById('common_Detail_Header'), {queue: 'front'});
	}
	new Effect.Appear(document.getElementById('common_Duplicate_Table'), {queue: 'end'});

}

function imageDuplicateAJAX(id) {
	var ajaxRequest;  // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			imageSelect(id, 'duplicate');
			imagesGetAll(document.getElementById('Hidden_sortBy').value);
		}
	}
	var params = "title=" + encodeURI(document.getElementById('common_Duplicate_Input').value);
	ajaxRequest.open("POST", "/images/duplicate/" + id, true);
	ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	ajaxRequest.setRequestHeader("Content-length", params.length);
	ajaxRequest.setRequestHeader("Connection", "close");
	ajaxRequest.send(params);
}

function imagesGetAll(sort) {
	var ajaxRequest;  // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			result = ajaxRequest.responseText;
			document.getElementById('MainBox_CLeft_Images').innerHTML = result;
		}

	}
	ajaxRequest.open("GET", "/images/all/", true);
	ajaxRequest.send(null);
}

function imageDelete(projectid) {
	var answer = confirm("Are you sure you want to delete this image?");
	if(answer) {
		var ajaxRequest;  // The variable that makes Ajax possible!

		try{
			// Opera 8.0+, Firefox, Safari
			ajaxRequest = new XMLHttpRequest();
		} catch (e){
			// Internet Explorer Browsers
			try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e){
					// Something went wrong
					alert("Your browser broke!");
					return false;
				}
			}
		}
		// Create a function that will receive data sent from the server
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				new Effect.Fade(document.getElementById('MainBox_CRight_Details'));
				//setTimeout(document.getElementById('MainBox_CRight_Details_Outer').style.display = 'none', 3000);
				imagesGetAll(document.getElementById('Hidden_sortBy').value);
			}
		}
		ajaxRequest.open("GET", "/images/delete/" + projectid, true);
		ajaxRequest.send(null);
	}
}

function imagesGetAllDelayed() {
	setTimeout(imagesGetAll(''), 1000);
}

function imagesGetUpload() {
	var ajaxRequest;  // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			result = ajaxRequest.responseText;
			document.getElementById('async_Iframe_Container').innerHTML = result;
		}
	}
	ajaxRequest.open("GET", "/images/upload", true);
	ajaxRequest.send(null);
}

function upload(){
	var par = window.parent.document;

	//imagesGetAll('name_a');

	// hide old iframe
	var iframes = par.getElementsByTagName('iframe');
	var iframe = iframes[iframes.length - 1];
	iframe.className = 'hidden';

	// create new iframe
	var new_iframe = par.createElement('iframe');
	new_iframe.src = '/images/upload';
	new_iframe.frameBorder = '0';
	par.getElementById('async_Iframe_Container').appendChild(new_iframe);

	setTimeout("document.iform.submit()",5000);
}

//you need to sync the value when user select a new file path;
function syncFilePath(){
	var pathInit = this.value;
	var pathValue = pathInit.split('\\');
	var pathCount = pathValue.length - 1;
	this.form.elements.namedItem('my-input-file-path').value = pathValue[pathCount];
}

function disableFilePath(){
	this.elements.namedItem('my-input-file-path').disabled = true ;
}

function render_start(){
	//var aBtn = document.getElementsByName('image');
	//var dBtn = document.getElementById('realPath');
	var aBtn = document.getElementById('filePath');
	var dBtn = document.iform.image.value;
	dBtn.addEventListener('change', syncFilePath, false);
	render_start = null;
}

// Utility function for getting elements by class name
function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

