/*******************************************************************************





THIS IS UNFINISHED SOFTWARE!!!!





/**
 * Openbox 1.01 - jQuery Plugin
 *
 * Copyright (c) 2008 David R. Casey, http://studiocasey.com
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * 
 * This implementation is based heavily off of Cody Lindley's Thickbox 
 * (http://jquery.com/demo/thickbox/) and Kevin Miller's Lightwindow
 * (http://stickmanlabs.com/lightwindow). The primary goal was to port the
 * functionality of Lightwindow to jQuery and add extensibility to Thickbox.
 * See docs for additional details.
 *
 * @author David R. Casey, http://studiocasey.com
 * @version 1.01
 * @requires jQuery v1.2.6, jquery.media.js v0.81
 *
 */
 
(function($) { // jQuery closure

/**
 * Openbox plugin definition
 * add openbox to href & area elements that have a class of .openbox
 * @param options Hash values to override the defaults
 * @param options Hash values to override the defaults
 */
$.fn.openbox = function(options, callback) {
	if (typeof(options) != 'object') {
		callback = options;
		options = {};
	}
	openbox.opts = $.extend({}, $.fn.openbox.defaults, options||{});
	
	// build the data structure
	$(this).each(function() {
		var i = 0;
		if (!this.rel) { // if not in a collection, append at the end
			i = openbox.bundle.length;
		} else { // find existing collection or append at the end if it doesn't already exist
			for (i=0; i < openbox.bundle.length; i++) {
				if (!openbox.bundle[i][0] || this.rel == openbox.bundle[i][0].collection) break;
			}
		}
		if (!openbox.bundle[i]) openbox.bundle.push([]);
		
		openbox.bundle[i].push({
			'$this'      : this,
			'title'      : this.title || this.name || '',
			'url'        : this.href  || this.alt,
			'collection' : this.rel   || false,
			'params'     : $.extend(new cloneObj(openbox.opts.params), eval('('+$(this).attr('params')+')') || {})
		});

		imgPreloader = new Image();
		imgPreloader.src = openbox.opts.loadingAnimation;
	});
	
	// show openbox when openbox object is clicked
	$(this).click(function() {
		$.fn.openbox.showOpenbox(this);
		this.blur();
		return false;
	});
	
	$(callback);
	return this;
}; // end $.fn.openbox

/*******************************************************************************
 * PRIVATE FUNCTIONS / VARIABLES
 */
var openbox = {
	opts : {},
	bundle: [],
	hasNext: false,
	hasPrev: false
};
/**
 * Initializes the common HTML for Openbox
 */
function initMarkup() {
	if (typeof document.body.style.maxHeight === 'undefined') { // if IE 6
		$('body','html').css({height: '100%', width: '100%'});
		$('html').css('overflow','hidden');
		if (document.getElementById('openbox_HideSelect') === null) { // iframe to hide select elements in IE 6
			$('body').append('<iframe id="openbox_HideSelect"></iframe><div id="openbox_overlay"></div>');
			$('#openbox_overlay').click(removeOpenbox);
		}
	} else {//all others
		if (document.getElementById('openbox_overlay') === null) {
			$('body').append('<div id="openbox_overlay"></div>');
			$('#openbox_overlay').click(removeOpenbox);
		}
	}
	if (detectMacXFF()) {
		$('#openbox_overlay').addClass('openbox_overlayMacFFBGHack');// use png overlay to hide flash
	}
	$('body').append(openbox.opts.skin);
	$('#openbox_loading img').attr('src',openbox.opts.loadingAnimation);
	$('#openbox_loading a').click(removeOpenbox);
	$('#openbox_ImageOff').click(removeOpenbox);
	$('#openbox_titlebar_close').click(removeOpenbox);
	if (openbox.opts.navigation) enableNavigation();
};
/**
 *
 */
function showImage(obj) {
	imgPreloader = new Image();
	imgPreloader.src = obj.url;
	imgPreloader.onload = function() {	
		imgPreloader.onload = null;
		// Resizing
		obj.params.width = imgPreloader.width
		obj.params.height = imgPreloader.height
		contentSize = resizeContents(obj, true);
		contentWidth = contentSize[0];
		contentHeight = contentSize[1];
		
		$('#openbox_contents').html('<a href="" id="openbox_ImageOff" title="Close"><img id="openbox_Image" src="'+obj.url+'" width="'+contentWidth+'" height="'+contentHeight+'" alt="'+obj.title+'"/></a>');
		
		if (contentSize[0] < $('#openbox_loading img').width() || 
				contentSize[1] < $('#openbox_loading img').height()) $('#openbox_loading img').hide();
		$.fn.openbox.postTransition(contentSize);
	}
}
/**
 *
 */
function showMedia(obj) {
	obj.params.src = obj.url;
	$('#openbox_contents').media(obj.params);
		if (obj.params.width < $('#openbox_loading img').width() || 
				obj.params.height < $('#openbox_loading img').height()) $('#openbox_loading').slideUp();
	$.fn.openbox.postTransition([obj.params.width, obj.params.height]);
}
/**
 *
 */
function showPage(obj) {
	contentSize = resizeContents(obj, obj.params.proportions);
	contentWidth = contentSize[0];
	contentHeight = contentSize[1];
		
	obj.params.src = obj.url;
	var o = $('<iframe' + ' width="' + contentWidth + '" height="' + contentHeight + '" >');
	o.attr('src', obj.url);
	$('#openbox_contents').append(o);
	
	$.fn.openbox.postTransition([contentWidth+4, contentHeight+4], function() {
																																							$('#openbox_navigation').hide() });
}
/**
 * showOpenboxObject()
 */
function showOpenboxObject(i,j) {
	$.fn.openbox.preTransition(function() {

		var obj = openbox.bundle[i][j];
		
		if (openbox.opts.navigation) { 
			$.fn.openbox.buildNavigation(i,j);
			$('#openbox_data_gallery_current').html(j+1);
			$('#openbox_data_gallery_total').html(openbox.bundle[i].length);
		}
		$('#openbox_titlebar_title').html(obj.title);
		$('#openbox_data_caption').html(obj.params.caption);
		$('#openbox_data_author').html(obj.params.author);

		if (openbox.opts.googleanalytics) {
			try { // GOOGLE ANALYTICS
				var pageTracker = _gat._getTracker(openbox.opts.googleanalytics);
				pageTracker._trackPageview(obj.url);
			} catch(err) {/*alert('Google Analytics Failed: '+openbox.opts.googleanalytics+'\n'+obj.url);*/}
		}

		//alert(obj.url);
		switch(getFileType(obj.url)) {
			case 'image':
				showImage(obj);
				break;
			case 'media':
				showMedia(obj);
				break;
			case 'page':
				showPage(obj);
				break;
			default:
				alert('Filetype not recognized!\n'+obj.url);
				removeOpenbox();
				break;
		}
	});
};
/**
 * Enables navigation by setting the background hover and onclick
 */
function enableNavigation() {
	$('#openbox_prev').hover(function() {
		$(this).css({'background':openbox.opts.prevHover});
	},function() {
		$(this).css({'background':openbox.opts.prev});
	});
	
	$('#openbox_next').hover(function() {
		$(this).css({'background':openbox.opts.nextHover});
	},function() {
		$(this).css({'background':openbox.opts.next});
	});	
};	
/**
 * Navigates from the current object to the desired object
 * @param current The index of the current object in the collection
 * @param move The number of indices to move from the current index, positive or negative
 */
function navigate(i,current,move) {
	try {
		$('#openbox_contents embed').Stop();
	} catch(e) {}
	var size = openbox.bundle[i].length;
	showOpenboxObject( i, ((move+current)%size+size)%size );
};
/**
 * Get the type of file
 * @param url The URL in question
 * @return The type of file referenced in the passed URL
 */
function getFileType(url) {
	var image = new RegExp("[^\.]\.("+openbox.opts.fileTypes.image.join('|')+")\s*$", "i");
	if (image.test(url)) return 'image';
	if (url.indexOf('#') > -1 && (document.domain == getDomain(url))) return 'inline';		
	if (url.indexOf('?') > -1) url = url.substring(0, url.indexOf('?'));
	var type = 'unknown';
	var page = new RegExp("[^\.]\.("+openbox.opts.fileTypes.page.join('|')+")\s*$", "i");
	var media = new RegExp("[^\.]\.("+openbox.opts.fileTypes.media.join('|')+")\s*$", "i");
	if (document.domain != getDomain(url)) type = 'page';
		if (media.test(url)) type = 'media';
	if (type == 'page' || type == 'media') return type;
		if (page.test(url) || url.substr((url.length-1), url.length) == '/') type = 'page';
	return type;
};
/**
 * Get the domain from a string
 * @param url The URL in question
 * @return The domain in the passed URL
 */
function getDomain(url) {    
	var leadSlashes = url.indexOf('//');
	var domainStart = leadSlashes+2;
	var withoutResource = url.substring(domainStart, url.length);
	var nextSlash = withoutResource.indexOf('/');
	var domain = withoutResource.substring(0, nextSlash);
	if (domain.indexOf(':') > -1){
		var portColon = domain.indexOf(':');
		domain = domain.substring(0, portColon);
	}
	return domain;
};
/**
 * @param obj The object to be resized
 * @param prop Boolean true - resize proportionaltely, false - resize as needed
 * @return The array containing width and height to resize
 */
function resizeContents(obj, prop) {
	var pagesize = getPageSize();
	var x = pagesize[0] - openbox.opts.bufferX;
	var y = pagesize[1] - openbox.opts.bufferY;
	var objWidth = obj.params.width;
	var objHeight = obj.params.height;
	if (objWidth > x) {
		if (prop) objHeight = objHeight * (x / objWidth);
		objWidth = x;
		if (objHeight > y) {
			if (prop) objWidth = objWidth * (y / objHeight);
			objHeight = y;
		}
	} else if (objHeight > y) {
		if (prop) objWidth = objWidth * (y / objHeight);
		objHeight = y;
		if (objWidth > x) {
			if (prop) objHeight = objHeight * (x / objWidth);
			objWidth = x;
		}
	}
	return [objWidth, objHeight];
};
/**
 * getPageSize()
 */
function getPageSize() {
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}
/**
 * removeOpenbox()
 */
function removeOpenbox() {
	$('#openbox_imageOff').unbind('click');
	$('#openbox_titlebar_close').unbind('click');
	$('#openbox_window').fadeOut('fast',function() {$('#openbox_window,#openbox_overlay,#openbox_HideSelect').trigger('unload').unbind().remove();});
	$('#openbox_loading').remove();
	if (typeof document.body.style.maxHeight == 'undefined') {//if IE 6
		$('body','html').css({height: 'auto', width: 'auto'});
		$('html').css('overflow','');
	}
	document.onkeydown = '';
	document.onkeyup = '';
	
	$(document).trigger('removeOpenbox');
	return false;
}
/**
 * detectMacXFF()
 */
function detectMacXFF() {
	var userAgent = navigator.userAgent.toLowerCase();
	if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
		return true;
	}
}
/**
 * Creates a clone of the Object passed using recursion, allowing a deep copy-by-value
 * @param s The object to be cloned
 */
function cloneObj(s) {
	for(p in s)
		this[p] = (typeof(s[p]) == 'object')? new cloneObj(s[p]) : s[p];
}

/*******************************************************************************
 * PUBLIC FUNCTIONS / VARIABLES
 */
/**
 * defaults
 */
$.fn.openbox.defaults = {
	googleanalytics: false,
	resize: true,
	navigation: true,
	loop: true,
	bufferX: 150,
	bufferY: 150,
	padding: 10,
	next: 'none',
	prev: 'none',
	nextHover: 'url(/wp-content/uploads/openbox/images/nextlabel.gif) right 15% no-repeat',
	prevHover: 'url(/wp-content/uploads/openbox/images/prevlabel.gif) left 15% no-repeat',
	loadingAnimation: '/wp-content/uploads/openbox/images/ajax-loading.gif',
	skin:
			'<div id="openbox_window">'+
				'<div id="openbox_titlebar">'+
					'<span id="openbox_titlebar_title"></span>'+
					'<a href="#" id="openbox_titlebar_close" >close</a>'+
				'</div>'+
				'<div id="openbox_stage">'+
					'<div id="openbox_contents"></div>'+
					'<div id="openbox_loading"><img alt="loading" /><span>Loading or <a href="#">Cancel</a></span></div>'+
				'</div>'+
				'<div id="openbox_navigation">'+
						'<a id="openbox_prev" ><span id="openbox_prev_title"></span></a>'+
						'<a id="openbox_next" ><span id="openbox_next_title"></span></a>'+
						'<iframe name="openbox_navigation_shim" id="openbox_navigation_shim"></iframe>'+
				'</div>'+
				'<div id="openbox_data_slide" >'+
					'<div id="openbox_data_slide_inner" >'+
						'<div id="openbox_data_details" >'+
							'<div id="openbox_data_gallery_container" >'+
								'<span id="openbox_data_gallery_current"></span>'+
								'&nbsp;of&nbsp;'+
								'<span id="openbox_data_gallery_total"></span>'+
							'</div>'+
							'<div id="openbox_data_author_container" >'+
								'<span id="openbox_data_author"></span>'+
							'</div>'+
						'</div>'+
						'<div id="openbox_data_caption" ></div>'+
					'</div>'+
				'</div>'+
			'</div>',
	fileTypes: {
		page: ['asp','aspx','cgi','cfm','htm','html','pl','php4','php3','php','php5','phtml','rhtml','shtml','txt','vbs','rb'],
		media: ['aif','aiff','aac','au','eps','gsm','mov','mid','midi','mpg','mpeg','mp4','m4a','psd','qt','qtif','qif','qti','snd','tif','tiff','wav','3g2','3pg','flv','mp3','swf','asf','avi','wma','wmv','ra','ram','rm','rpm','rv','smi','smil','xaml','pdf'],
		image: ['bmp','gif','jpg','png']
	},
	params: {
		autoplay: false,
		proportions: true,
		caption: false,
		author: false
	}
}
/**
 * Clears (resets) the openbox bundle
 */
$.fn.openbox.clearOpenbox = function() {
	openbox.bundle = [];
}
/**
 * Shows openbox
 * If you call this externally, you need run it through the callback
 * to ensure the data structure has been built.
 * @param obj The object to show.
 */
$.fn.openbox.showOpenbox = function(obj) {
	if (obj != null) {
		initMarkup();
		var found = false;
		for (var i=0; i < openbox.bundle.length; i++) {
			if ($(obj)[0].rel == openbox.bundle[i][0].collection) {
				for (var j=0; j < openbox.bundle[i].length; j++) {
					if ($(obj)[0] == openbox.bundle[i][j].$this) {
						found = true;
						break;
					}
				}
				if (found) break;
			}
		}
		showOpenboxObject(i,j);
	}
	return obj;
}
/**
 * Transition to display loading
 * show: loading, hide: contents
 * @param callback The callback function once transition is complete.
 */
$.fn.openbox.preTransition = function(callback) {
	$('#openbox_loading').show();
	$('#openbox_navigation').hide();
	$('#openbox_contents').hide();
	if ($('#openbox_data_slide').css('display') == 'block') { 
		$('#openbox_data_slide').slideUp('fast');
		$('#openbox_titlebar').animate({marginTop: '1.7em'}, 'fast', function() {
				$(this).hide();
				return $(callback);
		});
	} else {
		return $(callback);
	}
}
/**
 * Transition to display new openbox object
 * animate openbox, fadeIn contents, fadeOut loading
 * @param contentSize An array containing [0] = width, [1] = height of the content
 */
$.fn.openbox.postTransition = function(contentSize, callback) {
	w = contentSize[0]; //$('#openbox_contents').width();
	h = contentSize[1]; //$('#openbox_contents').height();
	
	// Hidden Elements: set size of contents, titlebar, data, and navigation. set marginTop of data
	$('#openbox_contents').css({width: w, height: h});
	$('#openbox_data_slide').css({width: w+openbox.opts.padding*2, marginTop: h+openbox.opts.padding*3+'px'});
	$('#openbox_titlebar').css({width: w+openbox.opts.padding*2});
	$('#openbox_navigation').css({width: w+openbox.opts.padding*2, height: h+openbox.opts.padding-20});
	
	$('#openbox_window').animate({marginLeft: -w/2-openbox.opts.padding+'px'}, 'normal')
			.animate({marginTop: -h/2-50-openbox.opts.padding+'px'}, 'normal');
	$('#openbox_loading').animate({	width: w-openbox.opts.padding*2+'px'}, 'normal')
			.animate({height: h-openbox.opts.padding*3+'px'}, 'normal');
	$('#openbox_stage').animate({width: w+'px'	}, 'normal')
			.animate({height: h+'px'}, 'normal', function() { 
					// slide title & data after stage completes animation
					$('#openbox_titlebar').show().animate({marginTop: '0'}, 'normal');
					$('#openbox_data_slide').slideDown('normal', function() { 
							// fade swap loading for contents after title & data completes animation
							$('#openbox_loading').fadeOut('slow');
							$('#openbox_contents').fadeIn('slow', function() { 
									// finally show navigation
									$('#openbox_navigation').show(function() { return $(callback); });
							});
					});				
			});
}
/**
 * 
 */
$.fn.openbox.buildNavigation = function(i,j) {
	var size = openbox.bundle[i].length;
	// openbox.hasNext
	if (size > 1 && openbox.opts.loop || j < size - 1) {
		openbox.hasNext = true;
		$('#openbox_next').show().unbind('click').click(function() { navigate(i,j,1) });
	} else {
		openbox.hasNext = false;
		$('#openbox_next').hide();
	}
	// openbox.hasPrev
	if (size > 1 && openbox.opts.loop || 0 < j) {
		openbox.hasPrev = true;
		$('#openbox_prev').show().unbind('click').click(function() { navigate(i,j,-1) });
	} else {
		openbox.hasPrev = false;
		$('#openbox_prev').hide();
	}
	$('#openbox_prev, #openbox_next').blur();

	document.onkeydown = function(e) { 	
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}
		// TODO: if we want to eliminate cases programatically, we will need to do a regexp
		switch(keycode) {
			//close
			case 27:  // escape
				removeOpenbox();
				break;
			//next item in collection
			case 190: // comma/lessthan
			case 78:  // n/N
			case 39:  // right arrow
				if (openbox.hasNext) {
					document.onkeydown = '';
					navigate(i,j,1);
				} break;
			// previous item in collection
			case 188: // period/greaterthan
			case 80:  // p/P
			case 37:  // left arrow
				if (openbox.hasPrev) {
					document.onkeydown = '';
					navigate(i,j,-1);
				} break;
		}
	};
// End Navigate Collection
}










/*
$.fn.openbox.buildContents = function(index,obj) {
	$('#openbox_contents').html(
			$('<object>').attr({
					height:  '640px',
					width:   '360px',
					name:    'openbox_media_primary',
					id:      'openbox_media_primary',
					codebase: openbox.opts.codebases[getFileExtension(obj.url)],
					classid:  openbox.opts.classids[getFileExtension(obj.url)]
			}) 
	);
	$('#openbox_media_primary')
				.append( $('<param />').attr({value:obj.url, name:'movie'}) )
				.append( $('<param />').attr({value:obj.url, name:'src'}) )
				.append( $('<param />').attr({value:'true',  name:'controller'}) )
				.append( $('<param />').attr({value:'transparent',name:'wmode'}) )
				.append( $('<param />').attr({value:'false', name:'cache'}) )
				.append( $('<param />').attr({value:'high',  name:'quality'}) );
	
	if (!$.browser.msie) {
		$('#openbox_media_primary').append( 
				$('<object>').attr({
					height:  '100%',
					width:   '100%',
					name: 'openbox_media_secondary',
					id:   'openbox_media_secondary',
					data:  obj.url,
					type:  openbox.opts.mimeTypes[getFileExtension(obj.url)]
				}) 
		);
		$('#openbox_media_secondary')
				.append( $('<param />').attr({value:'true',  name:'controller'}) )
				.append( $('<param />').attr({value:'transparent',name:'wmode'}) )
				.append( $('<param />').attr({value:'false', name:'cache'}) )
				.append( $('<param />').attr({value:'high',  name:'quality'}) );
	} else {/*
		$('#openbox_media_primary')
				.append( $('<param />').attr({value:'5080', name:'_cx'}) )
				.append( $('<param />').attr({value:'5080', name:'_cy'}) )
				.append( $('<param />').attr({value:'', name:'FlashVars'}) )
				.append( $('<param />').attr({value:'-1', name:'Play'}) )
				.append( $('<param />').attr({value:'-1', name:'Loop'}) )
				.append( $('<param />').attr({value:'', name:'SAlign'}) )
				.append( $('<param />').attr({value:'-1', name:'Menu'}) )
				.append( $('<param />').attr({value:'', name:'Base'}) )
				.append( $('<param />').attr({value:'ShowAll', name:'AllowScriptAccess'}) )
				.append( $('<param />').attr({value:'0', name:'DeviceFont'}) )
				.append( $('<param />').attr({value:'0', name:'EmbedMovie'}) )
				.append( $('<param />').attr({value:'', name:'BGColor'}) )
				.append( $('<param />').attr({value:'', name:'SWRemote'}) )
				.append( $('<param />').attr({value:'', name:'MovieData'}) )
				.append( $('<param />').attr({value:'1', name:'SeamlessTabbing'}) )
				.append( $('<param />').attr({value:'0', name:'Profile'}) )
				.append( $('<param />').attr({value:'', name:'ProfileAddress'}) )
				.append( $('<param />').attr({value:'0', name:'ProfilePort'}) )
				.append( $('<param />').attr({value:'all', name:'AllowNetworking'}) )
				.append( $('<param />').attr({value:'false', name:'AllowFullScreen'}) )
				
				.after( $('<param />').attr({value:obj.url, name:'movie'}) )
				.after( $('<param />').attr({value:obj.url, name:'src'}) )
				.after( $('<param />').attr({value:'true',  name:'controller'}) )
				.after( $('<param />').attr({value:'transparent',name:'wmode'}) )
				.after( $('<param />').attr({value:'false', name:'cache'}) )
				.after( $('<param />').attr({value:'high',  name:'quality'}) )
				.after('</object>');*//*
	}
	//alert($('#openbox_window').html());
}
/**
 * Write Content to the iframe using the skin
 *//*
function writeToIframe(content) {
	$('#openbox_contents').append($('iframe')
			.attr('id', 'openbox_iframe')
			.attr('name', 'openbox_iframe')
			.attr('src', 'about:blank')
			.attr('height', '100%')
			.attr('width', '100%')
			.attr('frameborder', '0')
			.attr('marginwidth', '0')
			.attr('marginheight', '0')
			.attr('scrolling', 'auto'));
	alert($('#openbox_contents').html());
			
	var template = openbox.opts.skin.iframe;
	template = template.replace('{body_replace}', content); 
	if ($('openbox_iframe').contentWindow){
		$('openbox_iframe').contentWindow.document.open();
		$('openbox_iframe').contentWindow.document.write(template);
		$('openbox_iframe').contentWindow.document.close();
	} else {
		$('openbox_iframe').contentDocument.open();
		$('openbox_iframe').contentDocument.write(template);
		$('openbox_iframe').contentDocument.close();
	}
};
*/


/*
*/
})(jQuery);
