// *************************************************************************
//		 ___________________________________________________________		#
//		|															|		#
//		|	Miscellaneous functions									|		#
//		|___________________________________________________________|		#
//																			#
// *************************************************************************

// Show Debug on AJAX => (0,1)
var ajaxDebug = 0;

// Items in rent list

function getRef(name) { return document.getElementById(name); }

function goPopup(mylink, windowname) {
	if (!window.focus) return true;
	var href;
	if (typeof(mylink) == 'string') href=mylink;
	else href=mylink.href;
	window.open(href, windowname, 'width=800,height=600,toolbar=no,resizable=yes,scrollbars=yes');
	return false;
}

function setStyleAttribs(bits, attribs, ref) {
	// USAGE:
	// var borders = new Array('borderTop','borderLeft','borderRight');
	// setStyleAttribs(borders, '1px #000 solid', 'id123XXX');
	for (var i = 0; i < bits.length; i++) {
		eval("getRef('"+ref+"').style."+bits[i]+" = '"+attribs+"';");
	}
}

// *************************************************************************
//		 ___________________________________________________________		#
//		|															|		#
//		|	Stop multiple form submits								|		#
//		|___________________________________________________________|		#
//																			#
// *************************************************************************

var formSubmitted = false;

function dubble(theForm) {
	if (!formSubmitted) {
		formSubmitted = true;
		return true;
	}
	else {
		alert("Please wait, this form has already been submitted.");
		return false;
	}
}

// *************************************************************************
//		 ___________________________________________________________		#
//		|															|		#
//		|	Swap DIVs												|		#
//		|___________________________________________________________|		#
//																			#
// *************************************************************************

function blocking(nr) {
	if (document.all) {
		current = (document.all[nr].style.display == 'none') ? 'block' : 'none';
		document.all[nr].style.display = current;
	}
	else if (document.getElementById) {
		vista = (document.getElementById(nr).style.display == 'none') ? 'block' : 'none';
		document.getElementById(nr).style.display = vista;
	}
}

function switchDiv(nr, suffix) {
	var otherdiv = nr + suffix;
	blocking(nr);
	blocking(otherdiv);
}

function navRollOver(obj1, obj2, state) {
	document.getElementById(obj1).className = (state == 'on') ? 'nav-highlight' : 'nav';
	document.getElementById(obj2).className = (state == 'on') ? 'nav-highlight' : 'nav';
}

// *************************************************************************
//		 ___________________________________________________________		#
//		|															|		#
//		|	Collapse/Expand Div height with acceleration & fading	|		#
//		|___________________________________________________________|		#
//																			#
// *************************************************************************

var pid = 0, i = 0; moving = 0, speed = 1, d = 0, height = 0;
ie5  = (document.all && document.getElementById);
ns6 = (!document.all && document.getElementById);

function callScroll(obj,length,speedfactor){
	basespeed = 0.025;
	var ratio = 1;
	var currentLength = parseInt(getRef(obj).style.height);
	if (ie5) speedfactor *= 3;
	else speedfactor *= 1.5;
	speed = basespeed*speedfactor;
	getRef(obj).style.display = 'block';
	if (!moving) {
		i = 0;
		if (currentLength != length) {
			if (currentLength == 1 || length > currentLength) {
				if (length > currentLength) {
					ratio = currentLength / length;
					i = ratio*Math.asin(1);
				}
				scrollOut(obj,length);
			} else {
				if (length < currentLength) {
					distance = currentLength - length;
					if (length == 1) fade = 1;
					else fade = 0;
					scrollIn(obj,length,distance,fade);
				}
			}
		}
	} else {
		length = currentLength;
		i = 1.25;
	}
	
	if (ajaxDebug) getRef('currentLength').value = currentLength;
	
}

function scrollIn(obj,length,distance,fade){
	if ( i < Math.asin(1)) {
		moving = 1;
		i=i+(speed*3);
		d = parseInt(Math.cos(i)*distance) + length;
		if (d <= (length)) { d = length; }
		getRef(obj).style.height = d;
		if (fade) {
			if(ie5) getRef(obj).filters.alpha.opacity = parseInt((-i+1.4)*100);
			if(ns6) getRef(obj).style.opacity = -i+1.4;
		}
		pid=setTimeout('scrollIn("'+ obj +'",'+ length +','+ distance +','+ fade +')', 15);
	} else {
		clearTimeout(pid);
		getRef(obj).style.height = length;
		moving = i = d = 0;
		if (fade) getRef(obj).style.display = 'none';
	}
	height = parseInt(getRef(obj).style.height);
	if (ajaxDebug) {
		getRef('height').value = height;
		getRef('d').value = d;
		getRef('i').value = i;
		getRef('moving').value = moving;
	}
}

function scrollOut(obj,length){
	if ( i < Math.asin(1)-speed) {
		moving = 1;
		i=i+(speed*1.5);
		d = Math.sin(i)*length;
		getRef(obj).style.height = d;
		if(ie5) getRef(obj).filters.alpha.opacity = parseInt((i-0.2)*100);
		if(ns6) getRef(obj).style.opacity = i-0.2;
		pid=setTimeout('scrollOut("'+ obj +'",'+ length +')', 15);
	} else {
		clearTimeout(pid);
		d = length;
		getRef(obj).style.height = d;
		moving = 0;
	}
	height = parseInt(getRef(obj).style.height);
	if (ajaxDebug) {
		getRef('height').value = height;
		getRef('d').value = d;
		getRef('i').value = i;
		getRef('moving').value = moving;
	}
}

// *************************************************************************
//		 ___________________________________________________________		#
//		|															|		#
//		|	Some fruity AJAX										|		#
//		|___________________________________________________________|		#
//																			#
// *************************************************************************



// Pre-load images for AJAX add/reserve buttons.
if (document.images) {
   img1 = new Image();
   img2 = new Image();
   img3 = new Image();
   img4 = new Image();
   img5 = new Image();
   img6 = new Image();
   img1.src = "/images/icons/Added.gif";
   img2.src = "/images/icons/Adding.gif";
   img3.src = "/images/icons/Reserving.gif";
   img4.src = "/images/icons/Reserved.gif";
   img5.src = "/images/icons/Removing.gif";
   img6.src = "/images/icons/Removed.gif";
}

function getHTTPObject() {
	var xmlhttp;
	/*@cc_on 
	@if (@_jscript_version >= 5) try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	} @else xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	} return xmlhttp;
}
var http = getHTTPObject();
var ajax_rand;
var ajax_add_option;
var ajax_add_command;
var ajax_perform;

function ajax_addTitle(action,id,ajaxMode,perform) {
	//  ajaxMode = (1 => 'quick', 2 => 'slim detail', 3 => 'full detail', 10 => 'prioritise')
	//  perform = mandatory_options('Load', 'Add', 'Reserve', 'Remove')
	if (!moving) {
		ajax_rand = Math.random();
		ajax_add_command = action;
		ajax_add_option = ajaxMode;
		if (perform == 'reserve') perform = 'Reserv';
		if (perform == 'add') perform = 'Add';
		if (perform == 'remove') perform = 'Remov';
		ajax_perform = perform;
		var loadingArea = 'buttons';
		var loadingText = '<img src=\"/images/icons/'+perform+'ing.gif\" alt=\"'+perform+'ing...\" border=\"0\" width=\"105\" height=\"19\" align=\"right\" />';
		if (ajax_add_option == 10) {
			loadingArea = 'inlineT';
			loadingText = '<span class=\"ajaxloading\" >'+perform+'ing...</span>';
		}
		getRef(loadingArea+id).innerHTML = loadingText;
		http.open('get', '/account/ajax_add_title.html?r='+ajax_rand+'&'+action+'='+id+'&ajax_add_option='+ajax_add_option);
		http.onreadystatechange = ajax_addTitle_response;
		http.send(null);
	}
}
function ajax_addTitle_response() {
	perform = ajax_perform;
	if(http.readyState == 4) {
		results = http.responseText.split("|");
		if (results[0] == ajax_rand) {
			
			if (ajaxDebug) getRef('xmlid').value = results[1];
			
			if (results[2]>0) {
				discs = " "+ results[2] +" discs";
				getRef('rentalListCount').innerHTML = parseInt(getRef('rentalListCount').innerHTML) + parseInt(results[2]);
			} else {
				discs = "";
				getRef('rentalListCount').innerHTML = parseInt(getRef('rentalListCount').innerHTML)+1;
			}
			var ajax_add_menu = "<a href=\"/account/selection.html\">My Selection</a> | <a href=\"#\" onclick=\"ajax_updatePriority("+results[1]+",'product_id'); return false;\" >Similar titles...</a>";
			if (ajax_add_option != 3 && ajax_add_option != 10) ajax_add_menu +=  " | <a href=\"#\" onclick=\"ajax_addTitle('"+ajax_add_command+"',"+results[1]+",10, 'Load'); return false;\">Prioritise delivery</a>";
			if ((ajax_add_option != 10) && (ajax_add_option != 1)) getRef('inlineT'+results[1]).innerHTML = perform+"ed"+ discs +"! <span>" +ajax_add_menu+ "</span>";
			else getRef('inlineT'+results[1]).innerHTML = " <span>" +ajax_add_menu+ "</span>";
			if (ajax_add_option != 10) {
				getRef('buttons'+results[1]).innerHTML = "<img src=\"/images/icons/"+perform+"ed.gif\" alt=\""+perform+"ed\" border=\"0\" width=\"105\" height=\"19\" align=\"right\" />";
				getRef('inlineB'+results[1]).style.background = "#FFF5E1";
				getRef('rowResult'+results[1]).style.border = '2px #FFE7B9 solid';
			}
			if (ajax_add_option == 2) {
				callScroll('activate'+results[1],25,10);
			} else {
				if (ajax_add_option == 3) {
					// Full detail
					getRef('rowThumbImg'+results[1]).style.display = "none";
					getRef('inlineC'+results[1]).innerHTML = results[3];
					callScroll('activate'+results[1],120,10);
				} else {
					if (ajax_add_option == 10) {
						// Title to be prioritised
						getRef('rowThumbImg'+results[1]).style.display = "none";
						getRef('inlineC'+results[1]).innerHTML = results[3];
						getRef('inlineB'+results[1]).style.background = "#FFF5E1";
						callScroll('activate'+results[1],120,10);
					}
				}
			}
		}
	}
}
function ajax_updatePriority(id,action,parent_id) {
	if (!parent_id) parent_id = false;
	ajax_rand = Math.random();
	var priority;
	var multi;
	var url = '/account/ajax_update_title.html?r='+ajax_rand+'&'+action+'='+id;
	if (action == 'update_priority') {
		eval("priority = document.f_"+id+".h_"+id+".value;");
		eval("multi = document.f_"+id+".m_"+id+".value;");
		url += "&priority="+priority+"&series_order="+multi;
	}
	if (parent_id) {
		url += "&parent_id="+parent_id;
		id = parent_id;
	}
	
	if (ajaxDebug) getRef('xmlname').value = url;
	
	var loadingText = 'Loading...';
	if (action == 'remove_from_rental_list') loadingText = 'Removing...';
	getRef('inlineT'+id).innerHTML = "<span class=\"ajaxloading\">"+loadingText+"</span>";
    http.open('get', url);
	http.onreadystatechange = ajax_updatePriority_response;
    http.send(null);
}
function ajax_updatePriority_response() {
	if(http.readyState == 4) {
		results = http.responseText.split("|");
		if (results[0] == ajax_rand) {
			
			if (ajaxDebug) {
				getRef('xmlid').value = results[1];
				getRef('xmlpriority').value = results[2];
			}
			getRef('rowThumbImg'+results[1]).style.display = "none";
			getRef('inlineT'+results[1]).innerHTML = results[3];
			getRef('inlineC'+results[1]).innerHTML = results[4];
			getRef('inlineB'+results[1]).style.background = "#FFF5E1";
			callScroll('activate'+results[1],164,8);
		}
	}
}
function ajax_hideItem(id,action) {
	if (!moving) {
		ajax_rand = Math.random();
		getRef('allbuttons'+id).innerHTML = "<div class=\"ajaxloading\" style=\"float: right\">Hiding...</div>";
		http.open('get', '/account/ajax_hide_item.html?r='+ajax_rand+'&'+action+'='+id);
		http.onreadystatechange = ajax_hideItem_response;
		http.send(null);
	}
}
function ajax_hideItem_response() {
	if(http.readyState == 4) {
		results = http.responseText.split("|");
		if (results[0] == ajax_rand) {
			
			if (ajaxDebug)getRef('xmlid').value = results[1];
			
			getRef('ajaxItemRow'+results[1]).style.display = 'none';
		}
	}
}
function ajax_removeTitle(action,id,ajaxMode,perform) {
	if (!moving) {
		ajax_rand = Math.random();
		if (perform == 'remove') perform = 'Remov';
		ajax_perform = perform;
		var loadingArea = 'buttons';
		var loadingText = '<img src=\"/images/icons/'+perform+'ing.gif\" alt=\"'+perform+'ing...\" border=\"0\" width=\"105\" height=\"19\" align=\"right\" />';
		getRef(loadingArea+id).innerHTML = loadingText;
		http.open('get', '/account/ajax_remove_item.html?r='+ajax_rand+'&'+action+'='+id);
		http.onreadystatechange = ajax_removeTitle_response;
		http.send(null);
	}
}
function ajax_removeTitle_response() {
	perform = ajax_perform;
	if(http.readyState == 4) {
		results = http.responseText.split("|");
		if (results[0] == ajax_rand) {
			
			if (ajaxDebug) getRef('xmlid').value = results[1];
			getRef('rentalListCount').innerHTML = parseInt(getRef('rentalListCount').innerHTML)-1;
			var ajax_add_menu = "<a href=\"/account/selection.html\">My Selection</a> | <a href=\"#\" onclick=\"ajax_updatePriority("+results[1]+",'product_id'); return false;\" >Similar titles..</a>";
			if (ajax_add_option != 1) getRef('inlineT'+results[1]).innerHTML = perform+"ed! <span>" +ajax_add_menu+ "</span>";
			
			else getRef('inlineT'+results[1]).innerHTML = " <span>" +ajax_add_menu+ "</span>";
			getRef('buttons'+results[1]).innerHTML = "<img src=\"/images/icons/"+perform+"ed.gif\" alt=\""+perform+"ed\" border=\"0\" width=\"105\" height=\"19\" align=\"right\" />";
			if (ajax_add_option != 1) {
				callScroll('activate'+results[1],25,10);
			}
		}
	}
}


// *************************************************************************
//		 ___________________________________________________________		#
//		|															|		#
//		|	Image transparency slideshow fader						|		#
//		|___________________________________________________________|		#
//																			#
// *************************************************************************

function fadepic() {
	if (curpos<100) {
		curpos+=10
		if (tempobj.filters) tempobj.filters.alpha.opacity=curpos
		else if (tempobj.style.MozOpacity) tempobj.style.MozOpacity=curpos/101
	} else {
		clearInterval(dropslide)
		nextcanvas=(curcanvas=="canvas0")? "canvas0" : "canvas1"
		tempobj=ie4? eval("document.all."+nextcanvas) : document.getElementById(nextcanvas)
		tempobj.innerHTML=insertimage(nextimageindex)
		nextimageindex=(nextimageindex<fadeimages.length-1)? nextimageindex+1 : 0
		var tempobj2=ie4? eval("document.all."+nextcanvas) : document.getElementById(nextcanvas)
		tempobj2.style.visibility="hidden"
		setTimeout("rotateimage()",pause)
	}
}

function insertimage(i) {
	var tempcontainer=fadeimages[i][1]!=""? '<a href="'+fadeimages[i][1]+'" target="'+fadeimages[i][2]+'">' : ""
	tempcontainer+='<img src="'+fadeimages[i][0]+'" border="0">'
	tempcontainer=fadeimages[i][1]!=""? tempcontainer+'</a>' : tempcontainer
	return tempcontainer
}

function rotateimage() {
	if (ie4||dom) {
		resetit(curcanvas)
		var crossobj=tempobj=ie4? eval("document.all."+curcanvas) : document.getElementById(curcanvas)
		crossobj.style.zIndex++
		tempobj.style.visibility="visible"
		var temp='setInterval("fadepic()",50)'
		dropslide=eval(temp)
		curcanvas=(curcanvas=="canvas0")? "canvas1" : "canvas0"
	} else document.images.defaultslide.src=fadeimages[curimageindex][0]
	curimageindex=(curimageindex<fadeimages.length-1)? curimageindex+1 : 0
}

function resetit(what) {
	curpos=10
	var crossobj=ie4? eval("document.all."+what) : document.getElementById(what)
	if (crossobj.filters) crossobj.filters.alpha.opacity=curpos
	else if (crossobj.style.MozOpacity) crossobj.style.MozOpacity=curpos/101
}

function startit(){
	var crossobj=ie4? eval("document.all."+curcanvas) : document.getElementById(curcanvas)
	crossobj.innerHTML=insertimage(curimageindex)
	rotateimage()
}


