// Xmanager Global Javascript
// Copyright 2008


// GreyBox - Popup windows
var GB_ROOT_DIR = "http://www.xmanager.ca/scripts/greybox/";

// Highslide JS popup configuration
//hs.graphicsDir = 'http://www.xmanager.ca/scripts/highslide/graphics/';
//hs.outlineType = 'rounded-white';
//hs.objectLoadTime = 'after';

function redirect(url) {
	window.location = url;
}

function statusWorking() {
	$JQ('#tabWorking').show();	
}

function statusIdle() {
	$JQ('#tabWorking').hide();
}

function makesure(name) {
	if (confirm('Are you sure you want to Remove this? \n\r' + name)) {
		
		return true;
	}
	else {
		return false;
	}
}
	
// body onLoad = reSize()
function reSize() {
	var h = document.getElementById("gallery_height").offsetHeight;
	h += 72;
	self.resizeTo(930,h);
}

// form submit function
function submitForm(formName, action, type) {
	jQuery('#submitType').attr("name", action);
	jQuery('#submitType').attr("value", type);
	
	jQuery('#' + formName).submit();
	
	//document.getElementById("submitType").name = action;
	//document.getElementById("submitType").value = type;
	
	//document.getElementById(formName).submit();
}
	
function show2(){
	var Digital=new Date();
	var n=Digital.getDate();
	var year=Digital.getFullYear();
	var hours=Digital.getHours();
	var minutes=Digital.getMinutes();
	var seconds=Digital.getSeconds();
	var dn="PM";
	
	var s="th";
	if(n===1 || n==21 || n==31) s="st";
	if(n===2 || n==22) s="nd";
	if(n===3 || n==23) s="rd";
	var dayNum = n+s;
	
	var day=new Array(7);
		day[0]="Sunday";
		day[1]="Monday";
		day[2]="Tuesday";
		day[3]="Wednesday";
		day[4]="Thursday";
		day[5]="Friday";
		day[6]="Saturday";
	var dayName = day[Digital.getDay()];
	
	var month=new Array(12);
		month[0]="January";
		month[1]="February";
		month[2]="March";
		month[3]="April";
		month[4]="May";
		month[5]="June";
		month[6]="July";
		month[7]="August";
		month[8]="September";
		month[9]="October";
		month[10]="November";
		month[11]="December";
	var monthName = month[Digital.getMonth()];
	if (hours<12)
		dn="AM";
	if (hours>12)
		hours=hours-12;
	if (hours==0)
		hours=12;
	if (minutes<=9)
		minutes="0"+minutes;
	if (seconds<=9)
		seconds="0"+seconds;
	var ctime=dayName+" "+monthName+" "+dayNum+", "+year+" "+hours+":"+minutes+":"+seconds+" "+dn;
	document.getElementById("tick2").innerHTML= ctime;
	setTimeout("show2()",1000);
}

function ajaxTreeClick(url, id) {	
	if($JQ('#'+id).html().length == 0) {
		$JQ.get(url,
				function(data) {
					$JQ('#'+id).html(data);
				});
	}
	else
		$JQ('#'+id).html('');
}

function preloadImages() {
 	var args = preloadImages.arguments;
	document.imageArray = new Array(args.length);
	for(var i=0; i<args.length; i++) {
		document.imageArray[i] = new Image;
		document.imageArray[i].src = args[i];
	}
}

function RemoveContentLink(contentLinkUUID, title) {
	if (makesure(title)) {
		$JQ.post('scripts/ajxRemoveContentLink.php?contentLinkUUID=' + contentLinkUUID, $JQ('#contentLinkForm').serialize(), 
			function(data){
				UpdateAreaLinks(data);
			});
	}		
}

function RemoveMediaLink(mediaUUID, linkUUID, title) {
	if (makesure(title)) {
		$JQ.post('scripts/ajxRemoveMediaLink.php?mediaUUID=' + mediaUUID + '&linkUUID=' + linkUUID, $JQ('#formCreate').serialize(), 
			function(data){
				UpdateMediaPoolList(data);
			});
	}
}

function StoreContentLink(contentUUID) {
	$JQ.post('scripts/ajxStoreContentLink.php?contentUUID=' + contentUUID, $JQ('#contentLinkForm').serialize(), 
			function(data){
				UpdateAreaLinks(data);
			});
}

function StoreMediaLink(mediaUUID, linkUUID) {
	//var contentUUID = document.getElementById('contentUUID').value;
	
	//if(contentUUID.length > 0)
	//	linkUUID = contentUUID;
	//else if(tempUUID.length > 0)
	//	linkUUID = tempUUID;
		
	$JQ.post('scripts/ajxStoreMediaLink.php?mediaUUID=' + mediaUUID + '&linkUUID=' + linkUUID, 
			function(data){
				UpdateMediaPoolList(data, linkUUID);
			});
}

function UpdateAreaLinks(args) {
	var areaUUID = document.getElementById('areaUUID').value;
	GetAreaLinks(areaUUID);
	document.getElementById('ajxOutput').innerHTML = args;
}

function UpdateMediaPoolList(args, linkUUID) {
	
	try {
		var uuid = $JQ('#contentUUID').val();
		
		if(uuid.length <= 0)
			uuid = linkUUID;
		$JQ.get("scripts/ajxRelatedContentMedia.php?contentUUID=" + uuid,
			function(data) {
				$JQ('#ajxRelatedContentMedia').html(data);
			});
			
		$JQ('#ajxOutput').html(args);
	}
	catch(ex) {
		var uuid = $JQ('#pageUUID').val();
		
		$JQ.get("scripts/ajxRelatedContentMedia.php?contentUUID=" + uuid,
			function(data) {
				$JQ('#ajxRelatedContentMedia').html(data);
			});
			
		$JQ('#ajxOutput').html(args);
	}
}

function GetContentList(uuid) {
	
	$JQ.get("scripts/ajxContentList.php?linkUUID=" + uuid,
		function(data) {
			$JQ('#ajxContentList').html(data);
			
			$JQ('#sortableContentList').sortable({
					opacity:'.7',
					zIndex:'1000',
					containment:'document',
					connectWith: ["#sortableAreaLinks"]
					//update: function(element){
					//	var weights = $JQ('#sortableAreaLinks').sortable("serialize",{expression:'(.+)[_](.+)'});
					//	var areaUUID = $JQ('#areaUUID').val();
					//	$JQ.get("scripts/ajxUpdateContentLinkListWeights.php?areaUUID="+areaUUID+"&"+weights,
					//		function(data) {
					//			GetAreaLinks(areaUUID);
					//		});
					//}
				});
		});	
}

function GetAreaLinks(uuid) {
	//GetContentList(uuid);
	
	$JQ('#areaUUID').val(uuid);
	
	// retrieve existing area links
	$JQ.get("scripts/ajxAreaLinks.php?areaUUID="+uuid,
        function(data){
			$JQ('#ajxAreaLinks').html(data);
			
			$JQ('#sortableAreaLinks').sortable({
					opacity:'.7',
					zIndex:'1000',
					containment:'document',
					connectWith: ["#sortableContentList"],
					update: function(element) {
						var weights = $JQ('#sortableAreaLinks').sortable("serialize",{expression:'(.+)[_](.+)'});
						$JQ.get("scripts/ajxUpdateContentLinkListWeights.php?areaUUID="+uuid+"&"+weights,
							function(data) {
								GetAreaLinks(uuid);
							});
					}
				});
			
			//Sortable.create('sortableAreaLinks', {
			//	overlap: 'vertical',
			//	constraint: false,
			//	containment: false,
			//	onUpdate: function(element) {
			//		var weights = Sortable.serialize(element.id);
			//		$JQ.get("scripts/ajxUpdateContentLinkListWeights.php?areaUUID="+uuid+"&"+weights,
			//			function(data) {
			//				GetAreaLinks(uuid);
			//			});					
			//	}
			//});	
		});
		
	// show list of created content
	$JQ('#contentList').css("display","block"); 
}