var featuredLocation = 0;
var totalFeaturedItems = 1;

var MM_FlashCanPlay = 0;
var MM_contentVersion = 9;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if ( plugin ) {
		var words = navigator.plugins["Shockwave Flash"].description.split(" ");
	    for (var i = 0; i < words.length; ++i)
	    {
		if (isNaN(parseInt(words[i])))
		continue;
		var MM_PluginVersion = words[i]; 
	    }
	var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
}
else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 
   && (navigator.appVersion.indexOf("Win") != -1)) {
	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
	document.write('on error resume next \n');
	document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
	document.write('</SCR' + 'IPT\> \n');
}

var oldVideoHtml = 0;

function vcDoFlash( linkItem, imageSize ) {
	var hrefReference = jQuery( linkItem ).attr( "href" );
	var imgObject = jQuery( linkItem ).find( "img" );
	var imgObjectSrc = imgObject.attr( "src" );
	var w = imgObject.width();
	var h = imgObject.height();
	
	var flashvars = {};
	flashvars.height = w;
	flashvars.width = h;
	flashvars.icons = "false";
	flashvars.controlbar = "over";
	flashvars.stretching = "exactfit";
	flashvars.smoothing = "false";
	flashvars.bandwidth = "2200";
	flashvars.file = hrefReference;
	flashvars.autostart = "true";	
	flashvars.skin = "http://cdn1.visioncritical.com/wp-content/themes/vc/flash/player-skin.swf";
	//flashvars.image = imgObjectSrc;
	var params = {};
	params.allowscriptaccess = "always";
	params.allowfullscreen = "true";
	params.wmode = "opaque";

	var attributes = {};
	
	var parentDiv = jQuery( linkItem ).parent();
	oldVideoHtml = parentDiv.html();
	
	var divID = parentDiv.attr( "id" );
	
	if (MM_FlashCanPlay) {
		swfobject.embedSWF("http://cdn1.visioncritical.com/wp-content/themes/vc/flash/player-licensed.swf", divID, w, h, "9.0.0", false, flashvars, params, attributes);		
	} else {
		var newHtml = '<a target="_blank" class="flash_install" href="http://get.adobe.com/flashplayer/otherversions/"><img src="/wp-content/themes/vc/images/flash/install_flash_' + imageSize + '.jpg" /></a>';
		parentDiv.html( newHtml );
	}	
		
	return false;
}

function vcMakeTab( name1, name2 ) {
	var count = 0;
	jQuery( name1 + " li a" ).each( function() { jQuery(this).attr( "rel", count ); count = count + 1; } );
	
	count = 0;
	jQuery( name2 + " li" ).each( function() { jQuery(this).attr( "rel", count ); count = count + 1; } );
	
	jQuery( name2 + " li" ).hide();
	jQuery( name1 + " li" ).addClass( "tab" );
	jQuery( name1 + " li:nth(0) a" ).addClass( "selected" );
	jQuery( name1 + " li:nth(0)" ).addClass( "li-selected" );
	
	jQuery( name2 + " li:nth(0)" ).show();
	jQuery( name1 + " li a" ).click( function() {
		jQuery( name1 + " li a" ).removeClass( "selected" );
		jQuery( name1 + " li" ).removeClass( "li-selected" );
		
		jQuery(this).addClass( "selected" );
		jQuery(this).parent().addClass( "li-selected" );
		
		var countNum = jQuery( this ).attr( "rel" );
		jQuery( name2 + " li" ).each( function() { if ( jQuery(this).attr( "rel" ) == countNum ) { jQuery(this).show(); } else { jQuery(this).hide(); } } );
		
		return false;
	});
}

function vcSetupTabs() {
	vcMakeTab( '#tag-widget-list', '#tag-widget-content' );
}

function doVCReady() {
	vcSetupTabs();
	
	getTwitters( 'vc-twitter-ajax', { 
		id: 'visioncritical', 
		count: 3, 
		enableLinks: true, 
		ignoreReplies: true, 
		clearContents: true,
		template: '%text% <a class="twit-time" href="http://twitter.com/%user_screen_name%/statuses/%id%/">%time%</a>'
	});
	
	getTwitters( 'angus-twitter-ajax', { 
		id: 'bkm555', 
		count: 3, 
		enableLinks: true, 
		ignoreReplies: true, 
		clearContents: true,
		template: '%text% <a class="twit-time" href="http://twitter.com/%user_screen_name%/statuses/%id%/">%time%</a>'
	});
	
	// Content Gallery
	jQuery( ".gallery-thumbs li a" ).click( function() { switchFeatured( this ); return false; } );		

	vcSetupInsights();
	vcSetupNewsroom();
	vcSetupPublic();
	
	jQuery("a.fancybox").fancybox(); 
	
	jQuery("#product-video-area a.video").fancybox({
		'frameWidth': 500,
		'frameHeight': 400,
		'hideOnContentClick': false,
		'overlayOpacity': 0.7	
	});
	
	jQuery(".widget_productsdemowidget a.video").fancybox({
		'frameWidth': 640,
		'frameHeight': 370,
		'hideOnContentClick': false,
		'overlayOpacity': 0.7,
		 'callbackOnClose': function() {
       	jQuery( "#overlay-video").empty();
       	jQuery("#fancy_content").empty();
      	// sendEvent('STOP')
      	//alert( 
      } 	
	});	
		
	jQuery("#sidebar-product-carousel").jcarousel({
		'scroll': 1
	});
	
	//jQuery("div.menu ul").boxShadow( 2, 2, 2, '#000' );
	jQuery( "a.featured-left" ).click( function() {
		if ( featuredLocation > 0 ) {
			switchFeatured( featuredLocation - 1 );	
		} else {
			switchFeatured( totalFeaturedItems - 1 );
		}
		
		return false;
	});
	
	jQuery( "a.featured-right" ).click( function() {
		if ( featuredLocation < (totalFeaturedItems-1) ) {
			switchFeatured( ++featuredLocation );	
		} else {
			switchFeatured( 0 );
		}
		
		return false;
	});	
	
	var items = jQuery( "li.featured-item" ).size();
	totalFeaturedItems = items;
	
	vcSetupAllVideos();
	
	 if( typeof( chatDeploymentSetup ) == 'function' ) {
	 	chatDeploymentSetup('ee2362e3-b9a9-495d-bdc1-acf870d76677', 'support.visioncritical.com', 5380, 5389);
	 }
}

function vcSetupAllVideos() {
	jQuery('a.vcvideo').click( function() { return vcDoQuicktime( this ); } );	
	jQuery('a.jwvideo').click( function() { return vcDoFlash( this, 'MED' ); } );
	jQuery('#sidebar a.jwvideo').click( function() { return vcDoFlash( this, 'SM' ); } );	
}

function vcSetupInsights() {
	// Industry Insights Top-Right Panel
	jQuery( "#industry-insights .insights-menu li a" ).click( function() {
		var loc = jQuery(this).html();
		jQuery.get( "/?vc_main=" + loc, function( data ) { jQuery( "#industry-insights" ).html( data ); vcSetupInsights(); } );
		return false;
	});
}

function vcSetupNewsroom() {
	// Newsroom Front Page Panel
	jQuery( "#news-room .insights-menu li a" ).click( function() {
		var loc = jQuery(this).html();
		jQuery.get( "/?vc_newsroom=" + loc, function( data ) { jQuery( "#news-room" ).html( data ); vcSetupNewsroom(); } );
		return false;
	});
}

function vcDoChat() {
	launchChatWindow("https://support.visioncritical.com/ics/support/default.asp?deptID=5389&task=chat&deploymentId=ee2362e3-b9a9-495d-bdc1-acf870d76677"); 	
}


function vcSetupPublic() {
		// Public Opinion Front Page Panel
	jQuery( "#global-trends .insights-menu li a" ).click( function() {
		var loc = jQuery(this).html();
		jQuery.get( "/?vc_public=" + loc, function( data ) { jQuery( "#global-trends" ).html( data ); vcSetupPublic(); } );
		return false;
	});
}

function switchFeatured( toWhich ) {
	//alert( toWhich );
	var whichNumber = jQuery( toWhich ).attr("rel");
	//alert( whichNumber );
	if ( whichNumber == undefined ) {
		whichNumber = toWhich;	
	}
	//alert( toWhich );
	
	jQuery( "li.featured-item" ).hide();
	jQuery( "li.featured-" + whichNumber ).fadeIn();	
	
	featuredLocation = whichNumber;
	//alert( featuredLocation );
}

function switchTwitter( twitType ) {
	var twitName = twitType + "-twitter";
	
	jQuery( "#social-area ul li.twit-li" ).removeClass("selected");
	jQuery( "#social-area ul li." + twitType ).addClass("selected");
	jQuery( "#social-area div.twitter-feed" ).hide();
	jQuery( "#social-area #" + twitName ).show();

}

function vcChangePage( pageNum ) {
	jQuery( "#industry-insights .all-pages" ).hide();
	jQuery( "#industry-insights .page-" + pageNum ).fadeIn( 500 );
	jQuery( "#industry-insights .all-dots" ).removeClass( "current" );
	jQuery( "#industry-insights .dot-" + pageNum ).addClass( "current" );
}
 
jQuery(document).ready( function() { doVCReady(); } );
