/**
 * Simple Blowup.
 * @author CNPP
 * @requires jquery.easing.js
 * @version 1.0.0	initial version			
 */
jQuery.fn.blowup = function(op) {	
	// defaults
	var defaults =  {
			selectorBlowupItem:".blowup",
			classActive:"active",
			initWidth:640,
			initHeight:460,
			timeAnimate:600,
			timeFadeIn:600,
			timeHideBlowup:300,
			easingAnimate:"easeOutSine",
			easingFadeIn:"easeOutSine",
			easingHideBlowup:"easeOutSine",
			blowupMargin:10,
			txtClose:"close",
			txtNext:"next",
			txtPrevious:"previous",
			txtDivider:"/"
	};
	jQuery.extend(defaults, op);
	
	// size
	var blowupSize = {
		width:defaults.initWidth,
		height:defaults.initHeight	
	}
	
	// markup
	generateMarkup();
	
	// browser flags
	if (jQuery.browser.msie) {
		// ie
		jQuery("#blowup").addClass("ie");
		// version
		var v = jQuery.browser.version;
		var vc = "v"+v.charAt(0);
		jQuery("#blowup").addClass(vc);
	}
	
	// self
	var self = this;
	var elContainer = jQuery("#blowup");
	var elWindow = jQuery("#blowupWindow");
	var elOverlay = jQuery("#blowupOverlay");
	var elContent = jQuery("#blowupContent");
	var elInfo = jQuery("#blowupInfo");
	var elTitle = jQuery("#blowupTitle");
	var elDescription = jQuery("#blowupDescription");
	var elClose = jQuery("#blowupClose");
	var elNext = jQuery("#blowupNext");
	var elPrevious = jQuery("#blowupPrevious");
	var elCounter = jQuery("#blowupCounter");
	var elTotal = jQuery("#blowupTotal");
	
	
	// hide
	elContainer.hide();
	elNext.hide();
	elPrevious.hide();
	elContainer.hide();
	
	
	// init
	var blowupItems = jQuery(defaults.selectorBlowupItem);
	for (var i = 0; i < blowupItems.length; i++) {
		// item
		var bItem = blowupItems[i];
		
		// events
		jQuery(bItem).bind("click",i,showBlowup);
		
	}
	var blowupItemIndex = 0;
	var blowupItemTotal = blowupItems.length;
	
	// info
	if (blowupItemTotal <= 1) {
		elInfo.hide();
	}
	
	// events
	elClose.bind("click",hideBlowup);
	elOverlay.bind("click",hideBlowup);
	elWindow.bind("mousemove",indicateNextPrev);
	elContent.bind("click",gotoNextPrev);
	
	// browser
	var browserIE = false;
	var browserIE6 = false;
	if (jQuery.browser.msi) {
		browserIE = true;
		if (jQuery.browser.version < 7) {
			browserIE6 = true;
		}
	}
	
	
	/**
	* Generates the markup.
	*/
	function generateMarkup() {
		// code
		var markup = "<div id='blowup'><div id='blowupOverlay'></div><div id='blowupWindow'><h3 id='blowupTitle'>Image Title</h3><p id='blowupDescription'>Image Description</p><div id='blowupInfo'><span id='blowupCounter'>1</span><span id='blowupDivider'>"+defaults.txtDivider+"</span><span id='blowupTotal'>12</span></div><a href='#' title='"+defaults.txtClose+"' id='blowupClose'>"+defaults.txtClose+"</a><a href='#' title='"+defaults.txtNext+"' id='blowupNext'>"+defaults.txtNext+"</a><a href='#' title='"+defaults.txtPrevious+"' id='blowupPrevious'>"+defaults.txtPrevious+"</a><div id='blowupContent'></div></div></div>"
		// append
		jQuery("body").append(markup);
		
		
	}
	
	
	

	
		
		
	/**
	* Shows the blowup.
	*/
	function showBlowup(e) {
		if (! browserIE6) {
			jQuery(elWindow).css({"width":defaults.initWidth,"height":defaults.initHeight,"margin-left":-defaults.initWidth/2,"margin-top":-defaults.initHeight/2});
		}
		jQuery(elContainer).css({"opacity":1});
		show(e.data);
		return false;
	}
	
	/**
	* Hides the blowup.
	*/
	function hideBlowup() {
		jQuery(elContainer).animate({"opacity":0},defaults.timeHideBlowup,defaults.easingHideBlowup,function(){elContainer.hide();});
		return false;
	}
	
	/**
	* Shows the blowup.
	*/
	function show(i) {
		// check
		if (i >= 0 && i < blowupItemTotal) {
			// show blowup
			elContainer.show();
			
			// index
			blowupItemIndex = i;
			
			// item
			var blowupItem = blowupItems[blowupItemIndex];
			
			// remove data
			var bData = jQuery("#bData").remove();
			
			// update info
			var t = jQuery(blowupItem).attr("title");
			elTitle.html(t);
			elCounter.html(blowupItemIndex+1);
			elTotal.html(blowupItemTotal);
			showNextPrev();
			

		
			// type
			var h = jQuery(blowupItem).attr("href");
			var hType = h.substring(h.length-3,h.length);
		
		
			// images
			if (hType == "jpg" || hType == "png" || hType == "gif") {
				
				// description
				var d = jQuery("img",blowupItem).attr("alt");
				elDescription.html(d);
							
				// preload
				var imgPreloader = new Image();
				imgPreloader.onload = function(){	
					// reset
					imgPreloader.onload = null;
					
					// data
					var markup = "<img id='bData' src='"+h+"' />";
					
					// append
					jQuery(elContent).append(markup);
					jQuery("#bData").hide();
			
					// resize 
					blowupSize.width = imgPreloader.width;
					blowupSize.height = imgPreloader.height;
					resizeBlowup();				
				}
				imgPreloader.src = h;
			}
			// applet
			else if (hType == "jar") {
				
				// description
				var d = jQuery(blowupItem).attr("caption");
				elDescription.html(d);
				
				// params: blowup_width=600,blowup_height=400,blowup_appletcode=cloud_draft01
				var params = jQuery(blowupItem).attr("params");
				
				
				var appletArchive = h;
				var appletCode = getParameter("blowup_appletcode",params);
				var appletWidth = parseInt(getParameter("blowup_width",params));
				var appletHeight = parseInt(getParameter("blowup_height",params));
				 
				// markup
				var markup = "<div id='bData'>";
				if (! browserIE) {
					markup += "<object classid='java:"+appletCode+"' type='application/x-java-applet' archive='"+appletArchive+"' width='"+appletWidth+"' height='"+appletHeight+"' standby='Loading Applet...' >";
					markup += "<param name='archive' value='"+appletArchive+"' />";
					markup += "<param name='mayscript' value='true' />";
					markup += "<param name='scriptable' value='true' />";
				}
				markup += "<object classid='clsid:8AD9C840-044E-11D1-B3E9-00805F499D93' codebase='http://java.sun.com/update/1.5.0/jinstall-1_5_0_15-windows-i586.cab' width='"+appletWidth+"' height='"+appletHeight+"' standby='Loading Applet...'  >";
				markup += "<param name='code' value='"+appletCode+"' />";
				markup += "<param name='archive' value='"+appletArchive+"' />";
				markup += "<param name='mayscript' value='true' />";
				markup += "<param name='scriptable' value='true' />";
				markup += "<div id='message'><p>Nothing here? Well, something went wrong. Try installing the latest <a href='http://www.java.com/en/download/' title='Download JRE' target='_blank'>Java Runtime Environment (JRE)</a> and use <a href='http://www.mozilla.com/en-US/firefox/all.html' title='Download Firefox' target='_blank'>Firefox</a>. Or just shut down your computer - it's nice outside! Technology is anyway overrated.</p></div>";
				markup += "</object>";
				if (! browserIE) {
					markup +="</object>";	
				}
				markup +="</div>";
				
				// append
				jQuery(elContent).append(markup);
				jQuery("#bData").hide();
		
				// resize 
				blowupSize.width = appletWidth;
				blowupSize.height = appletHeight;
				resizeBlowup();	
			}
			

		}
		
			
		// return 
		return false;
	}
	
	/*
	* Gets a parameter.
	*/
	function getParameter(parameter, parameters) {
		var value = "";
		var parameterArray = parameters.split(',');
		var compareString = parameter+'=';
		var compareLength = compareString.length;
		for (var i = 0; i < parameterArray.length; i++) {
			if (parameterArray[i].substr(0, compareLength) == compareString) {
				var currentParameter = parameterArray[i].split('=');
				value = currentParameter[1];
				break;
			}
		}
		return value;
	}
	
	/**
	* Resizes the blowup.
	*/
	function resizeBlowup() {
		// calculate padding
		var pTop = jQuery(elContent).css("padding-top");
		if (pTop.indexOf('px') >= 0) {
			pTop = parseFloat(pTop.substring(0,pTop.indexOf('p')));
		}
		var pBottom = jQuery(elContent).css("padding-bottom");
		if (pBottom.indexOf('px') >= 0) {
			pBottom = parseFloat(pBottom.substring(0,pBottom.indexOf('p')));
		}
		var paddingVertical = pTop + pBottom;
	
		var pLeft = jQuery(elContent).css("padding-left");
		if (pLeft.indexOf('px') >= 0) {
			pLeft = parseFloat(pLeft.substring(0,pLeft.indexOf('p')));
		}
		var pRight = jQuery(elContent).css("padding-right");
		if (pRight.indexOf('px') >= 0) {
			pRight = parseFloat(pRight.substring(0,pRight.indexOf('p')));
		}
		var paddingHorizontal = pLeft + pRight;
		
		// size
		var wWidth = jQuery(window).width();
		var wHeight = jQuery(window).height();
		var nWidth = blowupSize.width+paddingHorizontal;
		var nHeight = blowupSize.height+paddingVertical;
		
		// resize content
		if (nWidth > wWidth-2*defaults.blowupMargin) {
			var dWidth = nWidth - wWidth;
			var rWidth = blowupSize.width - dWidth - 2*defaults.blowupMargin;
			var r = rWidth / blowupSize.width;
			blowupSize.width = rWidth;
			blowupSize.height = blowupSize.height * r;
		}
		if (nHeight > wHeight-2*defaults.blowupMargin) {
			var dHeight = nHeight - wHeight;
			var rHeight = blowupSize.height - dHeight - 2*defaults.blowupMargin;
			var r = rHeight / blowupSize.height;
			blowupSize.height = rHeight;
			blowupSize.width = blowupSize.width * r;
		}
		nWidth = blowupSize.width+paddingHorizontal;
		nHeight = blowupSize.height+paddingVertical;
		jQuery("#bData").css({width:blowupSize.width+"px",height:blowupSize.height+"px","overflow":'hidden'});
		
		// margin
		var ml = nWidth / 2;
		var mt = nHeight / 2;

		// animate
		if (browserIE6 ) { // take away IE6
			elWindow.css("margin-left",-ml+"px");
		}
		else {
			jQuery(elWindow).animate(
			{"marginTop":-mt,"marginLeft":-ml,"width":nWidth,"height":nHeight},
			defaults.timeAnimate,
			defaults.easingAnimate,
			function(){
				jQuery("#bData").css({"display":'block',"opacity":0});
				jQuery("#bData").animate({"opacity":1.0},defaults.timeFadeIn,defaults.easingFadeIn);
			});
		}
	}
	
	
	/**
	* Showes the next prev.
	*/
	function showNextPrev() {
		if (blowupItemIndex < blowupItemTotal-1) {
			elNext.fadeIn(300);
		}
		else {
			elNext.fadeOut(300);
			elWindow.css("cursor",'default');
		}
		if (blowupItemIndex > 0) {
			elPrevious.fadeIn(300);
		}
		else {
			elPrevious.fadeOut(300);
			elWindow.css("cursor",'default');
		}
	}
	
	
	
	
	/**
	* Indicates the next prev.
	*/
	function indicateNextPrev(e) {
		
		// active
		if (isNext(e)) {
			elNext.addClass(defaults.classActive);
			elPrevious.removeClass(defaults.classActive);
			if (blowupItemIndex < blowupItemTotal-1) {
				elWindow.css("cursor",'pointer');
			}
			else {
				elWindow.css("cursor",'default');
			}
		}
		else {
			elPrevious.addClass(defaults.classActive);
			elNext.removeClass(defaults.classActive);
			if (blowupItemIndex != 0) {
				elWindow.css("cursor",'pointer');
			}
			else {
				elWindow.css("cursor",'default');
			}
		}
		
	}
	
	/**
	* Shows to the next prev.
	*/
	function gotoNextPrev(e) {
		if (isNext(e)) {
			gotoNext();
		}
		else {
			gotoPrevious();
		}
		return false;
	}
	
	/**
	* Indicates if next item.
	*/
	function isNext(e) {
		var n = false;
		var ww = jQuery(window).width();
		var mx = e.pageX ;
		if (mx > ww/2) {
			n = true;
		}
		return n;
	}
	
	/**
	* Goto next.
	*/
	function gotoNext() {
		show(blowupItemIndex+1);
		return false;
	}
	/**
	* Goto previous.
	*/
	function gotoPrevious() {
		show(blowupItemIndex-1);
		return false;
	}
		
	  
  
    // return
    return this;
};
