var OddBox = {

init: function(){
	this.settings = {
				//media string to play
				media:					'default.flv',
				mediaString:			'defaultstring',
				flash:					false,
				mediaType:				'mediatype',
				flvPlayerType:			'jw',
				// Configuration related to overlay
				overlayBgColor: 		'#000',		// (string) Background color to overlay; inform a hexadecimal value like: #RRGGBB. Where RR, GG, and BB are the hexadecimal values for the red, green, and blue values of the color.
				overlayOpacity:			0.8,		// (integer) Opacity value to overlay; inform: 0.X. Where X are number from 0 to 9
				// Configuration related to navigation
				fixedNavigation:		false,		// (boolean) Boolean that informs if the navigation (next and prev button) will be fixed or not in the interface.
				// Configuration related to images
				imageLoading:			'http://www.oddlystudios.com/images/oddly-loader.gif',		// (string) Path and the name of the loading icon
				imageBtnPrev:			'http://www.oddlystudios.com/images/light-prev.png',			// (string) Path and the name of the prev button image
				imageBtnNext:			'http://www.oddlystudios.com/images/light-next.png',			// (string) Path and the name of the next button image
				imageBtnClose:			'http://www.oddlystudios.com/images/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
				imageBlank:				'http://www.oddlystudios.com/images/lightbox-blank.gif',			// (string) Path and the name of a blank image (one pixel)
				// Configuration related to container image box
				containerBorderSize:	0,			// (integer) If you adjust the padding in the CSS for the container, #lightbox-container-image-box, you will need to update this value
				containerResizeSpeed:	400,		// (integer) Specify the resize duration of container image. These number are miliseconds. 400 is default.
				// Configuration related to texts in caption. For example: Image 2 of 8. You can alter either "Image" and "of" texts.
				txtImage:				'Image',	// (string) Specify text "Image"
				txtOf:					'of',		// (string) Specify text "of"
				// Configuration related to keyboard navigation
				keyToClose:				'c',		// (string) (c = close) Letter to close the jQuery lightBox interface. Beyond this letter, the letter X and the SCAPE key is used to.
				keyToPrev:				'p',		// (string) (p = previous) Letter to show the previous image
				keyToNext:				'n',		// (string) (n = next) Letter to show the next image.
				// Donīt alter these variables in any way
				imageArray:				[],
				activeImage:			0,
				contentWidth:			800,
				contentHeight: 			600,
				maxContentWidth: 		960,
				maxContentHeight: 		600
			};

	},
_start: function(mediatoplay, caption, widthC, heightC){
	this.settings.media = mediatoplay;
	if(widthC){
	this.settings.contentWidth = widthC;
	};
	if(heightC){
	this.settings.contentHeight = heightC;
	};

	this.media(mediatoplay);
	jQuery('embed, object, select').css({ 'visibility' : 'hidden' });
	this.settings.contentWidth = 250;
	this._set_interface();
	this._set_image_to_view();
},
_set_image_to_view: function(){ // show the loading
			
			// Show the loading
			jQuery('#lightbox-loading').show();
			
			if ( this.settings.fixedNavigation ) {
				jQuery('#lightbox-image,#lightbox-container-image-data-box,#lightbox-image-details-currentNumber').hide();
			} else {
				// Hide some elements
				jQuery('#lightbox-image,#lightbox-nav,#lightbox-nav-btnPrev,#lightbox-nav-btnNext,#lightbox-container-image-data-box,#lightbox-image-details-currentNumber').hide();
			}
			// Image preload process
			
			var objImagePreloader = new Image();
			that=this;
			objImagePreloader.onload = function() {
				if(that.settings.mediaType == "image"){
					that.settings.contentWidth = objImagePreloader.width;
					jQuery('#lightbox-image').attr('src', that.settings.mediaString);
					that._resize_container_image_box(objImagePreloader.width,objImagePreloader.height);
					
					// Perfom an effect in the image container resizing it
					
					//	clear onLoad, IE behaves irratically with animated gifs otherwise
					objImagePreloader.onload=function(){};
				};
				
				
			};
			if(this.settings.mediaType == "image"){
				objImagePreloader.src = this.settings.mediaString;
				
			}else{
				this.settings.contentWidth = 800;
				this._resize_container_image_box(this.settings.contentWidth,this.settings.contentHeight);

			}
		},

_resize_container_image_box: function(intImageWidth,intImageHeight){
			// Get current width and height
			var intCurrentWidth = jQuery('#lightbox-container-image-box').width();
			var intCurrentHeight = jQuery('#lightbox-container-image-box').height();
			// Get the width and height of the selected image plus the padding
			var intWidth = (intImageWidth + (this.settings.containerBorderSize * 2)); // Plus the imageīs width and the left and right padding value
			var intHeight = (intImageHeight + (this.settings.containerBorderSize * 2)); // Plus the imageīs height and the left and right padding value
			
			/*
			if( intWidth > 600){
				jQuery('#lightbox-image').width(600);
				var ratio = 600 / intCurrentWidth;
				var newHeight = intCurrentHeight * ratio;
				jQuery('#lightbox-image').height(newHeight);
				//intHeight = jQuery('#lightbox-container-image-box').css({ 'height' : 'auto'});
				//jQuery('#lightbox-image').width(intHeight);
				intWidth = (600 + (this.settings.containerBorderSize * 2));
				intHeight = (newHeight + (this.settings.containerBorderSize * 2));
				intImageWidth = 600;
				intImageHeight = intHeight;
			}
			*/
			
			
			if(intWidth > intHeight){
				if(intWidth > this.settings.maxContentWidth){
					ratio = this.settings.maxContentWidth / intWidth;   // get ratio for scaling image
					jQuery('#lightbox-image').width(this.settings.maxContentWidth); // Set new width

					
					intImageWidth = this.settings.maxContentWidth;
					intWidth = (this.settings.maxContentWidth + (this.settings.containerBorderSize * 2));
					intHeight =(intHeight * ratio + (this.settings.containerBorderSize * 2));
					intImageHeight = (intHeight - (this.settings.containerBorderSize * 2));
				}else{
					//do nothing
				}	
			}else{
			
				if(intHeight > this.settings.maxContentHeight){
					ratio = this.settings.maxContentHeight / intHeight;   // get ratio for scaling image
					jQuery('#lightbox-image').height(this.settings.maxContentHeight); // Set new height
					intImageHeight = this.settings.maxContentHeight;
					intHeight = (this.settings.maxContentHeight + (this.settings.containerBorderSize * 2));
					intWidth =(intWidth * ratio + (this.settings.containerBorderSize * 2));
					intImageWidth = (intWidth - (this.settings.containerBorderSize * 2));
				}else{
					//do nothing
				}
			}
			
			// Diferences
			var intDiffW = intCurrentWidth - intWidth;
			var intDiffH = intCurrentHeight - intHeight;
			var arrPageScroll = this.___getPageScroll();
			var arrPageSizes = this.___getPageSize();
			// Calculate top and left offset for the jquery-lightbox div object and show it

			

			// Perfomance the effect
			var that = this;
			jQuery('#lightbox-container-image-box').animate({ width: intWidth, height: intHeight },this.settings.containerResizeSpeed,function(){that._show_image()});
			if ( ( intDiffW == 0 ) && ( intDiffH == 0 ) ) {
				if ( jQuery.browser.msie ) {
					this.___pause(250);
				} else {
					this.___pause(100);	
				}
			}

			
			//jQuery('#lightbox-container-image-data-box').css({ width: intImageWidth });
			//jQuery('lightbox-nav').css({ height: intImageHeight + (this.settings.containerBorderSize * 2) });
			//jQuery('#lightbox-nav-btnPrev,#lightbox-nav-btnNext').css({ height: intImageHeight + (this.settings.containerBorderSize * 2) });
			
			jQuery('#jquery-lightbox').animate({
				top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
				//left:	arrPageScroll[0] + ((arrPageSizes[2] - this.settings.contentWidth)/2)}, 
				left:	(jQuery('#jquery-overlay').width() / 2) - (intWidth /2) }, 
				this.settings.containerResizeSpeed
			);
			
		},
_set_interface: function(){

			// Apply the HTML markup into body tag
			jQuery('body').append('<div id=jquery-overlay></div><div id=jquery-lightbox><div id=lightbox-container-image-data-box><div id=lightbox-container-image-data><div id=lightbox-image-details><span id=lightbox-image-details-caption></span><span id=lightbox-image-details-currentNumber></span></div><div id=lightbox-secNav><a href="#" id=lightbox-secNav-btnClose><img src="' + this.settings.imageBtnClose + '"></a></div></div></div><div id=lightbox-container-image-box><div id=lightbox-container-image><img id=lightbox-image><div id=lightbox-nav><a href="#" id=lightbox-nav-btnPrev></a><a href="#" id=lightbox-nav-btnNext></a></div><div id=lightbox-loading><a href="#" id=lightbox-loading-link><img src="' + this.settings.imageLoading + '"></a></div></div></div></div>');	
			// Get page sizes

			var arrPageSizes = this.___getPageSize();
			// Style overlay and show it
			jQuery('#jquery-overlay').css({
				backgroundColor:	this.settings.overlayBgColor,
				opacity:			this.settings.overlayOpacity,
				width:				arrPageSizes[0],
				height:				arrPageSizes[1]
			}).fadeIn();
			// Get page scroll
			var arrPageScroll = this.___getPageScroll();
			// Calculate top and left offset for the jquery-lightbox div object and show it
			jQuery('#jquery-lightbox').css({
				top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
				left:	arrPageScroll[0] + ((arrPageSizes[2] - this.settings.contentWidth)/2)
			}).show();
			
			//set number of images count
			var totlb = jQuery('.oddBoxLink').size();
			var currhref = this.settings.media;
			jQuery('.oddBoxLink').each(function(index) {
				if( jQuery(this).attr('href') == currhref ){
					//console.log(index);
					nextslidehref = jQuery(this).attr('href');
					thislb = index + 1;
				}
			});
			
			//console.log(nextslidehref);
			jQuery('#lightbox-image-details-currentNumber').text('Image:' + thislb + 'of' + totlb);
			
			// Assigning click events in elements to close overlay
			var that = this;
			jQuery('#jquery-overlay').click(function() {
				that._finish();									
			});
			
			jQuery('#lightbox-nav-btnNext').click(function() {
				vidPath = nextslidehref;
				that._start(vidPath, "something", this.settings.contentWidth, this.settings.contentHeight );
				return false;
					//$('html, body').css({'overflow' : 'hidden'});
			});
			
			// Assign the _finish function to lightbox-loading-link and lightbox-secNav-btnClose objects
			jQuery('#lightbox-loading-link,#lightbox-secNav-btnClose').click(function() {
				that._finish();
				return false;
			});
			// If window was resized, calculate the new overlay dimensions
			jQuery(window).resize(function() {
				// Get page sizes
				var arrPageSizes = that.___getPageSize();
				// Style overlay and show it
				jQuery('#jquery-overlay').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});
				// Get page scroll
				var arrPageScroll = that.___getPageScroll();
				// Calculate top and left offset for the jquery-lightbox div object and show it
				jQuery('#jquery-lightbox').css({
					top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
					left:	arrPageScroll[0] + ((arrPageSizes[2] - that.settings.contentWidth)/2)
				});
				
			});
		},
_show_image: function(){
	//because _show_image is a callback handler, 'this' is set to the div element by jquery

			jQuery('#lightbox-loading').hide();
			//show close button
			//jQuery('#lightbox-image, #lightbox-nav,#lightbox-nav-btnPrev, #lightbox-nav-btnNext, #lightbox-container-image-data-box, #lightbox-image-details-currentNumber').show();
			//////////////////////////////////////////////make a quick swap/////////////////////////
			if(this.settings.mediaType == "youtube"){
				jQuery('#lightbox-container-image').html(this.settings.mediaString);
			}
			if(this.settings.mediaType=="mp4player"){
				//set the html5 fallback
				var fallBackString = "<div id='fiveVid'><video width='"+this.settings.contentWidth+"' height='"+this.settings.contentHeight+"' controls><source src='"+this.settings.mediaString+"' type='video/mp4' /></video></div>";

				jQuery('#lightbox-image').replaceWith(fallBackString);
				//right now ff will break because no ogg version :(

				//flash version for non-html5 people
				var swfName = ("http://www.oddlystudios.com/swf/player.swf?file="+this.settings.mediaString+"&autostart=true");
				var flashvars = {
				};
				var params = {
				  wmode: "opaque"
				};
				var attributes = {
				  id: "myOddboxContent",
				  name: "myOddboxContent"
				};

				swfobject.embedSWF(swfName,  "fiveVid", this.settings.contentWidth, this.settings.contentHeight, "9.0.0","swf/playerProductInstall.swf",flashvars, params, attributes);
			}
			
			if(this.settings.mediaType == "flvplayer"){
				var swfName = (this.settings.mediaString);
				var flashvars = {
				  
				};
				var params = {
				  wmode: "opaque"
				};
				var attributes = {
				  id: "myOddboxContent",
				  name: "myOddboxContent"
				};

				swfobject.embedSWF(swfName,  "lightbox-image", this.settings.contentWidth, this.settings.contentHeight, "9.0.0","swf/playerProductInstall.swf",flashvars, params, attributes);
			};
			if(this.settings.mediaType == "image"){
				//do nothing
			};
			if(this.settings.mediaType == "quicktime"){
				jQuery('#lightbox-container-image').html(this.other);
			};
			///////////////////////////////////////////////////////////////////////////////////////////
			var that = this;
			jQuery('#lightbox-image').fadeIn('slow', function(){that._show_image_data()});
		},
_show_image_data: function(){
			//alert("showing image data");
			//jQuery('#lightbox-container-image-data-box').slideDown('fast');
			//jQuery('#lightbox-image-details-caption').hide();
			//	jQuery('#lightbox-image-details-caption').html(settings.imageArray[settings.activeImage][1]).show();
		},
_finish: function(){
			jQuery('#jquery-lightbox').remove();
			jQuery('#jquery-overlay').fadeOut(function() { jQuery('#jquery-overlay').remove(); });
			// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
			jQuery('embed, object, select').css({ 'visibility' : 'visible' });
		},
		/**
		 / THIRD FUNCTION
		 * getPageSize() by quirksmode.com
		 *
		 * @return Array Return an array with page width, height and window width, height
		 */
 ___getPageSize: function(){
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				//if using wrapper instead of body, use this. - added by adamjang
				if(document.body.scrollHeight > document.getElementById('wrapper').scrollHeight){
					xScroll = document.getElementById('wrapper').scrollWidth;
					yScroll = document.getElementById('wrapper').scrollHeight;
				}else{
					xScroll = document.body.scrollWidth;
					yScroll = document.body.scrollHeight;
				}
				
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			

					xScroll = document.body.offsetWidth;
					yScroll = document.body.offsetHeight;			

			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		},
		/**
		 / THIRD FUNCTION
		 * getPageScroll() by quirksmode.com
		 *
		 * @return Array Return an array with x,y page scroll values.
		 */
___getPageScroll: function(){
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		},
___pause: function(ms){
			var date = new Date(); 
			curDate = null;
			do { var curDate = new Date(); }
			while ( curDate - date < ms);
		 },
/////////////////////////////////////////////////////////////put this in another file///////////////////////////////////
media: function(sLinkHref){
		if (sLinkHref.match(/youtube\.com\/watch/i)) {
			this.settings.flash = false;
			this.settings.mediaType="youtube";
			var hRef = sLinkHref;
			var videoId = hRef.split('=');
			this.videoID = videoId[1];
			//this.settings.mediaString = "http://www.youtube.com/v/"+this.videoID+"&autoplay=1";
			this.settings.mediaString = "<object width='"+this.settings.contentWidth+"' height='"+this.settings.contentHeight+"'><param name='movie' value='http://www.youtube.com/v/"+this.videoID+"?fs=1&amp;autoplay=1&amp;hl=en_US&amp;rel=0&amp;color1=0x3a3a3a&amp;color2=0x999999'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src='http://www.youtube.com/v/"+this.videoID+"?fs=1&amp;autoplay=1&amp;hl=en_US&amp;rel=0&amp;color1=0x3a3a3a&amp;color2=0x999999' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='"+this.settings.contentWidth+"' height='"+this.settings.contentHeight+"'></embed></object>";
		}
		else if (sLinkHref.match(/\.mov/i)) {
			this.settings.mediaType="quicktime";
			this.settings.flash = false;
			if (navigator.plugins && navigator.plugins.length) {
				  this.other ='<object id="qtboxMovie" type="video/quicktime" codebase="http://www.apple.com/qtactivex/qtplugin.cab" data="'+sLinkHref+'" width="'+this.settings.contentWidth+'" height="'+this.settings.contentHeight+'"><param name="src" value="'+sLinkHref+'" /><param name="scale" value="aspect" /><param name="controller" value="true" /><param name="autoplay" value="true" /><param name="bgcolor" value="#000000" /><param name="enablejavascript" value="true" /></object>';
			  } else {
				this.other = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="'+this.settings.contentWidth+'" height="'+this.settings.contentHeight+'" id="qtboxMovie"><param name="src" value="'+sLinkHref+'" /><param name="scale" value="aspect" /><param name="controller" value="true" /><param name="autoplay" value="true" /><param name="bgcolor" value="#000000" /><param name="enablejavascript" value="true" /></object>';
			  }
		}
		//support for flv
		else if (sLinkHref.match(/\.flv/i)) {
		 this.settings.flash = true;
		  this.settings.mediaType="flvplayer";
			switch(this.settings.flvPlayerType){
				case "oddly":
				this.settings.mediaString = "swf/VideoModule.swf?videopath="+sLinkHref;
				break;
				case "jw":
				this.settings.mediaString = "swf/player.swf?file="+sLinkHref+"&autostart=true";
				break;
			}
		}
		//support for mp4
		else if (sLinkHref.match(/\.mp4/i)) {
		 this.settings.flash = true;
		  this.settings.mediaType="mp4player";
			switch(this.settings.flvPlayerType){
				case "oddly":
				this.settings.mediaString = "swf/VideoModule.swf?videopath="+sLinkHref;
				break;
				case "jw":
				this.settings.mediaString = sLinkHref;
				break;
			}
		}
		else if (sLinkHref.match(/\.jpg/i) || sLinkHref.match(/\.png/i)) {
		this.settings.flash = false;
		this.settings.mediaType = "image";
		this.settings.mediaString = sLinkHref;
		}
		else {
		  this.settings.flash = true;
		  this.settings.mediaType="flvplayer";
			//this.videoID = sLinkHref;
			this.settings.mediaString = "swf/VideoModule.swf?videopath=video/kfc_medquality.flv";
			//this.so = new SWFObject(this.videoID, "flvvideo", this.options.contentsWidth, this.options.contentsHeight, "0");
		}
	}
	};

