/*
 * Ceebox 1.0
 * By Colin Fahrion (http://www.catcubed.com)
 * Adapted from Thickbox (http://jquery.com/demo/thickbox/) Copyright (c) 2007 Cody Lindley (http://www.codylindley.com)
 * Video pop-up code inspired by Videobox (http://videobox-lb.sourceforge.net/)
 * Copyright (c) 2008 Colin Fahrion
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/

// Change base settings below	  
var cee_pathToImage = "../gfx/loadinganimation.gif";
var cee_htmlBaseWidth = 700
var cee_htmlBaseHeight = 400
var cee_videoBaseWidth = 500
var cee_videoBaseHeight = 360

/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

//on page load call cee_init (only if not homepage)

$(document).ready(function(){
	cee_init('a.ceebox');//pass where to apply ceebox
	imgLoader = new Image();// preload image
	imgLoader.src = cee_pathToImage;
});

//add ceebox to href & area elements that have a class of .ceebox
function cee_init(domChunk){
	$(domChunk).click(function(){
	var t = this.title || this.name || null;
	var a = this.href || this.alt;
	var g = this.rel || false;
	cee_show(t,a,g);
	this.blur();
	return false;
	});
}

function cee_show(caption, url, rel) {//function called when the user clicks on a ceebox link

	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("cee_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='cee_HideSelect'></iframe><div id='cee_overlay'></div><div id='cee_window'></div>");
				$("#cee_overlay").click(cee_remove);
			}
		} else {//all others
			if(document.getElementById("cee_overlay") === null) {
				$("body").append("<div id='cee_overlay'></div><div id='cee_window'></div>");
				$("#cee_overlay").click(cee_remove);
			}
		}
		
		if(cee_detectMacXFF()){
			$("#cee_overlay").addClass("cee_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#cee_overlay").addClass("cee_overlayBG");//use background and opacity
		}
		
		if(caption===null){caption="";}
		$("body").append("<div id='cee_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$('#cee_load').show();//show loader
		
		
        var vidSize = cee_getSize(rel);
		var vidId = url.split('v=')[1].split('&')[0];
        var vidSrc = "http://www.youtube.com/v/"+vidId+"&hl=en&fs=1&autoplay=1"
        var flashvars = {};
        var params = {};
        params.wmode = "transparent";
        params.allowFullScreen = "true";
        params.allowscriptaccess = "always";
        var attributes = {};

        cee_vidWindow(vidSize,caption);

        //embed swfobject
        swfobject.embedSWF(vidSrc, "cee_video", vidSize[0], vidSize[1], "9.0.0", false, flashvars, params, attributes);
        $("#cee_load").remove();
        $("#cee_window").css({display:"block"}); //for safari using css instead of show

		
	} catch(e) {
		//nothing here
	}
}

//helper functions below
function cee_showIframe(){
	$("#cee_load").remove();
	$("#cee_window").css({display:"block"});
}

function cee_remove() {
 	$("#cee_imageOff").unbind("click");
	$("#cee_closeWindowButton").unbind("click");
	$("#cee_window").fadeOut("fast",function(){$('#cee_window,#cee_overlay,#cee_HideSelect').trigger("unload").unbind().remove();});
	$("#cee_load").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 = "";
	return false;
}

function cee_position(w,h) {
$("#cee_window").css({marginLeft: '-' + parseInt((w / 2),10) + 'px', width: w + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#cee_window").css({marginTop: '-' + parseInt((h / 2),10) + 'px'});
	}
}

function cee_getSize(rel,baseW,baseH){	
			//To set width and height for the video use the rel attribute. I.E., rel="600 480"
			(baseW)? baseW=baseW*1 : baseW=cee_videoBaseWidth;
			(baseH)? baseH=baseH*1 : baseH=cee_videoBaseHeight;
			var sizes=new Array();
			if (rel && rel.match(/[0-9]+/g)){
				sizes = rel.match(/[0-9]+/g);
				(sizes[0])? sizes[0] = sizes[0]*1 : sizes[0] = baseW;
				(sizes[1])? sizes[1] = sizes[1]*1 : sizes[0] = baseH;
			} else {
				sizes = [baseW,baseH];
			}
			return sizes;
}

function cee_vidWindow(vidSize,caption) {
	//create ceebox window for video
	$("#cee_window").append("<div id='cee_video'>Video not available</div>" + "<div id='cee_caption'>"+caption+"</div><div id='cee_closeWindow'><a href='#' id='cee_closeWindowButton' title='Close'><img src='../gfx/lightbox/closelabel.gif'/></a></div>");
	$("#cee_closeWindowButton").click(cee_remove);
	cee_position(vidSize[0] + 30,vidSize[1] + 60);
}

function cee_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;
}

function cee_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}
