/*
 *	@package jQuery Framework
 *	@subpackage jQuery Custom Popup for Sony Biz
 * 	@author Umit KOL at Nexum Bogazici
**/
var popup = null;
(function($){
	jQuery.fn.jPop = function(o) {
		o = {
			overlayID : "#jPopOverlay",
			popupCont : "jPopCont",
			popupDiv : ".jPopContent",
			popupLoader : "jPopLoader",
			popupIframe : "jPopIframe",
			closeButton : "jPopupClose",
			height : 300,
			width : 500,
			speed : 600
		}
		jQuery(this).each(function() {
			jQuery(this).click(function(event) {
				event.preventDefault();
				popup = new Popup(o, {
					type : jQuery(this).attr("rel"),
					url : jQuery(this).attr("href")
				});
				if (jQuery(this).attr("rev") != "") {
					o.width = jQuery(this).attr("rev").split(/\|/g)[0].split(/=/g)[1];
					o.height = jQuery(this).attr("rev").split(/\|/g)[1].split(/=/g)[1];
				}
				popup.Create();
			});
		});
		jQuery("a."+o.closeButton).unbind("click").live("click", function() {
			popup.Close();
		});
		popup = null;
	}
})(jQuery);

function Popup(o, options) {
	var Popup = {
		$this : null,
		content : null,
		id : options.id || null,
		type : options.type || null,
		url : options.url || null,
		
		UseSize : function(o) {
			return (o == "h") ? jQuery("body").height() : jQuery("body").width();
		},
		
		CreateId : function() {
			return "popup-"+Math.ceil(Math.random()*100000);
		},
		
		Create : function() {
			this.id = this.CreateId();
			jQuery("<div></div>")
			.attr({
				id : o.overlayID.replace("#","")
			})
			.height(Popup.UseSize("h"))
			.width(Popup.UseSize("w"))
			.prependTo("body");
					
			this.$this = jQuery(
		   "<div>"+
				"<a class=\""+o.closeButton+"\" href=\"javascript:;\">Close</a>"+
				"<table>"+
					"<tr class=\"jPopupTop\"><td class=\"jPopupTopLeft\">&nbsp;</td><td class=\"jPopupTopMiddle\">&nbsp;</td><td class=\"jPopupTopRight\">&nbsp;</td></tr>"+
					"<tr class=\"jPopupMiddle\"><td class=\"jPopupMiddleLeft\"><img src=\"/biz/image/bdy/blank.gif\" width=\"20\" /></td><td class=\"jPopupMiddleMiddle\"><div class=\"jPopContent\"></div></td><td class=\"jPopupMiddleRight\"><img src=\"/biz/image/bdy/blank.gif\" width=\"20\" /></td></tr>"+
					"<tr class=\"jPopupBottom\"><td class=\"jPopupBottomLeft\">&nbsp;</td><td class=\"jPopupBottomMiddle\">&nbsp;</td><td class=\"jPopupBottomRight\">&nbsp;</td></tr>"+
				"</table>"+
			"</div>"
			)
			.attr({
				className : o.popupCont,
				id : Popup.id
			})
			.insertAfter(o.overlayID).css("position", function() {
				if (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6) {
					jQuery('<iframe src="javascript:false;document.write(\'\');" class="'+o.popupIframe+'"></iframe>').prependTo(o.overlayID).css({opacity:0});
					return "absolute";	
				}
			});
			
			this.Loading.Show();
			
			switch(this.type) {
				
				case 'gallery':
					this.content = this.Mode.Gallery();
				break;
					
				case 'video':
					this.content = this.Mode.Video();
					o.height = 342;
					o.width = 535;
				break;
				
				case '3d':
					this.content = this.Mode._3d();
					o.height = 600;
					o.width = 640;
				break;
				
			}
			
			this.Animate.Start(
			
				function() {
					if (Popup.Animate.isFinish) {
						Popup.content.appendTo(Popup.$this.find(o.popupDiv)).hide();
						if (Popup.type == "video") {
							setTimeout(function() {
								Popup.Loading.Hide();
								Popup.$this.find("a."+o.closeButton).show();
								Popup.content.show();					 
							},1000);
						} else {
							Popup.content.load(function() {
								Popup.Loading.Hide();
								Popup.$this.find("a."+o.closeButton).show();
								Popup.content.fadeIn("slow");
							});
						}
					}
				}
				
			);
			
			
			
			//this.$this.find(o.popupDiv).append(this.content);
			
		},
		
		Loading : {
			
			Show : function() {
				
				jQuery("#"+Popup.id).find(o.popupDiv).addClass(o.popupLoader);
				
			},
			
			Hide : function() {
				
				jQuery("#"+Popup.id).find(o.popupDiv).removeClass(o.popupLoader);
				
			}
			
		},
		
		Animate : {
			$this : null,
			isFinish : false, 
			
			Start : function(CallBack) {
				this.$this = Popup.$this;
				this.$this.animate(
					{
						marginTop : -(o.height/2),
						marginLeft : -(o.width/2)
					},
					o.speed
				);
				this.$this.find(o.popupDiv).animate(
					{
						height : o.height,
						width : o.width
					},
					o.speed,
					function() {
						Popup.Animate.isFinish = true;
						CallBack();
					}
				);
				
				this.isFinish = false;
				
			}
		},
		
		Mode : {
			parent : Popup,
			
			Gallery : function() {
				
				return jQuery('<iframe frameborder="0"></iframe><div class="cntImageGalleryNav clearfix"><p>Click on thumbnails to enlarge</p></div>').attr({
					className : "jGallery",
					src : Popup.url
				});
				
			},
			
			Video : function() {
				
				return jQuery('<object CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" CODEBASE="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="535" HEIGHT="342" ID="fetch_content"><param NAME="movie" VALUE="'+Popup.url+'"><param NAME="quality" VALUE="High"><param NAME="bgcolor" VALUE="#999999"><param NAME="allowfullscreen" VALUE="true"><param NAME="WMode" VALUE="Transparent"><embed SRC="'+Popup.url+'" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" QUALITY="High" BGCOLOR="#999999" WIDTH="535" HEIGHT="342" NAME="video_en_HVR-Z5E" ALIGN="" WMODE="transparent"></embed></object><iframe style="display:none" frameborder="0"></iframe>').attr({
					className : "jVideo"
				});
					
			},
			
			_3d : function() {
				
				return jQuery("<iframe frameborder='0'></iframe>").attr({
					className : "j3d",
					src : Popup.url
				});
				
			}
			
		},
		
		Close : function() {
			this.$this = Popup.$this;
				
			this.$this.animate(
				{
					marginTop : -(o.height/2-100)
				},
				o.speed/2,
				function() {
					Popup.$this.animate(
						{
							marginTop : -(o.height*5)
						},
						o.speed,
						function() {
							Popup.$this.remove();
							jQuery(o.overlayID).fadeOut("slow").remove();
						}
					);
				}
			);
		}
		
	}
	
	return Popup;
}
