var tickerItemCount=0;
var tickerArray=[
"<b>Opening Times: <a href=\"http://www.brentcross.co.uk/customer/customer.asp#open\" class=\"ticker\">Monday - Friday 10am - 8pm </a></b>",
"<b>Opening Times: <a href=\"http://www.brentcross.co.uk/customer/customer.asp#open\" class=\"ticker\">Saturday: 9am - 7pm Sunday: 11am - 5pm</a></b>"
];

var keepPopup=false;


function writeGlossaryWord(w,r,glossaryPath){
	//alert("writeGlossaryWord " + w + " " + r);
	x="<span class=\"glossaryTerm\"";
	x=x + " onmouseover=\"showGlossaryDefinition('over','"+ w +"','"+ r +"','"+glossaryPath+"',event)\"";		
	x=x + " onmouseout=\"showGlossaryDefinition('out','','','',event)\"";	
	x=x + ">";
	x=x + w;
	x=x + "<\/span>";
	document.write(x);
}



function ajaxFunction(w,r){
	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){
			d=document.getElementById("popup");
			
			//d.innerHTML="<p><strong>" + w + "</strong></p>" + ajaxRequest.responseText;
			d.innerHTML=ajaxRequest.responseText;
			//alert(ajaxRequest.responseText);
		}
	}
	ajaxRequest.open("GET", r+"\/?action=ajax&ajax=getGlossaryDefinition&glossaryTerm=" + w, true);
	ajaxRequest.send(null); 
}

function keepPopOpen(){
	keepPopup=true;
}

function showimage(path){
	//alert("<img src='"&path&"'/>");
	v=(document.getElementById("userfile").value);
	t="<img src='"+v+"' width='150' />";
	document.getElementById("localimage").innerHTML=t;
	
}

function winReload(u, GLOBAL_HOST){
	u="http://"+GLOBAL_HOST+u;

	//document.location.href=u;
	//popupEdit.close();
}

function flashTicker(tickerArray){
	document.getElementById("tickerText").innerHTML="<b>Opening Times:</b>";
	setTimeout("changeTicker(tickerArray);",200);
}

function changeTicker(tickerArray){
	c=tickerArray.length;
	t=tickerArray[tickerItemCount];
	document.getElementById("tickerText").innerHTML=t;
	tickerItemCount=tickerItemCount+1;
	if (tickerItemCount>=c){
		tickerItemCount=0;
	}
	setTimeout("flashTicker(tickerArray);",10000);
}

function initMultiContent(var_name) {
	var rowCount=document.getElementById(var_name+"RowCount").value-1;
	for (i=1; i<=rowCount; i++) {
		document.getElementById(var_name + i).style.display = "none";
	}
	document.getElementById(var_name + "New").style.display = "none";
}

function nextMultiContent(var_name) {
	document.getElementById(var_name+"New").style.display = "none";
	var_currentMultiContent = parseInt(document.getElementById("Current"+var_name).value);
	if (var_currentMultiContent == document.getElementById(var_name+"RowCount").value-1) {
		document.getElementById(var_name + var_currentMultiContent).style.display = "none";
		var_currentMultiContent = 0;
		document.getElementById(var_name+"0").style.display = "inline";
	} else {
		document.getElementById(var_name + var_currentMultiContent).style.display = "none";
		var_currentMultiContent = var_currentMultiContent + 1;
		document.getElementById(var_name + var_currentMultiContent).style.display = "inline";
	}
	document.getElementById("Current"+var_name).value=var_currentMultiContent;
	document.getElementById("recordNo").innerHTML = var_currentMultiContent+1;
}

function previousMultiContent(var_name) {
	document.getElementById(var_name+"New").style.display = "none";
	var_currentMultiContent = parseInt(document.getElementById("Current"+var_name).value);
	if (var_currentMultiContent == 0) {
		document.getElementById(var_name+"0").style.display = "none";
		var_currentMultiContent = document.getElementById(var_name+"RowCount").value-1;
		document.getElementById(var_name + var_currentMultiContent).style.display = "inline";
	} else {
		document.getElementById(var_name + var_currentMultiContent).style.display = "none";
		var_currentMultiContent = var_currentMultiContent - 1;
		document.getElementById(var_name + var_currentMultiContent).style.display = "inline";
	}
	document.getElementById("Current"+var_name).value=var_currentMultiContent;
	document.getElementById("recordNo").innerHTML = var_currentMultiContent+1;
}

function ShowNewMultiContent(var_name) {
	var_currentMultiContent = parseInt(document.getElementById("Current"+var_name).value);
	document.getElementById(var_name + var_currentMultiContent).style.display = "none";
	document.getElementById(var_name+"New").style.display = "inline";
}

function hideElement(e){
	if (document.getElementById(e)){
		document.getElementById(e).style.display = "none";
	}
}

function showhide(target){
	if (document.getElementById(target)){
		d=document.getElementById(target).style.display;
		hideElement("mainfieldset");
		hideElement("featuresfieldset");
		hideElement("extrafieldset");
		hideElement("footerfieldset");
		hideElement("headerfieldset");
		if (d!="block"){
			document.getElementById(target).style.display = "block";
		}else{
			document.getElementById(target).style.display = "none";
		}
	}
}

function helptext(overOrOut, help_id){
	if (overOrOut=="over"){
	
		document.getElementById(help_id).style.display = "block";
		document.getElementById(help_id).style.left = event.clientX+document.documentElement.scrollLeft +20;
		document.getElementById(help_id).style.top = event.clientY+document.documentElement.scrollTop-20;
	}else{
		document.getElementById(help_id).style.display = "none";
	}
}


function showGlossaryDefinition(overOrOut,w,r,glossaryPath,evt){
	//get definition from db
	
	d=document.getElementById("popup");
keepPopup=true;
	if (overOrOut=="over"){
	
		d.style.left = (evt.clientX+document.body.parentNode.scrollLeft-5) + "px";
		d.style.top = (evt.clientY+document.body.parentNode.scrollTop-150) + "px";
		
		if (d.style.display!="block"){
		
		d.style.display="block";
		d.style.width="400px";
		
	
		//d.innerHTML="<p><strong>" + overOrOut + "</strong></p>";
		
		ajaxFunction(w,r + glossaryPath);
		}
		
	}
	
	
	if (overOrOut=="out"){
		keepPopup=false;
		var evt = evt || window.event; // event object
	var target = evt.target || window.event.srcElement; // event target
	var targetID = target.getAttribute("id"); // event target id
	
	setTimeout("hidePopop(),100");

		//d.style.display="none";
		//d.innerHTML="";
	}

}

function hidePopop(){
	if (keepPopup==false){
		d=document.getElementById("popup");
		d.style.display="none";
		d.innerHTML="";
	}
}

function toggleEditor(id) {

	var elm = document.getElementById(id);
	var d = elm.style.display;
	if (d == "block") {
		d="none";
	}else{
		d="block";
	}
	elm.style.display=d;
	
	if (tinyMCE.getInstanceById(id) == null){
		alert(tinyMCE);
		tinyMCE.execCommand('mceAddControl', false, id);
//	}else{
//		tinyMCE.execCommand('mceRemoveControl', false, id);
	}
}

//HEADER SUCKERFISH START
sfHover = function() {     
	if (document.getElementById("dropdownnav")){
		var sfEls = document.getElementById("dropdownnav").getElementsByTagName("LI");     
		for (var i=0; i<sfEls.length; i++) {         
			sfEls[i].onmouseover=function() {             
				this.className+=" sfhover";         
			}         
			
			sfEls[i].onmouseout=function() {             
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");         
			}     
		}
	}
} 

//uncomment to enable sucker_fish menu
	if (window.attachEvent) window.attachEvent("onload", sfHover); 

//HEADER SUCKERFISH END

//IMAGE_ROTATOR COMPONENT STARTS
imageRotateArray=["xx"];
imageRotateID="";
imageRotatePath="";
imageRotateSpeed=1;

function image_rotatorInit(id,a,p,s){
	imageRotateArray=a;
	imageRotateID=id;
	imageRotatePath=p;
	imageRotateSpeed=s;
	setTimeout("image_rotate()", 1000);
}

function image_rotate(){
	c=imageRotateArray.length;
	r=Math.round(0.5+Math.random()*(c-0.5));
	document.getElementById(imageRotateID).innerHTML="<img src=\"" + imageRotatePath+imageRotateArray[r-1] + "\" />";
	setTimeout("image_rotate()", (imageRotateSpeed*1000));
}

//HIDDEN RULE
$(document).ready(function(){ $("hr.hiddenrule2") .wrap('<div class="hiddenrule"></div>');});

//IMAGE_ROTATOR COMPONENT ENDS

$(document).ready(
	function()
	{
		$('#drag1').Draggable({snapDistance: 10, frameClass: 'frameClass'});
		$('#drag2').Draggable(
			{
				ghosting:	true,
				opacity:	0.5,
				fx:			300
			}
		);
		$('#drag3').Draggable(
			{
				revert:		true,
				fx:			100
			}
		);
		$('#drag4').Draggable(
			{
				grid:		[50,50]
			}
		);
		$('#drag5').Draggable(
			{
				axis:	'vertically'
			}
		);
		$('#drag6').Draggable(
			{
				axis:	'horizontally'
			}
		);
		$('#drag7').Draggable(
			{
				handle:	'div'
			}
		);
		$('#insideParent').Draggable(
			{
				zIndex: 	1000,
				ghosting:	false,
				opacity: 	0.7,
				containment : 'parent'
			}
		);
		$('#drag8').Draggable(
			{
				/* onDragModifier : function(x,y)
				{
					var centerx = 600;
					var centery = 400;
					var angle = Math.atan((centery-y)/(centerx-x));
					if((centerx-x)>=0)
						angle += Math.PI;
					radius = 160;
					return {
						x: radius * Math.cos(angle) + centerx, 
						y: radius * Math.sin(angle) + centery
					}
				}
				*/
				cursorAt: { top: -5, left: -5 }
			}
		);
	}
);


$(document).ready(
	function()
	{
		
		$('#windowOpen').bind(
			'click',
			function() {
				
				if($('#window').css('display') == 'none') {
					$(this).TransferTo(
						{
							to:'window',
							className:'transferer2', 
							duration: 400,
							complete: function()
							{
								$('#window').show();
							}
						}
					);
				}
				this.blur();
				return false;
			}
		);
		$('#windowClose').bind(
			'click',
			function()
			{
				$('#window').TransferTo(
					{
						to:'windowOpen',
						className:'transferer2', 
						duration: 400
					}
				).hide();
			}
		);
		$('#windowMin').bind(
			'click',
			function()
			{
				$('#windowContent').SlideToggleUp(300);
				$('#windowBottom, #windowBottomContent').animate({height: 10}, 300);
				$('#window').animate({height:40},300).get(0).isMinimized = true;
				$(this).hide();
				$('#windowResize').hide();
				$('#windowMax').show();
			}
		);
		$('#windowMax').bind(
			'click',
			function()
			{
				var windowSize = $.iUtil.getSize(document.getElementById('windowContent'));
				$('#windowContent').SlideToggleUp(300);
				$('#windowBottom, #windowBottomContent').animate({height: windowSize.hb + 13}, 300);
				$('#window').animate({height:windowSize.hb+43}, 300).get(0).isMinimized = false;
				$(this).hide();
				$('#windowMin, #windowResize').show();
			}
		);
		$('#window').Resizable(
			{
				minWidth: 200,
				minHeight: 60,
				maxWidth: 700,
				maxHeight: 400,
				dragHandle: '#windowTop',
				handlers: {
					se: '#windowResize'
				},
				onResize : function(size, position) {
					$('#windowBottom, #windowBottomContent').css('height', size.height-33 + 'px');
					var windowContentEl = $('#windowContent').css('width', size.width - 25 + 'px');
					if (!document.getElementById('window').isMinimized) {
						windowContentEl.css('height', size.height - 48 + 'px');
					}
				}
			}
		);
	}
);