//
// library something something
//

/*

	<div id="nsplurtBackground"></div>
	<div id="nsplurtShadow"></div>
	<div id="nsplurtWindow" class="headSless controls">
		<div id="nsplurtTitleBar">
        	<a href="#" id="nsplurtPrevLink">Previous Image</a>
        	<a href="#" id="nsplurtNextLink">Next Image</a>
            <h4><em>(3 of 15)</em> SmartTouch&trade; Lock</h4>
        </div> 
        <div id="nsplurtStage" class="round-img">
        
        </div>
		<div id="nsplurtFooter">
        	<a href="#" id="nsplurtCloseLink">Close Window</a>
        </div>
    </div>
	<div id="nsplurtLoading"><img src="/_img/shared/splurt-loading.gif" alt="loading..." /></div>


*/


var SPLURT={ 
	anchors:null,
	background:null,
	backgroundId:"splurtBackground",
	baseHeight:0,
	baseWidth:0,
	buttonClose:null,
	buttonCloseId:"splurtCloseLink",
	buttonNext:null,
	buttonNextId:"splurtNextLink",
	buttonPrev:null,
	buttonPrevId:"splurtPrevLink",
	caption:null,
	captionId:"splurtCaption",
	carousel:null,
	carouselNum:0,
	carouselImages:[],
	controls:false,
	corners:{"tl":null,"tr":null,"bl":null,"br":null},
	footer:null,
	footerId:"splurtFooter",
	gutters:{"h":5,"v":5},
	content:null,
	contentId:"splurtContent",
	imageHeight:0,
	imageWidth:0,
	loading:null,
	loadingId:"splurtLoading",
	shadow:null,
	shadowId:"splurtShadow",
	stage:null,
	stageId:"splurtStage",
	stageclick:null,
	titlebar:null,
	titlebarId:"splurtTitleBar",
	window:null,
	windowId:"splurtWindow",
	
	maxHeight:800,
	maxWidth:1050,
	
	top:10,
	left:10,
	width:568,
	height:360,

	changeImage:function(iNum){
		var imgPreloader=new Image();
		imgPreloader.onload=function() {
			bNeedScrollers = false;
			imgPreloader.onload=null;
			imageWidth=this.width;
			imageHeight=this.height;
			SPLURT.baseHeight=imageHeight;
			SPLURT.baseWidth=imageWidth;
			
			SPLURT.content.src=SPLURT.carouselImages[iNum].image;
			SPLURT.content.alt=SPLURT.carouselImages[iNum].title;
			caption="<em>("+(iNum+1)+" of "+SPLURT.carouselImages.length+")</em> "+SPLURT.carouselImages[iNum].title;
			SPLURT.caption.innerHTML=caption;
			SPLURT.carouselNum=iNum;
			GLOBALS.removeElement(SPLURT.loading);
			SPLURT.loading=null;
			imgPreloader=null;
			};
		imgPreloader.src=SPLURT.carouselImages[iNum].image;
		
		
	},
	createLayers:function(mode){
		if(!mode)mode="normal";
		if (!$.getById(SPLURT.backgroundId)){
			SPLURT.background=GLOBALS.addElement(document.body,"div",{"id":SPLURT.backgroundId});
			SPLURT.shadow=GLOBALS.addElement(document.body,"div",{"id":SPLURT.shadowId});
			SPLURT.window=GLOBALS.addElement(document.body,"div",{"id":SPLURT.windowId});
			SPLURT.loading=GLOBALS.addElement(document.body,"div",{"id":SPLURT.loadingId});
			GLOBALS.addElement( SPLURT.loading,"img",{"src":"/_img/shared/splurt-loading.gif"});

			// get the loading graphic up ASAP
			SPLURT.loading.style["display"]="block";
			SPLURT.background.style["display"]="block";
			
			SPLURT.window.style["left"]="-9000px";
			if(mode){
				SPLURT.window.className=mode;
			}
			/*
			SPLURT.window.onclick=function(){
				try{
					SPLURT.window.onclick=null;
					return SPLURT.remove();
				} catch(e){
					// alert("bad window");	
				}
			};
			*/
			
			SPLURT.window.style["display"]="block";
			SPLURT.shadow.style["display"]="none";
						
			SPLURT.background.onclick=function(){
				try{
					SPLURT.background.onclick=null;
					return SPLURT.remove();
				} catch(e){
					// alert("bad background");	
				}
			};
			window.onscroll=function(){ 
				SPLURT.position();
			};
			
			SPLURT.titlebar=GLOBALS.addElement(SPLURT.window,"div",{"id":SPLURT.titlebarId});
			SPLURT.buttonPrev=GLOBALS.addElement(SPLURT.titlebar,"a",{"id":SPLURT.buttonPrevId});
			SPLURT.buttonNext=GLOBALS.addElement(SPLURT.titlebar,"a",{"id":SPLURT.buttonNextId});
			SPLURT.caption=GLOBALS.addElement(SPLURT.titlebar,"h4",{"id":SPLURT.captionId});

			SPLURT.stage=GLOBALS.addElement(SPLURT.window,"div",{"id":SPLURT.stageId});
			// SPLURT.stage.className="round-img";
			/*
			SPLURT.stage.onclick=function(){
				try{
					SPLURT.stage.onclick=null;
					return SPLURT.remove();
				} catch(e){
					// alert("bad stage");
				}
			};
			*/
			
			SPLURT.footer=GLOBALS.addElement(SPLURT.window,"div",{"id":SPLURT.footerId});
			SPLURT.buttonClose=GLOBALS.addElement(SPLURT.titlebar,"a",{"id":SPLURT.buttonCloseId});
			SPLURT.buttonClose.innerHTML="Close Window";
			SPLURT.buttonClose.onclick=function(){
				try{
					SPLURT.buttonClose.onclick=null;
					return SPLURT.remove();
				} catch(e){
					// alert("bad buttonClose");
				}
			};
			
			var calcWinHeight=String(SPLURT.window.offsetHeight).replace(/px/i,"");
			var calcStageHeight=String(SPLURT.stage.offsetHeight).replace(/px/i,"");
			
			if(!isNaN(calcWinHeight)&&!isNaN(calcStageHeight)){
				SPLURT.gutters.v=parseInt(calcWinHeight)-parseInt(calcStageHeight);
			} else {
				SPLURT.gutters.v=0;
			}

			
			var calcWinWidth=String(SPLURT.window.offsetWidth).replace(/px/i,"");
			var calcStageWidth=String(SPLURT.stage.offsetWidth).replace(/px/i,"");

			if(!isNaN(calcWinWidth)&&!isNaN(calcStageWidth)){
				SPLURT.gutters.h=parseInt(calcWinWidth)-parseInt(calcStageWidth);
			} else {
				SPLURT.gutters.h=0;
			}

			if(mode.indexOf("controls")>=0){
				SPLURT.buttonNext.innerHTML="Next";
				SPLURT.buttonPrev.innerHTML="Previous";
				SPLURT.buttonNext.onclick=function(){ return SPLURT.moveNext();};
				SPLURT.buttonPrev.onclick=function(){ return SPLURT.movePrev();};
			}
			
			SPLURT.shadow.style["display"]="none";
			window.onresize=function(){ SPLURT.position();};

			}
		},
 	init:function(){
		SPLURT.anchors=$.getByClass("splurt",null,"a");
		for(var counter=0;counter<SPLURT.anchors.length;counter++){
			SPLURT.anchors[counter].viewlargerDescription = SPLURT.anchors[counter].title;
			SPLURT.anchors[counter].title = "click to view larger";
			SPLURT.anchors[counter].onclick=function(){
				this.blur();
				SPLURT.show(this.viewlargerDescription,this.href);
				return false;
				};
			// GLOBALS.addElement(anchors[counter],"img",{"alt":"View a larger version of this image","class":"magnify","src":"/_img/shared/SPLURT-magnify.gif"});
			}
			// SPLURT.createLayers();
		},
	loadJson:function(id){
		try{
			var jsonData=SERVERREQUEST.get("/_json/carousel/"+id+".js");
		} catch(e){
			// alert(e.message);
			jsonData="[];";	
		}
		SPLURT.carouselImages=eval(jsonData);
		if(!SPLURT.carouselImages)SPLURT.carouselImages=[];
		},
	loadPosition:function(){
		SPLURT.overlaySize();
		with(SPLURT.loading.style){
			left=(SCREEN.getScrollLeft()+(SCREEN.getViewportWidth()-100)/2)+"px";
			top=(SCREEN.getScrollTop()+((SCREEN.getViewportHeight()-100)/2))+"px";
			display="block";
			}
		},
	moveNext:function(){
		SPLURT.loading=GLOBALS.addElement(document.body,"div",{"id":SPLURT.loadingId,"style":"display:none;"});
		SPLURT.loadPosition();
		var targetNum=SPLURT.carouselNum;
		targetNum++;
		if(targetNum>=SPLURT.carouselImages.length){
			targetNum=0;
		}
		SPLURT.changeImage(targetNum);
	},
	movePrev:function(){
		SPLURT.loading=GLOBALS.addElement(document.body,"div",{"id":SPLURT.loadingId,"style":"display:none;"});
		SPLURT.loadPosition();
		var targetNum=SPLURT.carouselNum;
		targetNum=targetNum-1;
		if(targetNum<0){
			targetNum=SPLURT.carouselImages.length-1;
		}
		SPLURT.changeImage(targetNum);
	},
	overlaySize:function(){
		SPLURT.background.style.height=SCREEN.getDocumentHeight()+"px";
		SPLURT.background.style.width=SCREEN.getDocumentWidth()+"px";
		},
	position:function(){
		SPLURT.overlaySize();
		var bNeedScrollers=false;

		var vpWidth=SCREEN.getViewportWidth();
		var vpHeight=SCREEN.getViewportHeight();
		SPLURT.maxWidth=vpWidth-(SPLURT.gutters.h+0);
		SPLURT.maxHeight=vpHeight-(SPLURT.gutters.v+0);

		SPLURT.setHeight(SPLURT.baseHeight);
		SPLURT.setWidth(SPLURT.baseWidth);

		if(parseInt(SPLURT.width)>parseInt(SPLURT.maxWidth)){
			SPLURT.setWidth(SPLURT.maxWidth);
			bNeedScrollers=true;
		}
		if(parseInt(SPLURT.height)>parseInt(SPLURT.maxHeight)){
			SPLURT.setHeight(SPLURT.maxHeight);
			bNeedScrollers=true;
		}
		
		/// winHeight=$.getStyle(SPLURT.window,"height");
		// winWidth=$.getStyle(SPLURT.window,"width");
		
		if(bNeedScrollers){
			SPLURT.stage.style["overflow"]="scroll";
		} else {
			SPLURT.stage.style["overflow"]="visible";
		}
		
		SPLURT.left=(SCREEN.getScrollLeft()+((vpWidth-(SPLURT.width+SPLURT.gutters.h))/2));
		SPLURT.top=(SCREEN.getScrollTop()+((vpHeight-(SPLURT.height+SPLURT.gutters.v))/2));

		with(SPLURT.window){
			style.top=SPLURT.top+"px";
			style.left=SPLURT.left+"px";
		}
		with(SPLURT.shadow){
			style.top=(SPLURT.top+10)+"px";
			style.left=(SPLURT.left+10)+"px";
		}
		},
	remove:function(){
		try{
			with(window){
				if (window.onresize) window.onresize=null;
				if (window.onscroll) window.onscroll=null;
			};
		} catch(e){
			//
		}
		if (SPLURT.background){
			SPLURT.background.onclick=null;
			GLOBALS.removeElement(SPLURT.background);
			SPLURT.background=null;
		}

		if (SPLURT.buttonClose){
			SPLURT.buttonClose.onclick=null;
			GLOBALS.removeElement(SPLURT.buttonClose);
			SPLURT.buttonClose=null;
		}


		if (SPLURT.footer){
			GLOBALS.removeElement(SPLURT.footer);
			SPLURT.footer=null;
		}

		if (SPLURT.image){
			GLOBALS.removeElement(SPLURT.image);
			SPLURT.image=null;
		}

		if (SPLURT.content){
			SPLURT.content.onclick=null;
			GLOBALS.removeElement(SPLURT.content);
			SPLURT.content=null;
		}

		if (SPLURT.stageclick){
			SPLURT.stageclick.onclick=null;
			GLOBALS.removeElement(SPLURT.stageclick);
			SPLURT.stageclick=null;
		}

		if (SPLURT.stage){
			SPLURT.stage.onclick=null;
			GLOBALS.removeElement(SPLURT.stage);
			SPLURT.stage=null;
		}

		if (SPLURT.buttonPrev){
			SPLURT.buttonPrev.onclick=null;
			GLOBALS.removeElement(SPLURT.buttonPrev);
			SPLURT.buttonPrev=null;
		}

		if (SPLURT.buttonNext){
			SPLURT.buttonNext.onclick=null;
			GLOBALS.removeElement(SPLURT.buttonNext);
			SPLURT.buttonNext=null;
		}

		if (SPLURT.caption){
			GLOBALS.removeElement(SPLURT.caption);
			SPLURT.caption=null;
		}

		if (SPLURT.titlebar){
			GLOBALS.removeElement(SPLURT.titlebar);
			SPLURT.titlebar=null;
		}

		if (SPLURT.background){
			SPLURT.background.onclick=null;
			GLOBALS.removeElement(SPLURT.background);
			SPLURT.background=null;
		}

		if (SPLURT.shadow){
			GLOBALS.removeElement(SPLURT.shadow);
			SPLURT.shadow=null;
		}

		if (SPLURT.window){
			GLOBALS.removeElement(SPLURT.window);
			SPLURT.window=null;
		}

		if (SPLURT.loading){
			GLOBALS.removeElement(SPLURT.loading);
			SPLURT.loading=null;
		}

		return false;
		},
	setHeight:function(ht){
		SPLURT.height=ht;
		SPLURT.stage.style["height"]=ht+"px";
		SPLURT.shadow.style["height"]=(ht+SPLURT.gutters.v)+"px";
	},
	setWidth:function(wd){
		SPLURT.width=wd;
		SPLURT.stage.style["width"]=wd+"px";
		SPLURT.shadow.style["width"]=(wd+SPLURT.gutters.h)+"px";
		SPLURT.titlebar.style["width"]=wd+"px";
		SPLURT.footer.style["width"]=wd+"px";
	},
	show:function(caption,url){
      	var queryString = url.replace(/^[^\?]+\??/,'');
		// bCloseOnClick defaults to false for HTML content splurts, and true for images, etc.
		// can override by including the "clickclose=" parameter on the splurt query string.
		var bCloseOnClick=false;
		var params = GLOBALS.parseQueryString( queryString );
		if(params['c']){
			SPLURT.carousel=params['c'];
			SPLURT.loadJson(params['c']);
			SPLURT.carouselNum=0;
			if(params['p']){
				if(!isNaN(params['p'])){
					SPLURT.carouselNum=parseInt(params['p']);
				}
			}
		}
		var mode="normal";
		if (params['mode']){
			mode=params['mode'];
		} else if(caption==""){
			mode="headless";
		}
		if(SPLURT.carouselImages.length>0){
			mode+=" controls";
		}
		SPLURT.createLayers(mode);
		
		var urlString = /\.jpg|\.jpeg|\.png|\.gif|\.bmp/g;
     	var urlType = url.toLowerCase().match(urlString);

    	if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){//code to show images
			// SPLURT.loadPosition();
			imgPreloader=new Image();
			bCloseOnClick=true;
			imgPreloader.onload=function() {
				bNeedScrollers = false;
				imgPreloader.onload=null;
				imageWidth=this.width;
				imageHeight=this.height;
				SPLURT.baseHeight=imageHeight;
				SPLURT.baseWidth=imageWidth;

				// SPLURT.WIDTH=imageWidth+30;
				// SPLURT.HEIGHT=imageHeight+60;
				
	
	
				SPLURT.content=GLOBALS.addElement(SPLURT.stage,"img", {"id":SPLURT.contentId, "src":url, "width":imageWidth,"height":imageHeight,"alt":caption});

				// SPLURT.content.className="round-img";
/*
			var ul=document.createElement("div");
			ul.className="ul";
			div.appendChild(ul);
			var ur=document.createElement("div");
			ur.className="ur";
			div.appendChild(ur);
			var ll=document.createElement("div");
			ll.className="ll";
			div.appendChild(ll);
			var lr=document.createElement("div");
			lr.className="lr";
			div.appendChild(lr);
*/			
				SPLURT.corners.tl=GLOBALS.addElement(SPLURT.stage,"div",{"class":"tl"});
				SPLURT.corners.tr=GLOBALS.addElement(SPLURT.stage,"div",{"class":"tr"});
				SPLURT.corners.bl=GLOBALS.addElement(SPLURT.stage,"div",{"class":"bl"});
				SPLURT.corners.br=GLOBALS.addElement(SPLURT.stage,"div",{"class":"br"});


				// SPLURT.stage.className="round-img";
				// ROUND.init();

				if(SPLURT.carouselImages.length>0){
					caption="<em>("+(SPLURT.carouselNum+1)+" of "+SPLURT.carouselImages.length+")</em> "+caption;
				}

				SPLURT.caption.innerHTML=caption;
				
				SPLURT.position();
				SPLURT.window.style['display']="block";
				SPLURT.shadow.style['display']="block";

				
				
				GLOBALS.removeElement(SPLURT.loading);
				SPLURT.loading=null;

				imgPreloader=null;
				};
			imgPreloader.src=url;
				
		} else {
	     	SPLURT.baseWidth = (params['width']*1) + 30 || 572; //defaults to 630 if no paramaters were added to URL
	      	SPLURT.baseHeight = (params['height']*1) + 40 || 298; //defaults to 440 if no paramaters were added to URL
	
			var scrollParam="no";
			if (params['scroll']=="yes"){
				scrollParam="yes";
			} else {
				SPLURT.stage.className="noborder";
			}
			
	      	SPLURT.width = SPLURT.baseWidth - 5;
	      	SPLURT.height = SPLURT.baseHeight - 45;
			
			SPLURT.content=GLOBALS.addElement(SPLURT.stage,"iframe",{"id":SPLURT.content,"src":url,"frameBorder":"0","scrolling":scrollParam});
			/*
			SPLURT.contentclicker=GLOBALS.addElement(SPLURT.stage,"div",{"id":"splurtContentClicker","style":"background-color:transparent; display:block; height:100%; position:absolute; top:0; width:100%;"});
			SPLURT.contentclicker.onclick=function(){
				alert("hello");
				// SPLURT.contentclicker.onclick=null;
				// return SPLURT.remove();
			};
			*/			

			// SPLURT.content.style['frameborder']="0";

			SPLURT.content.style['width']=SPLURT.baseWidth + "px";
			SPLURT.content.style['height']=SPLURT.baseHeight + "px";

				// SPLURT.caption.innerHTML=caption;
			SPLURT.position();

			// SPLURT.window.className="headless";
			SPLURT.window.style['display']="block";
			SPLURT.shadow.style['display']="block";

			GLOBALS.removeElement(SPLURT.loading);
			SPLURT.loading=null;

	    }
		if (params['clickclose']){
			bCloseOnClick=params['clickclose'];
		}
		if(bCloseOnClick){
			SPLURT.stageclick=GLOBALS.addElement(SPLURT.stage,"div",{"id":"splurtStageClick","style":"background-color:transparent; display:block; height:100%; position:absolute; top:0; width:100%;"});
			SPLURT.stageclick.onclick=function(){
				try{
					SPLURT.stageclick.onclick=null;
					return SPLURT.remove();
				} catch(e){}
				};
		}
		// window.onscroll=function(){ SPLURT.position();};
		// window.onresize=function(){ SPLURT.position();};
		}
	};	

GLOBALS.addOnload(SPLURT.init);


//