﻿//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  Reno.LoadLast Begin
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//================================================================
//  $Reno.DocumentReady - varialbe implementation of 
//  JQuery function. for use in conditions that should 
//  not fire until the document DOM is loaded, like Suggest
//````````````````````````````````````````````````````````````````
//  Example:
//  if ($Reno.DocumentReady)
//  {
//      //-- do something that requirs the DOM to be ready
//  }
//================================================================
$(document).ready(function() 
{
    $Reno.DocumentReady = true;
    
    $("input[type='text']").each(function(){
        new textUI(this)
    });
    
});



$(function(){
	$.ajaxSetup({
//		error:function(x,e)
//		    {
//			    if(x.status==0)
//			    {
//			        //  alert('You are offline!!\n Please Check Your Network.');
//			        //  This error can occur when a call is made for reporting just before the page changes
//			        //  to the href link. Disabled.
//			        //return false;
//			    }
//			else if(x.status==404){
//			alert('Requested URL not found.');
//			}else if(x.status==500){
//			alert('Internal Server Error.');
//			}else if(e=='parsererror'){
//			alert('Error.\nParsing JSON Request failed.');
//			}else if(e=='timeout'){
//			alert('Request Time out.');
//			}else {
//			alert('Unknown Error.\n'+x.responseText);
//			}
//		}
	});
});
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  Reno.LoadLast End
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	Text Input UI
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function textUI(input){
    
    //console.log("apples");
    
    var s = this;
    this.input = $(input);
    this.orignalValue = this.input.val();
    
    this.input.focus(function(){
        s.input.currentValue = this.value;
        if(s.input.currentValue == s.orignalValue){
            s.input.val("");
        }
    });
    
    this.input.blur(function(){
        if(s.input.val()==""){
            s.input.val(s.orignalValue);
        }
    });

}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	Image Proxy Server Manipulation
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

var IPXCurrent = new Array();		// contains keys being managed
var IPXCollection = new Array();	// contains objects by key
var IPXCount = new Array();         // don't let recursive error occur

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function ImgProxyReloader(IPXObjectId, ele)
{
    var idImg = ele.id;
	var TargetImgPath = ele.src;
	    if (IPXCount[idImg] == null)
	    {
	       IPXCount[idImg] = 0; 
	    }
	    IPXCount[idImg]++; // counter to prevent onerror call from running too many times. Shoud never trigger more than twice.
	    
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//	New Logic, calls the image directly through the local proxy
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	var newPath = Static + "/Reno/Widgets/Video/photoProxy.aspx";
	var ProxyPath = "http://imagescdn.gabriels.net/reno/imagereader.aspx";
	
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//	change onerror event
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	if (IPXObjectId != "IPX_ListView")
//	{
//		eval("IPXTemp = new " + IPXObjectId + "();");
//		$("#"+idImg).attr("onError", "this.src='" + IPXTemp.FailureImg + "'");
//	}
	if (IPXCount[idImg] > 10) // don't allow to run more than this many times
	{
		eval("IPXTemp = new " + IPXObjectId + "();");
		$("#"+idImg).attr("src", IPXTemp.FailureImg); // loads failure image
		return; // don't allow code below to rewrite src
	}
	
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	//	replace root path -- 
	//  this src tag replacement triggers a load event
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	var TimeSpan = 1000; // call back after 1 second
	if (IPXCount[idImg] == 1)
	{
	    TimeSpan = 1; // call back immediately if first call
	}
	window.setTimeout(function()
    {
        var src = TargetImgPath;
        if ((IPXCount[idImg] % 2) > 0)
        {
            src = src.toLowerCase().replace(newPath, ProxyPath);
        }
        else
        {
            src = src.toLowerCase().replace(ProxyPath, newPath);
        }
	    $("#"+idImg).attr("src", src); // <-- event
    }, TimeSpan);

	
	/* 
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	Old logic
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	var idImg = ele.id;
	var TargetImgPath = ele.src;
	
	if (IPXCurrent[idImg] == null) 
	{
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		// first try after error was thrown
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		IPXCurrent[idImg] = true;	// flag indicates reload is taking place
		eval("IPXCollection[idImg] = new " + IPXObjectId + "();");
	}
	
	if (IPXCurrent[idImg] == true)
	{
		ImgProxyReloader_Utility(IPXCollection[idImg], idImg, TargetImgPath);
	}
	*/
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function ImgProxyReloader_Utility(IPXObject, idImg, TargetImgPath)
{
	//alert(TargetImgPath);
	IPXCurrent[idImg] = false;								// set flag to false to prevent stack overload which occurs when same mehod is called rapidly
	$('#'+idImg).attr("src", IPXObject.ReloadingImg);
	
	window.setTimeout(function()							// timeout required to allow rendering engine to catch up
	{
		IPXObject.Retry();
		if (IPXObject.Retries <= 0)
		{
			IPXCurrent[idImg] = null;						// reset to null for possibility to try again, example results with mini-paging of images if failure
			$('#'+idImg).attr("src", IPXObject.FailureImg); // process ends with failure img
		}
		else
		{
			window.setTimeout(function()
			{
				IPXCurrent[idImg] = true;					// flag sert to true, allowing reentry
				$('#'+idImg).attr("src", TargetImgPath);	// trying to load image again, may call onerror
			},
			IPXObject.Pause);								// length to pause between tries from object
		}
	},100);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function IPX_DetailsMain()
{
	this.Retries = 5;
	this.Pause = 1250;
	this.ReloadingImg = Static + "/Reno/GlobalResources/Images/ImageProxy/reloading_378_278.gif";
	this.FailureImg = Static + "/Reno/GlobalResources/Images/NoPhoto_378x278.jpg";
	IPX_DetailsMain.prototype.Retry = function()
	{
		this.Retries = this.Retries - 1;
	};
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function IPX_DetailCarousel()
{
	this.Retries = 5;
	this.Pause = 1250;
	this.ReloadingImg = Static + "/Reno/GlobalResources/Images/ImageProxy/reloading_67_50.gif";
	this.FailureImg = Static + "/Reno/GlobalResources/Images/NoPhoto_150x118.jpg";
	IPX_DetailCarousel.prototype.Retry = function()
	{
		this.Retries = this.Retries - 1;
	};
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function IPX_MiniResult()
{
	this.Retries = 5;
	this.Pause = 1250;
	this.ReloadingImg = Static + "/Reno/GlobalResources/Images/ImageProxy/reloading_79_60.gif";
	this.FailureImg = Static + "/Reno/GlobalResources/Images/NoPhoto_150x118.jpg";
	IPX_MiniResult.prototype.Retry = function()
	{
		this.Retries = this.Retries - 1;
	};
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function IPX_ListView()
{
	this.Retries = 3;
	this.Pause = 3000;
	this.ReloadingImg = Static + "/Reno/GlobalResources/Images/ImageProxy/reloading_150_118.gif";
	this.FailureImg = Static + "/Reno/GlobalResources/Images/NoPhoto_150x118.jpg";
	IPX_ListView.prototype.Retry = function()
	{
		this.Retries = this.Retries - 1;
	};
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function IPX_ListViewAgentLogo()
{
	this.Retries = 3;
	this.Pause = 3000;
	this.ReloadingImg = Static + "/Reno/GlobalResources/Images/ImageProxy/reloading_150_118.gif";
	this.FailureImg = Static + "/Reno/GlobalResources/Images/NoPhoto_150x118.jpg";
	IPX_ListView.prototype.Retry = function()
	{
		this.Retries = this.Retries - 1;
	};
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function IPX_ListViewAgentPhoto()
{
	this.Retries = 3;
	this.Pause = 3000;
	this.ReloadingImg = Static + "/Reno/GlobalResources/Images/ImageProxy/reloading_150_118.gif";
	this.FailureImg = Static + "/Reno/GlobalResources/Images/noagent.jpg";
	IPX_ListView.prototype.Retry = function()
	{
		this.Retries = this.Retries - 1;
	};
}
