/*globals window,document,jQuery,alert */
/* ***********************************************************************************

	CJ Image Video Previewer JavaScript framework

	Copyright (c) 2009-2010, Doug Jones. All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:
	
	a) Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	  
	b) Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution. 
	  
	c) Neither the name of the Creative Juices, Bo. Co. nor the names of its
	   contributors may be used to endorse or promote products derived from
	   this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
	A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
	OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
	LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
	OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

	For further information, visit the Creative Juices website: www.cjboco.com.

*********************************************************************************** */

/*
 mit license. paul irish. 2010. (http://gist.github.com/268257)
*/
(function($){jQuery.fn.imagesLoaded=function(b){var c=this.filter('img'),len=c.length;c.bind('load',function(){if(--len<=0){b.call(c,this)}}).each(function(){if(this.complete||this.complete===undefined){var a=this.src;this.src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";this.src=a}})}})(jQuery);

/*
 CJ Image Video Previewer
*/
(function($){$.fn.cjImageVideoPreviewer=function(c){var d={images:[],delay:450,autoPlay:false,showProgress:true};var f={version:'1.0.2',elem:null,idx:1,timer:null,loaded:0,mouseX:null,mouseY:null,state:false};function clearTimer(){if(f.timer!==null){window.clearTimeout(f.timer);f.timer=null}}function stopAnimation(){if(f.state){clearTimer();f.idx=0;f.state=false;$(f.elem).find("div.cjImageVideoPreviewer img:first").css({"display":"block"});$(f.elem).find("div.cjImageVideoPreviewer img:not(:first)").css({"display":"none"})}}function setThumbnail(){var a=$(f.elem).attr("id")+"_IMG_"+f.idx;var b=$(f.elem).attr("id")+"_IMG_"+(f.idx+1>d.images.length-1?1:f.idx+1);f.idx=f.idx+1>d.images.length-1?1:f.idx+1;$(f.elem).find(".cjImageVideoPreviewer img#"+b).css({"display":"block"});$(f.elem).find(".cjImageVideoPreviewer img#"+a).css({"display":"none"});clearTimer();if(d.autoPlay){f.timer=window.setTimeout(setThumbnail,d.delay)}else{if(f.mouseX>=$(f.elem).offset().left&&f.mouseX<=$(f.elem).offset().left+$(f.elem).width()&&f.mouseY>=$(f.elem).offset().top&&f.mouseY<=$(f.elem).offset().top+$(f.elem).height()){f.timer=window.setTimeout(setThumbnail,d.delay)}else{stopAnimation()}}}function checkLoaded(){if(f.loaded>d.images.length-2){$(f.elem).find("div.cjImageVideoPreviewerProgress").hide();f.timer=window.setTimeout(setThumbnail,d.delay)}else{if(d.showProgress){var a=$(f.elem).find("div.cjImageVideoPreviewerProgress"),pbar=$(f.elem).find("div.cjImageVideoPreviewerProgress div.cjImageVideoPreviewerProgressBar");if($(a).css("display")!=="block"){$(a).show()}$(pbar).css({"left":parseInt($(pbar).css("left"),10)+parseInt($(pbar).width()/(d.images.length-2),10)+"px"})}}}function setUpImageQue(){var b=null;d.images.unshift($(f.elem).find("img:first").attr("src"));if($(f.elem).find("img").parent()[0].nodeName==="A"){b=$(f.elem).find("img").parent()[0].href}$(f.elem).append('<div class="cjImageVideoPreviewer">');$(f.elem).find(".cjImageVideoPreviewer").css({"display":"none","width":$(f.elem).width()+"px","height":$(f.elem).height()+"px","overflow":"hidden","position":"absolute","top":"0px","left":"0px"});if(d.showProgress){$(f.elem).find(".cjImageVideoPreviewer").append($('<div class="cjImageVideoPreviewerProgress">').css({"display":"none","width":($(f.elem).width()-4)+"px","height":"8px","overflow":"hidden","position":"absolute","top":"0px","left":"0px","background-color":typeof $(f.elem).css("border-top-color")==="string"?$(f.elem).css("border-top-color"):"#000","border-width":"2px","border-style":"solid","border-color":typeof $(f.elem).css("border-top-color")==="string"?$(f.elem).css("border-top-color"):"#000","z-index":"20"}).append($('<div class="cjImageVideoPreviewerProgressBar">').css({"display":"block","width":($(f.elem).width()-4)+"px","height":"6px","overflow":"hidden","position":"absolute","top":"0","left":"-"+($(f.elem).width()-4)+"px","background":"#6bc4f7 url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAGCAIAAABSPBl5AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAANRJREFUeNpckUtyxDAIRGms8WIq+9lMTpzL5RC5QhYZ23QakCcfFaIQEo+P8Pb+5TDJAgzY8FOAi7ffANMi7aAFbQvbyV26Jf2MuhofnzFZTuGEWBesLoNrcvEPtwcfkUTpx8FCJ7SJQwe3WsywxTJJ5EkaLAPz/ulknM8O6+pSJ+52hXCLz2Yv3pKdjrId6HyFqHICWxYoG9vZ7BHQg3G/es/uz/iQCTS+9j+bDSKyEAVL49fgMGf3+pKPtVVCBeew+gcmy35w7B6J/pPSxTpvvwUYAKIhoZSkg7l0AAAAAElFTkSuQmCC) repeat-x top left","z-index":"21"})))}$(d.images).each(function(i){var a=$(f.elem).attr("id")+"_IMG_"+i;$(f.elem).find(".cjImageVideoPreviewer:first").append($('<img src="'+d.images[i]+'" id="'+a+'" />').css({"display":"none","position":"absolute","top":"0px","left":"0px","z-index":10}));$("#"+a).imagesLoaded(function(){f.loaded++;checkLoaded()})});if(b){$(f.elem).find(".cjImageVideoPreviewer").append($('<div>').css({"display":"block","width":$(f.elem).width()+"px","height":$(f.elem).height()+"px","position":"absolute","top":"0px","left":"0px","cursor":d.autoPlay?"":"pointer","z-index":100}).click(function(){document.location.href=b}))}$(f.elem).find(".cjImageVideoPreviewer").css({"display":"block"})}function startAnimation(){f.state=true;clearTimer();if($(f.elem).find(".cjImageVideoPreviewer:first").length===0){setUpImageQue();f.timer=window.setTimeout(startAnimation,100)}else{f.timer=window.setTimeout(setThumbnail,d.delay)}}function init(){if(d.images.length>1&&$(f.elem).find("img").length>0){d.delay=d.delay<0?0:parseInt(d.delay,10);if($(f.elem).css("position")!=="relative"&&$(f.elem).css("position")!=="absolute"){alert("CJ Image Video Preview v"+(f.version)+" Error!\n\nYou parent element (#"+$(f.elem).attr("id")+") must have it's positioning set to either RELATIVE or ABSOLUTE.\n\nPosition: "+($(f.elem).css('position')));return}if(d.autoPlay){startAnimation()}else{$(document).mousemove(function(e){f.mouseX=e.pageX;f.mouseY=e.pageY});$(f.elem).hover(function(e){if(f.timer===null){startAnimation()}},function(){stopAnimation()})}}}if(c){$.extend(d,c)}return this.each(function(){f.elem=this;init()})}})(jQuery);
