if(!SA) var SA = {};

SA._ModalPopUp = Class.create({
	initialize:function(options){
		Element.addMethods(SA);
		this.options = (options || {});
		this.bodycontainer = $$("body")[0];
		this.modalWindow = "modalWindow";
		this.overlay = "overlay";
		this.imageContainer = this.options.imageContainer;
		this.videoContainer = "#videoThumbs div.videoWrap";
		this.images;
		this.activeModal;
	},
	
	preload:function(){
		var image = new Array();
		for(i = 0 ; i < this.images.length; i++ ){
			image[i] = new Image();
			image[i].src = this.images[i];
		}
	},
	
	setImageLink:function(){
		var ths = this;
		var images = [];
		$$(this.imageContainer).each(function(e,i){
			e.setCursorAsClickable();
			var image =  SA.createImage({id:'images',src:ths.images[i]});
			e.on('click',function(){
				ths.createOverlay("0.8");
				ths.adjustedImageSizeBasedOnViewPort(image);
				ths.createModalWithHeader(image);
				ths.setCopyright($("contentWrap"))
				SA.ContextMenu({images: $('images')});
			});
		});
	},
		
	setVideoLink:function(){
		var ths = this;
		var videos = [];
		$$(this.videoContainer).each(function(e,i){
			e.setCursorAsClickable();
			videos[i] = e.title;
			e.on('click',function(){
				ths.createOverlay("0.8");
				ths.showYouTubeVideoInModal(videos[i]);
			});
		});
	},
	
	createOverlay:function(opacity){
		var id = this.overlay;
		var ol = ($(id) == undefined)?SA.createElement('div',{id:id}):$(id).show();
		var head = $$('head')[0];
		style = SA.createElement('style',{type:'text/css'});
		var cssStrOne = '#'+id+'{position: absolute;margin: auto;top: 0; left: 0;width: 100%; height: 100%;z-index: 9999;border: 0;background-color: #000!important;color:#fff;}\n'+
		'\n#'+id+'[id] { position: fixed; }';
		(style.styleSheet)?style.styleSheet.cssText =cssStrOne : style.insert(cssStrOne);
		head.appendChild(style);		
		this.bodycontainer.insert({top:ol});ol.setStyle({zIndex:'200'}); ol.setOpacity(opacity); 
	},
		
	
	adjustedImageSizeBasedOnViewPort:function(thumb,image){
		var spacer = 120;
		if(image == undefined) image = thumb;
		if (SA.elementIsVertical(thumb)){
			if(SA.viewportIsVertical()){
				(image.height > SA.viewport().height)?image.setStyle({width:'auto',height: (SA.viewport().height - spacer) +'px'}):image.setStyle({width:'auto',height: image.height +'px'}) ;
			}else{
				(image.width > SA.viewport().width) ? image.setStyle({width:(SA.viewport().width - spacer) +'px',height: 'auto' }):image.setStyle({width:'auto',height: (SA.viewport().height - spacer)  +'px'});
			}
		}else{
			if(SA.viewportIsVertical()){
				(image.width > SA.viewport().width)?image.setStyle({width:(SA.viewport().width - spacer) +'px',height: 'auto' }):image.setStyle({width:image.width +'px',height: 'auto' }) ;
			}else{
				(image.height > SA.viewport().height) ? image.setStyle({width:'auto',height: (SA.viewport().height - spacer) +'px'}):image.setStyle({width:image.width +'px',height: 'auto' }) ;
			}
		}
	
	},
	createAndSetModal:function(content,w,h){
		var modal = SA.createElement("div",{id:this.modalWindow}).update(content) ;
		this.bodycontainer.insert({top:modal});modal.setStyle({backgroundColor:'#444',position:'absolute',margin:'auto',top:'50%',left:'50%',zIndex:'201',width:w+'px',height:h+'px'});
		modal.centerElement();		
		this.closeOverlay('closeButton',true);
	},
	
	createModalWithHeader:function(content,text){
		var closeButton = SA.createElement("div",{id:'closeButton'}).update("X");
		closeButton.setStyle({position:'absolute',top:'0',right:'0',width:'13px',height:'13px',backgroundRepeat:'none'});
		closeButton.setCursorAsClickable();
		
		var header = SA.createElement("div",{id:'modalHeader'}).update(text);
		header.setStyle({position:'relative',height:'17px', backgroundColor:'#fff',paddingLeft:'3px'});
		header.insert({top:closeButton});
		
		var contentWrap = SA.createElement("div",{id:'contentWrap'}).update(content);
				
		var modal = SA.createElement("div",{id:this.modalWindow});
		modal.insert({top:header});
		modal.insert({bottom:contentWrap});
		this.bodycontainer.insert({top:modal});modal.setStyle({backgroundColor:'#fff',position:'absolute',margin:'auto',top:'50%',left:'50%',zIndex:'201',padding:'5px', color:'#666',fontFamily:'Arial, Helvetica, San-Serif',fontSize:'12px'});
		modal.centerElement();
		
		this.closeOverlay('closeButton',true);
	},
	
	showYouTubeVideoInModal:function(vid){
		 var w = 640; var h = 390;
		if(SA.isiPad()) {
			w = 900;
			h = 550;
		}
		this.createModalWithHeader('<iframe title="YouTube video player" width="'+w+'" height="'+h+'" src="http://www.youtube.com/embed/'+vid+'" frameborder="0" allowfullscreen></iframe>');
	},
	
	createModalFromDiv:function(div){
		this.createOverlay("0.8");
		this.createModalWithHeader($(div).innerHTML);
	},
	
	divPopupLink:function(div){
		var ths = this;
		$("divPopUp").on("click",function(){
			ths.createModalFromDiv(div);
		});
	},
	
	setCopyright:function(parent){
		var footNote = SA.createElement("div",{id:'modalFooter'}).update("Copyright 2011 URNEWYORK. All rights reserved.");
		footNote.setStyle({position:'absolute',height:'17px',bottom:'10px',right:'10px',color:'#fff',padding:'5px',zIndex:'205'});
		parent.insert({bottom:footNote});
	},
	
	removeLargeImageModal:function(){
		if($(this.modalWindow)) $(this.modalWindow).remove();
	},
	closeOverlay:function(el,both){
		var ths = this;
		if(el == undefined){
			el = this.overlay;
			$(this.overlay).setCursorAsClickable();
		}
		Event.observe(el,"click",function(){ ths.removeLargeImageModal();$(ths.overlay).hide(); });
		if(both == true){ $(this.overlay).setCursorAsClickable(); Event.observe(this.overlay,"click",function(){ ths.removeLargeImageModal();$(ths.overlay).hide(); }); }
	}
	
});

SA.ModalPopUp  = function(options){
	return new SA._ModalPopUp(options);
}

