
var megaConfig = {
    interval: 100,
    sensitivity: 4,
    over: addMega,
    timeout: 100,
    out: removeMega
};


// jquery ready event
$(document).ready(
	function() {
		initDemos();
		initMedia();
		$("li.mega").hoverIntent( megaConfig );
		initNav();
		//$('body').supersleight({shim: 'http://localhost/xampp/wp-li/wp-content/themes/lincolnInteractive/img/blank.gif'});
	}
);





/** MAIN NAVIGATION **/

function addMega(){
    $(this).addClass("hovering");
	var h = $(this).find( ".megaContent" ).height();
	var a = $(this).find( ".megaBackground" );
	a.height( h );
}

function removeMega(){
    $(this).removeClass("hovering");
}

/* 
if IE6 make nav item bg white, else add gradient bg image
*/
function initNav(){
	if(typeof document.body.style.maxHeight === "undefined") {
        //alert('ie6');
		$( ".megaContent" ).css( { "background-color" : "#fff" } );
	} else {
        $( ".megaContent" ).css( { "background-image" : 'url( "' + templateURL + '/img/list-image.png" )', "background-repeat" : "no-repeat", "background-position" : "bottom center" } );
	}
}

 /** END MAIN NAVIGATION **/  
   
   
   
   
   
/** MEDIA */

var firstVideo;
var firstTitle;
var playerID = 'player';

function initMedia(){
	if( exists( "#media" ) ){
		addOverlay();
		insertVideoPlayer();
		setupMedia();
	}
}

function insertVideoPlayer(){
	var flashvars            		= {};
    //flashvars.file           		= templateURL + "/videos/arc-educational-highres.flv";
   // flashvars.image 			= templateURL + "/img/noflash.jpg";
    flashvars.id             		= playerID;
    flashvars.autostart      	= 'false';


    var params               		= {};
    params.allowfullscreen   	= 'true';
    params.allowscriptaccess 	= 'always';

    var attributes           		= {};
    attributes.id            		= playerID;
    attributes.name         	 	= playerID;
    attributes.styleclass    		= playerID;
    
	var swf = templateURL + "/flash/player.swf";
	var expressInstall = templateURL + "/flash/expressInstall.swf";
	/* controls are 20px high */
    swfobject.embedSWF( swf, 'flashmedia', '538', '383', '9.0.124', expressInstall, flashvars, params, attributes );
}

//on ready set the first video and the main poster image
function playerReady(obj){
	addListeners();
	var player = document.getElementById( playerID );
	$( '#videoTitle' ).find( 'h2').html( firstTitle );
	player.sendEvent('LOAD',{ file:firstVideo, image: templateURL + "/img/noflash.jpg" } );
}
      
function addListeners(){
	var player = document.getElementById( playerID );
	player.addModelListener("STATE", "onVideoState");
	player.addModelListener("META", "onMeta");
}

function onMeta( o ){
	if( o.width ){
		//alert( "meta : width : " + o.width + " : height : " + o.height )
	}	
}

function onVideoState( o ) {
	//oldstate == o.oldstate
	//newstate == o.newstate
	if (o.newstate == "COMPLETED") {
		// video has finished playing, reload main poster
		//alert( "video complete" );
	}
}

function setupMedia(){
	addOverlay();
	var dr = $( ".demoRight" );
	var dl = $( ".demoLeft" );
	addMediaClick( dr, false );
	addMediaClick( dl, true );	
}

function addMediaClick( e, val ){
	var selectFirst = val;
	jQuery.each( e, function(){
		var a = $(this).find( "a" );
		var h2 = $(this).find( "h2" );
		var classType = a.attr( "class" );
		
		var href= $(a).attr( "href" );
		var title = $(h2).html();
		if( selectFirst == true ){
			selectFirst = false;
			firstVideo = href;
			firstTitle = title
		}
		a.attr( "href", "#" );
		a.remove();
		$(this).click( 
		
		function(){
			setMedia( href, title )
			return false;
		});
	});
}

function setMedia( media, title ) {
	$( '#videoTitle' ).find( 'h2').html( title );
	var player = document.getElementById( playerID );
	player.sendEvent('LOAD',{ file:media } ); //,image:'image.jpg'});
	player.sendEvent('PLAY');
	window.location.hash="top";   //after click move back up to video
}

/** END MEDIA **/





/** DEMOS */

function initDemos(){
	if( exists( "#demos" ) ){
		setupDemos();
	}
}

var demos = [
				{ id:"arc", width:660, height:490, title:"ARC" },
				{ id:"flashcards", width:565, height:420, title:"FlashCards" },
				{ id:"artnumbers", width:715, height:515, title:"ArtByNumbers" },
				{ id:"dunk", width:715, height:515, title:"DunkARoo" },
				{ id:"monkey", width:715, height:515, title:"MakeAMonkey" },
				{ id:"precalc", width:700, height:530, title:"Pre-CalculusLesson" },
				{ id:"yesterday", width:670, height:510, title:"YesterdaysNews" }
			];

function setupDemos(){
	addOverlay();
	var dr = $( ".demoRight" );
	var dl = $( ".demoLeft" );
	addDemoClick( dr );
	addDemoClick( dl );	
}

function addDemoClick( e ){
	jQuery.each( e, function(){
		var a = $(this).find( "a" );
		var id = a.attr( "id" );
		
		var href= $(a).attr( "href" );
		var obj = findInArray( demos, id, "id" );
		var w = obj.width;
		var h = obj.height;
		var title = obj.title;
		a.attr( "href", "#" );
		a.remove();
		$(this).click( 
		
		function(){
			popWindow( href, w, h, title )
			return false;
		});
	});
}

function findInArray( arr, value, key ){
	var len = arr.length;
	for( var i=0; i< len; i++ ){
		if( arr[i][key] == value ){
			return arr[i];
		}
	}
}

function popWindow( file, w, h, name ){
	var win= null;
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	settings='height=' + h + ',width=' + w + ',top=' + wint + ',left=' + winl+  ',scrollbars=no,toolbar=no';
	win=window.open(file,name,settings);
	if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

/** END DEMOS **/






/** Add Play Overlay Hover / Out Classes */
function addOverlay(){
	$(".demo").before("<div class='overlay'><img src='" + templateURL + "/img/play-overlay.png' width='262' height='156' alt='play'></div>");
	/* $(".overlay").fadeTo("fast", 0); */
	$(".overlay").hide();
	$(".demoLeft").hover(function(){
		$(this).find( ".overlay" ).show();
		$(this).addClass("over");
		//$(this).prev( ".overlay" ).fadeTo( "fast", 1 );
	},function(){
		$(this).removeClass("over");
		$(this).find( ".overlay" ).hide();
	});
	
	$(".demoRight").hover(function(){
		$(this).find( ".overlay" ).show();
		$(this).addClass("over");
		//$(this).prev( ".overlay" ).fadeTo( "fast", 1 );
	},function(){
		$(this).removeClass("over");
		$(this).find( ".overlay" ).hide();
	});
}

function exists( e ){
	return $(e).length > 0;
}


		