// DEPENDENCY! 
// "c26v1-pilot" requires "c16v3-pilot" 
// (lightbox pilot requires video player pilot) 

// = c26v1.js =
/*
c26v1.js
$Revision: 1.5 $
JS Functions for the lightbox.
(c) 1992-2009 Cisco Systems, Inc. All rights reserved. 
Terms and Conditions: http://cisco.com/en/US/swassets/sw293/sitewide_important_notices.html
*/

var ie6=$.browser.msie&&($.browser.version == "6.0");

//
// ** {{{ object creation }}} **
//
// Call to the helper function cdc.util.ensureNamespace.
cdc.util.ensureNamespace('cdc.clb.c26v1_pilot');


// ** {{{ cdc.clb.c26v1_pilot.init }}} **
//
// Triggers on page load.
// It will attach each trigger with the corresponding lightbox.
cdc.clb.c26v1_pilot.init = function() {

	// Below each loop is to support multiple lightbox's in a page.
	jQuery(".c26v1-pilot").each(function(index){	

		var lightbox = this;
		var show = function(){};
    var hide = function(){};
		
		// Below if loop is to check whether the current lightbox contains "c16v3-pilot" or "c15v7" or "clb" component. 
		if(jQuery('.c16v3-pilot',this).length){

			 show = cdc.clb.c26v1_pilot.movieopen;
			 hide = cdc.clb.c26v1_pilot.movieclose;
			
			// There are three callback functions "onShow", "onHide", and "onLoad".
			// Each callback is passed the jqModal "hash" for a window.
			jQuery(lightbox).jqm({modal:false, onShow:show, onHide:hide });
		}

		/* Below else if loop is commented because both c15v7 component or clb div inside lightbox component contains the class "clb" so no need to use two seperate conditions.*/
		/*else if(jQuery('.c15v7',this).length){

			jQuery(lightbox).jqm({modal:false});
		}*/
		
		else if(jQuery('.clb',this).length || jQuery('.cl',this).length){	

			jQuery(lightbox).jqm({modal:false});
		}

		// Find the trigger using rel attribute.
		var mytriggers=jQuery("a[rel="+lightbox.id+"],div[rel="+lightbox.id+"],img[rel="+lightbox.id+"]");	
		
		// Attaching the trigger to the corresponding lightbox. 
		jQuery(lightbox).jqmAddTrigger(mytriggers);

		// Attaching the component to the body.
		jQuery(this).remove().appendTo('body');
	});
}



// ** {{{ cdc.clb.c26v1_pilot.position }}} **
//
// It sets the position of the lightbox.
cdc.clb.c26v1_pilot.position = function(lightboxId) {

	var vlbObj = jQuery("#"+lightboxId);

	// It sets the position of the lightbox to center of the screen.
	cdc.clb.c26v1_pilot.setPosition(vlbObj);

	// Lightbox should move vertically when user vertically scroll the window.
	jQuery(window).bind('scroll',function(){cdc.clb.c26v1_pilot.setPosition(vlbObj);});
	
	if (jQuery.browser.msie && (parseInt(jQuery.browser.version) < 7 )) {
		jQuery(".nav a").hide();jQuery(".nav a").show(); // fix disappearing nav in ie6, needs both hide and show to work
	}
}

// ** {{{ cdc.clb.c26v1_pilot.setPosition }}} **
//
// It sets the position position of the lightbox to center of the screen.
cdc.clb.c26v1_pilot.setPosition = function(vlbObj) {
	
	var intH = jQuery(window).height();
	var intW = jQuery(window).width();
	
	var scrollTop=0;  
	var scrollLeft=0;  		
		
	// position:fixed will not work for IE6.So need to add scrollTop and scrollLeft dynamically for IE6.
	if (jQuery.browser.msie && (parseInt(jQuery.browser.version) < 7 )) {
		scrollTop = jQuery(window).scrollTop();
		jQuery(window).scrollLeft();
	}
	
	// vlbObj.height() returns the height of the current lightbox.
	// vlbObj.width() returns the width of the current lightbox.
	// intH returns the height of the window.
	// intH returns the width of the window.
	vlbObj.css("top", ((parseInt(intH)-vlbObj.height())/2)+scrollTop+'px'); 
	vlbObj.css("left", ((parseInt(intW)-vlbObj.width())/2)+scrollLeft+'px');

};

// ** {{{ cdc.clb.c26v1_pilot.movieopen }}} **
//
// It sets the position of the video component div on the browser and appends the movie file by calling "cdc.clb.c16v3_pilot.open" function.
// Each callback is passed the jqModal "hash" for a window.
cdc.clb.c26v1_pilot.movieopen = function(hash){

	var lightboxId=hash.w.attr("id");	
	if ($.browser.safari) {
			hash.w.show(cdc.clb.c16v3_pilot.open(lightboxId));
	} else {
		hash.w.slideDown('fast',cdc.clb.c16v3_pilot.open(lightboxId));
	}
	jQuery("#"+lightboxId+" .jqmClose").focus(); 
	
};

// ** {{{ cdc.clb.c26v1_pilot.movieclose }}} **
//
// Triggers when user click on close button/link in the video component div.
cdc.clb.c26v1_pilot.movieclose = function(hash){ 

	var lightboxId=hash.w.attr("id");
	if ($.browser.safari) {
		hash.w.css("display","none");
		jQuery("body .jqmOverlay").remove();
		jQuery("#" + lightboxId + " .c16v3-pilot").empty();
	} else {
		hash.w.hide('fast',cdc.clb.c16v3_pilot.close(lightboxId)); 
	}

	hash.o.remove(); 
}; 

// onload function.
cdc.clb.c26v1_pilot.init();


//
// ** {{{ object creation }}} **
//
// Call to the helper function cdc.util.ensureNamespace.
cdc.util.ensureNamespace('cdc.clb.c16v3_pilot');


// ** {{{ cdc.clb.c16v3_pilot.moviewrite }}} **
//
// create swfObject in IE, embed in FF.
cdc.clb.c16v3_pilot.moviewrite = function (lightboxId) {

var videoNode = jQuery('#' + lightboxId + ' .c16v3-pilot');
var mv = videoNode.attr('c16v3_pilot_config');

mv.movie?null:mv.movie="http://www.cisco.com/cdc_content_elements/flash/video-player/mediaplayer.swf"; 
mv.usefullscreen?null:mv.usefullscreen="false";
mv.autostart?null:mv.autostart="false";
// should version or size have defaults ? or too likely to change soon?
// what about mv.aspect? if one value, one default size if other value other default size?
mv.wmode?null:mv.wmode="opaque";
mv.callback?null:mv.callback="true";
mv.allowfullscreen?null:mv.allowfullscreen="false";
// set up Visual sciences params that are grabbed from the page info
mv.sitearea=cdc.util.getSiteArea();
mv.contentgroup?null:"";
mv.pagetype="video";
mv.basepage=window.location.href;
mv.countrysite?null:mv.countrysite="US";
mv.locationid?null:mv.locationid=videoNode.attr('id'); 
//mv.javascriptid?null:mv.javascriptid="video-lightbox-javascript-id"+lightboxId; 

	if (jQuery.browser.msie) {

		// other possible params as well? ugh. Can we loop through params and get them in right places?
		var sVid = new SWFObject(cdc_cache_bust(mv.movie),"videoplayer",mv.width,mv.height,mv.version);
		sVid.addVariable("usefullscreen",mv.usefullscreen);
		sVid.addVariable("autostart",mv.autostart);
		sVid.addVariable("width", mv.width);
		sVid.addVariable("height", mv.height);
		sVid.addVariable("wmode", mv.wmode);
		sVid.addVariable("callback", mv.callback);
		sVid.addParam("allowfullscreen",mv.allowfullscreen);
		sVid.addVariable("file",mv.file);
		sVid.addVariable("image",mv.image);
		sVid.addVariable("pagename",mv.pagename);
		sVid.addVariable("sitearea",mv.sitearea);
		sVid.addVariable("contentgroup",mv.contentgroup);
		sVid.addVariable("pagetype",mv.pagetype);
		sVid.addVariable("basepage",mv.basepage);
		sVid.addVariable("countrysite",mv.countrysite);
		sVid.addVariable("videoname",mv.videoname); // this one obsolete?
		// empty captions arg can make CC button appear on player - SAMBAQA-846
		if (mv.captions) {
		sVid.addVariable("captions",mv.captions);
		}

		sVid.write(mv.locationid);
	} else {

		var writeWmode = "";
		if (navigator.appVersion.toLowerCase().indexOf("mac")==-1) { // is not a mac
			writeWmode = "wmode='"+mv.wmode+"' ";
		}

		var embedMovie = "<embed "+writeWmode+" allowscriptaccess='samedomain' src='"+mv.movie+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' "+
		"type='application/x-shockwave-flash' width='"+mv.width+"' height='"+mv.height+"' " +
		"flashvars='autostart="+mv.autostart+"&width="+mv.width+"&height="+mv.height+"&callback="+mv.callback+"&file="+mv.file+"&image="+mv.image;

		// empty captions arg can make CC button appear on player - SAMBAQA-846
		if (mv.captions) {
			embedMovie += "&captions="+mv.captions;
		}

		embedMovie += "&pagename="+mv.pagename+"&sitearea="+mv.sitearea+"&contentgroup="+mv.contentgroup+
		"&pagetype="+mv.pagetype+"&basepage="+mv.basepage+"&countrysite="+mv.countrysite+
		"&allowfullscreen="+mv.allowfullscreen+"&usefullscreen="+mv.usefullscreen+"&javascriptid="+mv.javascriptid+"' ></embed>";
		jQuery("#"+mv.locationid).html(embedMovie);
	}
	cdc.clb.c26v1_pilot.position(lightboxId);
};


// ** {{{ cdc.clb.c16v3_pilot.open }}} **
//
// Appends the movie file to the video component div.

cdc.clb.c16v3_pilot.open = function(lightboxId) {

	jQuery("body .jqmOverlay").bgiframe();           // avoid select box bleed-through (IE6)

	if (jQuery.browser.msie && (parseInt(jQuery.browser.version) < 7 )) {
		jQuery(".nav a").hide();jQuery(".nav a").show(); // fix disappearing nav in ie6, needs both hide and show to work
	}

	cdc.clb.c16v3_pilot.moviewrite(lightboxId);

	if (ie6) {
		$('html,body').css({height:'',width:''});
		function resetOverlay() {
			try {
				jQuery(".jqmOverlay").css({left: ''});
				var leftmargin = 0 - (jQuery(".jqmOverlay").position().left);
				jQuery(".jqmOverlay").css({left: leftmargin, width: jQuery(window).width(), height: jQuery(window).height()});
				var repositionLeft = leftmargin + (jQuery(window).width() / 2) - (jQuery(".jqmID1").width() / 2);
				var repositionTop = (jQuery(window).height() / 2) - (jQuery(".jqmID1").height() / 2);
				jQuery(".jqmID1").css({left: repositionLeft, top: repositionTop});

			} catch(err) {
			}
		}
		resetOverlay();	
		jQuery(window).resize(function() {
			setTimeout(function() {resetOverlay()},300);
		});
	}
}


// ** {{{ cdc.clb.c16v3_pilot.close }}} **
//
// Triggered inside the "cdc.clb.c16v3_pilot.movieclose" function.

cdc.clb.c16v3_pilot.close = function(lightboxId) {

	jQuery('#' + lightboxId + ' .c16v3-pilot').empty();

	// jQuery("body#fw-mb").css('width','908px'); /* changed from #libra */

}

jQuery(function(){
	cdc.clb.c26v1_pilot.init();
});

