///-----------------------------------------------------------------------------------------------
/// Name: ricePhoto
/// Version: 1.0 
/// Copyright: 2011 Richard Newbold
/// Information: http://www.richnewbold.co.uk/blog/8/ricephoto-jquery-prettyphoto-lightbox-clone
/// Licence: http://creativecommons.org/licenses/by/3.0/
///-----------------------------------------------------------------------------------------------
    function getRootURL() {
        var baseURL = location.href;
        var rootURL = baseURL.substring(0, baseURL.indexOf('/', 7));
        if (baseURL.indexOf('localhost') == -1) {
            return rootURL + "/";
        } else {
            return rootURL + baseURL.substring(baseURL.indexOf('/', 8), baseURL.indexOf('/', baseURL.indexOf('/', 8)+1)) + "/";        
        }
    }
	/// some holding values
	if (ricePhotoInterval == undefined) {
		var ricePhotoInterval = false;
		var ricePhotoAssets = new Array();
		var ricePhotoTrans = false;
		var ricePhotoSetup = false;
        var root = getRootURL();
		var ricePhotoPath = root + '/wp-content/themes/quest_1.2/scripts/'; /// set this value so ricePhoto can find the button images
	}
	
	jQuery(document).ready(function() {  
		if (!ricePhotoSetup) { 
			jQuery(window).resize(function() { ricePhotoPageResize(); });
			jQuery(window).scroll(function() { ricePhotoPageResize(); });
			ricePhotoInit(); /// comment out this line if you don't want to automatically setup galleries
		}		
	});
	
	/// find links to convert to ricePhoto links
	function ricePhotoInit() {
		ricePhotoSetup = true;
		jQuery("a[rel^='ricePhoto']").each(function(i) {
			ricePhotoAssets[i] = new Array(jQuery(this).attr('href'),jQuery(this).attr('title'),jQuery(this).attr('rel'));
			jQuery(this).click(function(e) { e.preventDefault(); ricePhotoList(i); });
		});		
	}

	/// create a list of images to display in galleries
	function ricePhotoList(i) {
		if (ricePhotoTrans){ return; }
		var p = n = -1;
		if (ricePhotoAssets[i] != undefined) { 
			for(x=0;x<ricePhotoAssets.length;x++) {
				if ((x < i) && (ricePhotoAssets[x][2] == ricePhotoAssets[i][2])) { p = x; }
				if ((x > i) && (ricePhotoAssets[x][2] == ricePhotoAssets[i][2]) && (n < 0)) { n = x; }
			}
			ricePhotoOpener(ricePhotoAssets[i][0],ricePhotoAssets[i][1],p,n);
		}
	}


	/// open a new modal window ( c = image or iframe, d = description, p and n are automatically generated by ricePhotoInit() )
	function ricePhotoOpener(c,d,p,n) {
		
		/// do not continue if opening in progress
		if (ricePhotoTrans){ return; }
		ricePhotoTrans = true;

		/// set some defaults
		var viewWidth = jQuery(window).width();
		var viewHeight = jQuery(window).height();

		/// create the mask
		if (jQuery('#ricePhotoMask').length < 1) { 
			jQuery("body").css("overflow","hidden");
			jQuery("html").css("overflow","hidden");
			jQuery('body').append('<div id="ricePhotoMask" style="display:none; background:#000 url(\''+ricePhotoPath+'ricePhotoMaskLoading.gif\'); background-position:center center; background-attachment:fixed; background-repeat:no-repeat; position:absolute; top:0px; left:0px; width:'+(jQuery(document).width())+'px; height:'+(jQuery(document).height())+'px; z-index:9000;"></div>'); 
			jQuery('#ricePhotoMask').click(function() { ricePhotoClose() });
		}

		/// create the viewer
		if (jQuery('#ricePhotoFrameOuter').length < 1) { 
			jQuery('body').append('<div id="ricePhotoFrameOuter" style="width:200px; background:transparent; display:none; float:left; display:none; position:absolute; top:0px; left:0px; z-index:9001;"><div id="ricePhotoFrameInner" style="padding:10px; background:#000; -moz-border-radius:15px; border-radius:15px;"><div id="ricePhotoFrame" style="padding:10px; background:#fff; -moz-border-radius:8px; border-radius:8px; overflow:hidden;"><div id="ricePhotoContent" style="display:none; background:#eee url(\''+ricePhotoPath+'ricePhotoLoading.gif\'); background-position:center center; background-repeat:no-repeat;font-size:9pt; overflow:hidden;"><span style="display:block; height:100px; color:#999; font-family:arial,verdana;">Loading...</span></div><div id="ricePhotoDescription" style="display:none;"></div></div></div></div>');
		}

		/// fade in the viewer
		jQuery('#ricePhotoMask').fadeTo('fast',0.8,function() {
			if (jQuery('#ricePhotoLinkPrevious').length > 0) { jQuery('#ricePhotoLinkPrevious').remove(); }
			if (jQuery('#ricePhotoLinkNext').length > 0) { jQuery('#ricePhotoLinkNext').remove(); }
			jQuery('#ricePhotoDescription').fadeOut('fast',function(){
				jQuery('#ricePhotoDescription').html('<a href="#close" id="ricePhotoCloseButton"><img src="'+ricePhotoPath+'ricePhotoClose.gif" alt="close" title="Close" style="border:0px;float:right;vertical-align:bottom;margin:5px 0px 0px 10px;" /></a>');
				jQuery('#ricePhotoDescription').append('<span style="display:block; height:20px; overflow:hidden; padding:8px 0px 0px; background:#fff; font-family:arial,verdana; line-height:150%; color:#333; font-size:9pt; text-align:left;">'+d+'</span><div style="height:1px;overflow:hidden;clear:both"></div>');
				jQuery('#ricePhotoDescription').fadeIn('fast');
				jQuery('#ricePhotoCloseButton').click(function(event){ event.preventDefault(); ricePhotoClose(); });
			});
			if (jQuery('#ricePhotoHolder').length > 0) { jQuery('#ricePhotoHolder').fadeOut('fast',function() { jQuery('#ricePhotoHolder').remove(); }); }
			if (jQuery('#ricePhotoOverlay').length > 0) { jQuery('#ricePhotoOverlay').remove(); }


			/// display an iframe
 			if (c.match(/\biframe=true\b/i)) { 
				var tmpWidth = parseInt(ricePhotoParams('width',c));
				var tmpHeight = parseInt(ricePhotoParams('height',c));
				if ((tmpWidth < 1) || (isNaN(tmpWidth))) { tmpWidth = viewWidth; }
				if ((tmpHeight < 1) || (isNaN(tmpHeight))) { tmpHeight = viewHeight; }
				var newFrameHeight = tmpHeight + 70;
				var newFrameWidth = tmpWidth+40;
				if ((viewWidth - 20) < newFrameWidth) { tmpWidth = viewWidth - 80; newFrameWidth = tmpWidth+40; }
				if ((viewHeight - 40) < newFrameHeight) { tmpHeight = viewHeight - (70 + 40); newFrameHeight = tmpHeight + 70; } 
				var newTop = Math.round((((jQuery(window).height() - newFrameHeight)/2)+jQuery(window).scrollTop()));
				var newLeft = Math.round((((jQuery(window).width() - newFrameWidth)/2)+jQuery(window).scrollLeft()));
				jQuery('#ricePhotoContent').append('<iframe id="ricePhotoIFrame" src="'+c+'" style="border:0px; overflow:hidden; width:'+tmpWidth+'px; height:'+tmpHeight+'px;" frameBorder="no" scrolling="no">');
				jQuery('#ricePhotoFrameOuter').animate({width:newFrameWidth,left:newLeft,top:newTop},200);
				jQuery('#ricePhotoContent').animate({height:tmpHeight,width:tmpWidth},200,function() {
					jQuery('#ricePhotoContent span').remove();
					jQuery('#ricePhotoContent').fadeIn('fast',function() {
						ricePhotoPageResize();
						jQuery('#ricePhotoFrameOuter').fadeIn('fast',function() { ricePhotoTrans = false; });
					});										
				});
			}

			/// display an image
			else if (c.match(/\b(jpg|jpeg|png|gif)\b/gi)) { 
				var img = new Image();
				jQuery(img).load(function() {
					jQuery(this).hide();
					var tmpWidth = jQuery(this).attr('width');
					var tmpHeight = jQuery(this).attr('height');
					var newFrameHeight = tmpHeight + 70;
					var newFrameWidth = tmpWidth+40;
					if ((viewWidth - 20) < newFrameWidth) { 
						tmpWidth = viewWidth - 80;
						newFrameWidth = tmpWidth+40;
						var tmpRatio = Math.round((tmpWidth / jQuery(this).attr('width')) * 100);
					}
					if ((viewHeight - 40) < newFrameHeight) { 
						tmpHeight = viewHeight - (70 + 40);
						newFrameHeight = tmpHeight + 70;
						var tmpRatio = tmpHeight / jQuery(this).attr('height');
						tmpWidth = Math.round(jQuery(this).attr('width') * tmpRatio);
						newFrameWidth = tmpWidth+40;
					} 
					var newTop = Math.round((((jQuery(window).height() - newFrameHeight)/2)+jQuery(window).scrollTop()));
					var newLeft = Math.round((((jQuery(window).width() - newFrameWidth)/2)+jQuery(window).scrollLeft()));
					jQuery('#ricePhotoContent').append('<div id="ricePhotoHolder" style="position:absolute; top:20px; left:20px; height:1px; width:1px; display:none; z-index:9002;"><img src="'+c+'" alt="photo" title="'+d+'" width="'+tmpWidth+'" height="'+tmpHeight+'" style="border:0px; float:left; z-index:9003;" /></div><div id="ricePhotoOverlay" style="display:none; position:absolute; top:20px; left:20px; z-index:9004; float:left; width:'+tmpWidth+'px; height:'+tmpHeight+'px;"></div>');
					jQuery('#ricePhotoFrameOuter').animate({width:newFrameWidth,left:newLeft,top:newTop},200);
					jQuery('#ricePhotoContent').animate({height:tmpHeight,width:tmpWidth},200,function() {
						jQuery('#ricePhotoContent span').remove();
						jQuery('#ricePhotoHolder').fadeIn('fast',function() {
							if ((p > -1) || (n > -1)) { 
								var tmpLinkWidth = (tmpWidth > 120)? Math.floor(tmpWidth / 3) : Math.floor(tmpWidth / 2);
								if (($.browser.msie != undefined) && ($.browser.version == "6.0")) { var butExt = 'gif'; } else { var butExt = 'png'; }
								if ((p > -1) && (jQuery('#ricePhotoLinkPrevious').length < 1)) { jQuery('#ricePhotoOverlay').append('<div id="ricePhotoLinkPrevious" style="float:left; overflow:hidden; position:relative; left:0px; top:0px;"><a href="#previous" style="display:block; background:transparent url(\''+ricePhotoPath+'ricePhotoPrevious.'+butExt+'\'); background-position:-100px center; background-repeat:no-repeat;" id="ricePhotoPreviousButton"><img src="'+ricePhotoPath+'ricePhotoNav.gif" style="width:'+tmpLinkWidth+'px; height:'+tmpHeight+'px; border:0px;" alt="PREVIOUS" title="PREVIOUS" /></a></div>'); jQuery('#ricePhotoLinkPrevious a').hover(function(){ jQuery(this).css('background-position','left center'); },function(){ jQuery(this).css('background-position','-100px center'); } ); jQuery('#ricePhotoPreviousButton').click(function(event){ event.preventDefault(); ricePhotoList(p); });  }
								if ((n > -1) && (jQuery('#ricePhotoLinkNext').length < 1)) { jQuery('#ricePhotoOverlay').append('<div id="ricePhotoLinkNext" style="float:right; overflow:hidden; position:relative; right:0px; top:0px;"><a href="#next" style="display:block; background:transparent url(\''+ricePhotoPath+'ricePhotoNext.'+butExt+'\'); background-position:-100px center; background-repeat:no-repeat;" id="ricePhotoNextButton"><img src="'+ricePhotoPath+'ricePhotoNav.gif" style="width:'+tmpLinkWidth+'px; height:'+tmpHeight+'px; border:0px;" alt="NEXT" title="NEXT" /></a></div>'); jQuery('#ricePhotoLinkNext a').hover(function(){ jQuery(this).css('background-position','right center'); },function(){ jQuery(this).css('background-position','-100px center'); } ); jQuery('#ricePhotoNextButton').click(function(event){ event.preventDefault(); ricePhotoList(n); });  }
							}
							jQuery('#ricePhotoContent').fadeIn('fast',function() {
								ricePhotoPageResize();
								jQuery('#ricePhotoFrameOuter').fadeIn('fast',function() { 
									jQuery('#ricePhotoOverlay').show();
									ricePhotoTrans = false; 
								});
							});
						});
					});
				}).attr('src',c);
				jQuery(img).error(function(){
					alert('Error: could not load '+c);
					ricePhotoClose();
				});
			}
		}); 
	}

	/// resize the mask and the viewer
	function ricePhotoPageResize() {
		if (jQuery('#ricePhotoMask').length > 0) { 
			jQuery('#ricePhotoMask').css({'width':jQuery(window).width(),'height':jQuery(window).height()}); 
			jQuery('#ricePhotoMask').css({'width':jQuery(document).width(),'height':jQuery(document).height()}); 
		}
		if (jQuery('#ricePhotoFrameOuter').length > 0) { 
			jQuery('#ricePhotoFrameOuter').css('top',(((jQuery(window).height()-jQuery('#ricePhotoFrameOuter').height())/2)+jQuery(window).scrollTop())).css('left',(((jQuery(window).width()-jQuery('#ricePhotoFrameOuter').width())/2)+jQuery(window).scrollLeft())); 
		}
	}

	/// close the viewer
	function ricePhotoClose() {
		jQuery('#ricePhotoFrameOuter').fadeOut('fast',function() {
			jQuery('#ricePhotoMask').fadeOut('fast',function() {
				jQuery('#ricePhotoMask').remove();
				jQuery('#ricePhotoFrameOuter').remove();
				jQuery("body").css("overflow","auto");
				jQuery("html").css("overflow","auto");
			});
		});
		ricePhotoTrans = false;
	}

	/// get parameters
	function ricePhotoParams(n,u) {
    	var match = RegExp('[?&]'+n+'=([^&]*)').exec(u);
    	return match && decodeURIComponent(match[1].replace(/\+/g,' '));
	}
		
