// avoid prototype.js collisions
$jq = jQuery.noConflict();

// variable configurations
var animspeed = 'normal';
var d = new Date; // used for assigning dynamic IDs to page elements
var flowplayerSwf = "/layouts/SJMSites/_swf/flowplayer.commercial-3.1.1.swf";

// determine and assign correct key to use for flowplayer
var fp_keys = { // all registered keys
	"sjm.com"				:	"@7b282e03bf81ce8616a",
	"*.sjm.com"				:	"#@7b282e03bf81ce8616a",
	"sjmprofessional.com"	:	"@f9b898b905329866fc7",
	"*.sjmprofessional.com"	:	"#@f9b898b905329866fc7",
	"heartvalveanswers.com"	:	"@5b53b6b6a5590d0b253",
	"*.heartvalveanswers.com"	:"#@5b53b6b6a5590d0b253"
	
};
var fp_key = '#@5b53b6b6a5590d0b253';
var hostkey = '';
var hostname = location.host;
var arr_mask = hostname.split('.');
if(arr_mask.length > 2){
	hostkey = '*.';
	hostkey += arr_mask[arr_mask.length-2]+'.';
	hostkey += arr_mask[arr_mask.length-1];
}else{
	hostkey = arr_mask[0]+'.'+arr_mask[1];
}
fp_key = fp_keys[hostkey];
// Setting to a default for testing environments
if (!fp_key)
{
    fp_key = '@7b282e03bf81ce8616a';
}


// utility function for calling from inside popup iframes
// use self.parent.closeOverlay() inside an iframe to close 
// the overlay structure
function closeOverlay(){
	$jq('#js-content').trigger('click');
}

$jq(function(){
	
	// popup displays
	$jq('body').append('<div id="js-overlay"><\/div><div id="js-content"></\div>');
	$jq('#js-content').livequery(
		'click',
		function(){		 
			$jq('#js-content').fadeOut(animspeed);
			$jq('#js-overlay').fadeOut(
				animspeed,
				function(){ 
					$jq('html').removeClass('overlay-active');
					/*
						to get things to work in ie6 we need to completely
						destroy and rebuild these elements
					*/
					$jq('div#js-overlay').remove();
					$jq('div#js-content').remove();
					$jq('body').append('<div id="js-overlay"><\/div><div id="js-content"></\div>')
				}
			);
		}
	);
	$jq('#js-content table.overlay-content').livequery(
		'click',
		function(e){ e.stopPropagation(); }
	);
	// popup failsafe
	$jq('*[class^="pop-"]:not("a")').each(function(){
		var thetype = $jq(this).attr('class');
		$jq(this).parents('a').addClass(thetype);
	});
	// popup: video
	$jq('a.pop-video').livequery(
		'click',
		function(){
		   
			var theRandomNumber = d.getTime();
			//var theRandomNumber = 3;
			var theURL = $jq(this).attr('href');
			$jq('#js-content').append(
				'<table class="overlay-wrapper overlay-wrapper-close" cellspacing="0" cellpadding="0" border="0"><tr><td>'+
					'<table class="overlay-content" cellspacing="0" cellpadding="0" border="0"><tr><td>'+
						'<a class="popup-video" id="vid'+theRandomNumber+'" href="'+theURL+'"></a>'+
					'<\/td><\/tr><\/table>'+
				'<\/td><\/tr><\/table>'
			);
			//alert('pop-video'+fp_key);
		    $jq('#vid'+theRandomNumber).flowplayer(flowplayerSwf, {
		        clip: {
						onStart: function(clip) {
							$jq(this.getParent()).css(
								{
									width: clip.metaData.width,
									height: clip.metaData.height
								}
							);
						} // more config vars can go here
					}, 
                key: fp_key 
            }); 
			//$jq('#vid'+theRandomNumber).flowplayer(
			//	'_res/swf/flowplayer.commercial-3.1.1.swf',
			//	{ 					
			//		key: fp_key
			//	}
			//);
			
			$jq('html').addClass('overlay-active');
			$jq('#js-overlay').fadeIn(animspeed);
			$jq('#js-content').fadeIn(animspeed);
			return false;
		}
	);
	// popup: image
	$jq('a.pop-image').livequery(
		'click',
		function(){
			var theURL = $jq(this).attr('href');
			$jq('#js-content').append(
				'<table class="overlay-wrapper" cellspacing="0" cellpadding="0" border="0"><tr><td>'+
					'<table class="overlay-content" cellspacing="0" cellpadding="0" border="0"><tr><td>'+
					'<\/td><\/tr><\/table>'+
				'<\/td><\/tr><\/table>'
			);
			$jq('table.overlay-content td:first').load(
				theURL,
				function(){
					$jq('html').addClass('overlay-active');
					$jq('#js-overlay').fadeIn(animspeed);
					$jq('#js-content').fadeIn(animspeed);
				}
			);
			return false;
		}
	);
	// popup: iframe/form
	$jq('#signup a').addClass('pop-iframe');
	$jq('a.pop-iframe').livequery(
		'click',
		function(){
			var theURL = $jq(this).attr('href');
			$jq('#js-content').append(
				'<table class="overlay-wrapper" cellspacing="0" cellpadding="0" border="0"><tr><td>'+
					'<table class="overlay-content" cellspacing="0" cellpadding="0" border="0"><tr><td>'+
						'<iframe src="'+theURL+'" width="710" height="450" frameborder="0"><\/iframe>'+
					'<\/td><\/tr><\/table>'+
				'<\/td><\/tr><\/table>'
			);
			$jq('html').addClass('overlay-active');
			$jq('#js-overlay').fadeIn(animspeed);
			$jq('#js-content').fadeIn(animspeed);
			return false;
		}
	);
	// popup: closer boxes
	$jq('.closer img').livequery(
		'click',
		function(){
			//$jq('#js-content').trigger('click');
			//alert('close');
			self.parent.closeOverlay();		
		}
	);
	
	// Left Navigation Video Promo
	$jq('a#player_left_nav').livequery(
		'click',
		function() {
	        var theURL = $jq(this).attr('href');
	        trackIt('/VideoPromo/'+theURL,false,true);
		}
    );
	
	$jq("#player_left_nav").flowplayer(flowplayerSwf, {
	    key: fp_key,
        clip: {
		    autoPlay: true,
			autoBuffering: true
        },
        // Set Play Button Image     
	    play: {         
			        url: '/layouts/SJMSites/_images/video_icon_227px.png',         
			        width: 227,         
			        height: 129             
	    } , 
		// disable default controls
		plugins: {controls: null}

    });

    // Left Navigation Video Promo One
    $jq('a#player_left_nav_1').livequery(
		    'click',
		    function() {
		        var theURL = $jq(this).attr('href');
		        trackIt('/VideoPromo/' + theURL, false, true);
		    }
        );

    $jq("#player_left_nav_1").flowplayer(flowplayerSwf, {
        key: fp_key,
        clip: {
            autoPlay: true,
            autoBuffering: true
        },
        // Set Play Button Image     
        play: {
            url: '/layouts/SJMSites/_images/video_icon_227px.png',
            width: 227,
            height: 129
        }, 
        // disable default controls
        plugins: { controls: null }

    });

    // Left Navigation Video Promo One
    $jq('a#player_left_nav_2').livequery(
		    'click',
		    function() {
		        var theURL = $jq(this).attr('href');
		        trackIt('/VideoPromo/' + theURL, false, true);
		    }
        );

    $jq("#player_left_nav_2").flowplayer(flowplayerSwf, {
        key: fp_key,
        clip: {
            autoPlay: true,
            autoBuffering: true
        },
        // Set Play Button Image     
        play: {
            url: '/layouts/SJMSites/_images/video_icon_227px.png',
            width: 227,
            height: 129
        }, 
        // disable default controls
        plugins: { controls: null }

    });

    // Left Navigation Video Promo One
    $jq('a#player_left_nav_3').livequery(
		    'click',
		    function() {
		        var theURL = $jq(this).attr('href');
		        trackIt('/VideoPromo/' + theURL, false, true);
		    }
        );

    $jq("#player_left_nav_3").flowplayer(flowplayerSwf, {
        key: fp_key,
        clip: {
            autoPlay: true,
            autoBuffering: true
        },
        // Set Play Button Image     
        play: {
            url: '/layouts/SJMSites/_images/video_icon_227px.png',
            width: 227,
            height: 129
        }, 
        // disable default controls
        plugins: { controls: null }

    }); 
	
	// Video Player and Menu on page wherever 
    $jq('a.video_player_clip').livequery(
		'click',
		function(){		    
			var theURL = $jq(this).attr('href');
			
			// Remove current from rest			
	        $jq("a.video_player_clip").removeClass('current');
	        
			// Set the class to current
			$jq(this).addClass('current');
			
			//alert('video_player_clip '+theURL);
			//$jq('#video_player').flowplayer( flowplayerSwf, theURL);
			
			// Had to do this twice so first video would play - need to figure out why, but leaving it for now
			$jq('#video_player').flowplayer(flowplayerSwf, {   
                key: fp_key,
                clip: {
                    url: theURL,
                    autoPlay: true,
			        autoBuffering: true
			    },
			    // Set Play Button Image     
			    play: {
			    url: '/layouts/SJMSites/_images/video_icon_438px.png',         
			        width: 438,         
			        height: 247             
			    }            
            }); 
            
            // Track the Video
            trackIt('/Video/'+theURL,false,true);
            $jq('#video_player').flowplayer(flowplayerSwf, {   
                key: fp_key,
                clip: {
                    url: theURL,
                    autoPlay: true,
			        autoBuffering: true
                },
                // Set Play Button Image     
			    play: {
			        url: '/layouts/SJMSites/_images/video_icon_438px.png',
			        width: 438,
			        height: 247    
			    }   
                //,  
                //onBeforeFinish: function() { 
                //    return false; 
                //} 
         
            }); 
            
			return false;
		}
	);
	
    //$jq("#player").click(function(){
	//    $jq(this).parent().find("span").addClass("playing");
    //})
    
    //$jq("a", "#clips").each(function(){
	//    $jq(this).click(function(e){
	//	    $f().play(this.getAttribute("href", 2));
	//	    var str = $jq(this).parents("div.body").find("a:eq(1)").text();
	//	    $jq("#player").parent().find("span").addClass("playing").text(str);
	//	    e.preventDefault();
	//	    location.href="#movie-clip";
	//   });
	//});
	
	// Left Navigation Video Promo
	$jq('a.trackIt').livequery(
		'click',
		function() {
	        var theURL = $jq(this).attr('href');
	        trackIt('/Link/'+theURL,false,true);
		}
    );
	
});