//Spry.Data.Region.debug = true;
// ---------------------------------------------------------//
// RESET THE URL HASH
// ---------------------------------------------------------//

function hashString(key)
{
	var value = null;
	if(hashStringArr[key])
	{
		value=hashStringArr[key];
	}
	return value;
}

hashStringArr = new Array();

function hashString_Parse()
{
	var query = window.location.hash.substring(1);
	var pairs = query.split("&");
	
	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			hashStringArr[argname]=value;
		}
	}

}

/* Get the current query string */
hashString_Parse(); // Start the query string gathering
var urlCat = hashString("cat"); // Get's the value of Category.
var urlSec = hashString("sec"); // Get's the value of Section.
var urlMea = hashString("mea"); // Get's the value of Media.


/* Set and keep the root url */
var topURL = window.location.href; // get the current url
if(topURL.indexOf("#") > -1){ // check to see if there is a ? with data
	var topURL = topURL.substring(0, topURL.indexOf("#")) //reset the url to the page url only
}

/* check to see if url is direct */
if(urlCat != null && urlSec != null && urlMea != null){	
	var newUrl = true; // set the flag to true
}else{
	var newUrl = false;	 // set the flag to false
}




// ---------------------------------------------------------//
// BEHAVIOR HANDLER
// ---------------------------------------------------------//

var gBehaviorsArray = []; // set the behavior handler for all effects

/* checks and sets the behavior ON/OFF (Run/Cancel) */
function CancelBehavior(id){
  if (gBehaviorsArray[id]){
    gBehaviorsArray[id].cancel();
    gBehaviorsArray[id] = null;
  }
}


// ---------------------------------------------------------//
// IMAGE PRELOADER
// ---------------------------------------------------------//
/*
function preloadImages(arr, callback)
{

	if(typeof arr == "string"){
		arr = [arr];	
	}
		   if (!preloadImages.cached)
	{
		preloadImages.cached = {};
		preloadImages.queue = [];
		preloadImages.loader = new Image;
	}

	if (preloadImages.queue.length >= 0)
	{
		// The loader is already running, just append the images
		// to our queue and return.
		preloadImages.queue = preloadImages.queue.concat(arr);
		//return;
	}
	// Run through the queue and only fire off an image load
	// for images that aren't already in our cache.

	preloadImages.queue = arr;
	while(preloadImages.queue.length)
	{
		var src = preloadImages.queue[0];
		
		if (!preloadImages.cached[src])
		{
			// We found an image that isn't in our cache. Set up
			// an onload handler for our image loader, and then
			// fire off a load for the first image in our queue.
			//alert("got here");
			//preloadImages.loader.onload = function()
			//{
				if (callback){
					callback(preloadImages.queue[0]);
				}
				preloadImages.loader.src = null;
				
				//alert("length : " + preloadImages.queue.length);
				while(preloadImages.queue.length)
				{
					var path = preloadImages.queue[0];

					if (!preloadImages.cached[path])
					{
						//alert("loading :" );
						preloadImages.loader.src = path;
						//alert("loading :" + preloadImages.loader.src);
						return;
					}
					preloadImages.queue.shift();
					preloadImages.cached[preloadImages.queue.shift()] = true;
				}
				preloadImages.loader.onload = null;
			//};
			preloadImages.loader.src = src;
			break;
		}
		preloadImages.queue.shift();
	}
}
*/
function preloadImages(arr, callback)
{
	if(typeof arr == "string"){
		arr = [arr];	
	}
	   if (!preloadImages.cached)
	{
		preloadImages.cached = {};
		preloadImages.queue = [];
		preloadImages.loader = new Image;
	}

	if (preloadImages.queue.length > 0)
	{
		// The loader is already running, just append the images
		// to our queue and return.
		preloadImages.queue = preloadImages.queue.concat(arr);
		return;
	}

	// Run through the queue and only fire off an image load
	// for images that aren't already in our cache.

	preloadImages.queue = arr;
	while(preloadImages.queue.length)
	{
		var src = preloadImages.queue[0];
		if (!preloadImages.cached[src])
		{
			// We found an image that isn't in our cache. Set up
			// an onload handler for our image loader, and then
			// fire off a load for the first image in our queue.

			preloadImages.loader.onload = function()
			{
				if (callback)
					callback(preloadImages.queue[0]);

				//preloadImages.loader.src = null;
				preloadImages.cached[preloadImages.queue.shift()] = true;
				while(preloadImages.queue.length)
				{
					var path = preloadImages.queue[0];
					if (!preloadImages.cached[path])
					{
						preloadImages.loader.src = path;
						return;
					}
					preloadImages.queue.shift();
				}
				//preloadImages.loader.onload = null;
			};
			//preloadImages.loader.src = src;
			break;
		}
		preloadImages.queue.shift();
	}
}



// ---------------------------------------------------------//
// BANNER REFRESHER (728X90) //
// ---------------------------------------------------------//

var bannerCheck = 0;  // set the initial variable for counting
function bannerRefresh(){
	if(bannerCheck != 0){
		var ban = document.getElementById('ad728x90');
		ban.src = ban.src;		
	}
	bannerCheck++;	
}


// ---------------------------------------------------------//
// SET RATINGS //
// ---------------------------------------------------------//

function setRatings(){
	 rated=0;
	 preSet = '';
	 var rateData = dsMedia.getCurrentRow(); // get the current media information
	 var rating = rateData['@rating']; // set the rating of the clip
	 var totalRated = rateData['@totalRated'];
	 var rate=''; // set the default rate
	 for(i=0; i<rating; i++){ // setup the current rating display
		 rate += "<span class='rateImageOn'><\/span>";
	 }	 
	 document.getElementById('descRating').innerHTML=rate; // write current rating to the page
	 document.getElementById('actRating').innerHTML=rate; // write the current rating to the page
}


// ---------------------------------------------------------//
// SET PERMALINK INFORMATION //
// ---------------------------------------------------------//
var permalinking;
function setPermalink(){
	
	var curCatRow = dsCategory.getCurrentRow()['@id']; // get the current category information
	if(dsSection.getCurrentRow()){
		var curSecRow = dsSection.getCurrentRow()['@id']; // get the current section information
	}else{
		var curSecRow = '';
	}
	var curMeaRow = dsMedia.getCurrentRow()['@id'] ; // get the current row information

	permalinking = "cat="+curCatRow+"&sec="+curSecRow+"&mea="+curMeaRow; // setup the permalink
	document.permalinkage.permalinkMe.value = topURL+"#"+permalinking; // put the permalink in the field
	document.send2friendSection.url.value = topURL+"#"+permalinking; // put the permalink in the field	
	
}

function setHash(loc){
	document.location.hash= loc; // set the new url
	var newTitle = dsMedia.getCurrentRow(); //get the current media title
	var newSite = dsShow.getCurrentRow(); //get the current media title
	document.title = "Photos: "+newSite['@site']+": "+newTitle['description']; // reset the HTML title bar	
	hashString_Parse(); // reset the parse...		
}


// ---------------------------------------------------------//
// SEND TO FRIEND SECTION //
// ---------------------------------------------------------//
function send2Friend(){
	
	var email = escape(document.send2friendSection.email.value); // get the senders email
	var friendemail = escape(document.send2friendSection.friendemail.value); // get the friends email
	var subject = escape(document.send2friendSection.subject.value); // get the subject
	var message = escape(document.send2friendSection.message.value); // get the message
	var url = escape(document.send2friendSection.url.value); // get the url
	var mediaName = escape(document.send2friendSection.mediaName.value); // get the media name
	var mediaDesc = escape(document.send2friendSection.mediaDesc.value); // get the media name
	
	if(validateFriend() == true){// see if the form is valid
	Spry.Utils.loadURL("GET", "/app/form_mail/send2friend.scet.php?email="+email+"&friendemail="+friendemail+"&subject="+subject+"&message="+message+"&url="+url+"&mediaName="+mediaName+"&mediaDesc="+mediaDesc, false, confirmIt); //send to the friend
	}
	
}

function confirmIt(req){ // confirm the request information
	var text = req.xhRequest.responseText; // get the response text
	alert('This video has been sent to your friend!');	// display the alert
	document.send2friendSection.friendemail.value = ''; // reset the friends email
}

function validateFriend(){ // validation
	if (!validate_email(document.send2friendSection.email.value))
	{
		alert("Please enter a valid e-mail address for yourself.");
		document.send2friendSection.email.focus();
		return false;
	}
	
	if (!validate_email(document.send2friendSection.friendemail.value))
	{
		alert("Please enter a valid friend's e-mail address.");
		document.send2friendSection.friendemail.focus();
		return false;
	}
	
	return true;
}

function validate_email(value)	{
		
	var good = /^([\w\d\.\-_]+)@([\w\d\.\-]+)$/i;
	var evil =  /[^a-z0-9\@_\-\.]+/i;
		
	if (good.test(value) && !evil.test(value)) {
		return true;
	} else {
		return false;
	}
}


// ---------------------------------------------------------//
// COMMENT POSTING 
// ---------------------------------------------------------//

function postMyComment(){

	var err='';
	var cg = document.comment_form.cg.value; // get the senders email
	var type = document.comment_form.type.value; // get the friends email
	var order = document.comment_form.order.value; // get the subject
	var max_per_page = document.comment_form.max_per_page.value; // get the message
	var name = document.comment_form.name.value; // get the url
	var comment = document.comment_form.comment.value; // get the media name
	
	if(validateComment() == true){// validte the form
		Spry.Utils.loadURL("GET", "/app/comments/post_ajax.php?cg="+cg+"&type="+type+"&order="+order+"&max_per_page="+max_per_page+"&name="+name+"&comment="+comment, false, function(req){ // go get the comments
		window.setTimeout("loadComments()", 1000);		 
		 
		 }); //send to the friend
	}	
		
}





