/* VIDEO PLAYER FUNCTIONS */
var player = null;
var current_video = null;
var stats_video_file = '';
function playerReady(obj){
		player_id = obj.id;
		player = document.getElementById(player_id);
		addListeners();
}; //get player location from DOM, ready
function addListeners() {
	playlist = player.getPlaylist();
	if(playlist != null) {
		player.addViewListener("LOAD","videoLoad");
		player.addViewListener("PLAY","videoStatistics");
	} else {
		// alert('No Config Yet');
		setTimeout("addListeners();", 100);
	}
}
function videoLoad(o) {
	stats_video_file = decodeURIComponent(o['object']['file']);
}

function videoStatistics(o) {
	var cfg = player.getConfig();
	var id = cfg['id'];
	var state = cfg['state'];
	if(stats_video_file == '') { stats_video_file = cfg['file']; }
	/*console.log('file: ' + stats_video_file);*/
	$('#video_views').load('/', {
		nd: 'xmlhttp-video_statistics',
		action: 'add_view',
		file: stats_video_file
	});
	return true;
}


$(document).ready(function(){
	
	//CONSTANT WIDTH TABS FOR PRIMARY NAV
	$('#primarynav > li a').each(function(){
		$(this).css('width',$(this).width()+'px');
	});
	$('#primarynav > li.on a').css('fontWeight','bold');
	
	//PARENT IN SECONDARY NAV IS NOT CLICKABLE
	$('ul#secondarynav > li > ul').siblings('a').removeAttr('href');			   
	
	//RETAIN VALUE OF INPUT TEXT FIELDS WITH CLASS retainValue
	$('input.retainValue').each(function(){
		$(this).data('value', $(this).attr('value'));
	});
	$('input.retainValue').focus(function(){
		if ($(this).attr('value') == $(this).data('value')){
			$(this).attr('value', '');
		}
	});
	$('input.retainValue').blur(function(){
		if ($(this).attr('value') == ''){
			$(this).attr('value', $(this).data('value'));
		}
	});
						   	
	//REMOVE TAB FOCUS OUTLINE
	$('.module .module_tabs li a').focus(function(){$(this).blur()});
	//MODULE TAB FUNCTIONALITY (exclude for letter tabs on networking directory)
	$('.module_tabs:not(.letter_tabs)').each(function(){
		//set active items
		var activeTab = $(this).children('li.on');
		var activeContent = $(this).siblings('.module_body').find('.on');
		
		//switch on click
		$(this).children('li').click(function(e){
											  
			//check if not active link
			if($(e.target).attr('href')[0] == "#"){
				e.preventDefault();
			}
											  
			//toggle class tabs
			$(activeTab).removeClass('on');
			activeTab = $(this).addClass('on');	
			
			//toggle content
			if ($.browser.msie && $.browser.version.substr(0,1)<7) {
				activeContent = $(activeContent).hide().closest('.module_body').find($(this).children('a').attr('href')).show();
			} else {
				activeContent = $(activeContent).removeClass('on').closest('.module_body').find($(this).children('a').attr('href')).addClass('on');
			}
		});
	});
	
	//REMOVE EMPTY P TAGS
	$('p').each(function() {
    	if ($.trim($(this).html()) == "") {
			$(this).remove();
		}
	});
	
	//MATCH SCROLL LIST HEIGHT TO CONTENT
	$('.module_video .scrolllist:not(#expertvideolist)').each(function(){
		$(this).css('height',$(this).closest('.main').siblings('.left').outerHeight() - $(this).siblings('h4').outerHeight()+'px');
	});
	
	//LIKE FUNCTIONALITY
	$('a.like').click(function(){
		if($(this).hasClass('unlike')){
			$(this).text('Like').removeClass('unlike');
		} else {
			$(this).text('Unlike').addClass('unlike');
		}						   
	});
	
	//FORMBUILDER FORMAT HACK
	if ($('form.formbuilder').length > 0){
		$('form.formbuilder label br').remove();
	}
	
	//ADD SHADOW TO SCROLLLISTS
	if ($('.scrollhead').length > 0){
		$('.scrollhead').append("<div class='scrolllist_shadow'></div>");
	}
	
	
	
	
	//IE6 fixes
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		$('ul#secondarynav > li').hover(function(){
			$(this).addClass('hover');
		},function(){
			$(this).removeClass('hover');
		});
		$('ul.meta > li').addClass('metaItem');
	}
});

/* FUNCTIONS FROM EXISTING SITE */
//benefits
function open_benefits_popup() {
	$("#benefits_popup_bg").css("height", $(document).height());
	//VERTICALLY CENTER POPUP
	var windowHeight = $(window).height();
	var popupHeight = $("#benefits_popup_container").height();
	var scrollPos = $(window).scrollTop();
	var popupY = windowHeight/2 - popupHeight/2 + scrollPos;
	$("#benefits_popup_bg").css("display","block"); 
	$("#benefits_popup_container").css("display","block"); 
	$("#benefits_popup_container").css("top", popupY);
	return false;
}

function close_benefits_popup() {
	$("#benefits_popup_bg").css("display","none"); 
	$("#benefits_popup_container").css("display","none");
	$("#overlay_form").html("");
}

//extended profile
function open_extended_profile(handoffnd) {
	//VERTICALLY CENTER POPUP
	$("#extended_profile_bg").css("height", $(document).height());
	var windowHeight = $(window).height();
	var popupHeight = $("#extended_profile_container").height();
	var scrollPos = $(window).scrollTop();
	//var popupY = windowHeight/2 - popupHeight/2 + scrollPos;
	var popupY = 25 + scrollPos;
	$("#extended_profile_bg").css("display","block"); 
	$("#extended_profile_container").css("display","block");
	$("#extended_profile_container").css("top",popupY); 
	$("#overlay_form").load('/', {
		nd: 'xmlhttp-load_extended_profile',
		handoffnd: handoffnd
	}, function() {
		$('#extended_profile_container #adskRegistrationForm').submit(
		function(e) {
			var requiredFieldEmpty = false;
			$('.required').each(function() {
				if ($.trim($(this).val()) == '') {
					$('label[for='+$(this).attr('id')+']').css('color','red');
					requiredFieldEmpty = true;
				} else {
					$('label[for='+$(this).attr('id')+']').attr('style','');
				}
			});
			
			if (requiredFieldEmpty) {
				alert("A required field was left blank");
				e.preventDefault();
			}
		});
	});
	return false;
}

function close_extended_profile() {
	$("#extended_profile_bg").css("display","none"); 
	$("#extended_profile_container").css("display","none");
	$("#overlay_form").html("");
	$.get("/?", { nd: "xmlhttp-load_extended_profile_cancel" } );
	return false;
}

function showDWF (name){
	$('.showDWF').click(function(){
		 
		//find URLS
		var URL = $(this).next('#DWF_urls').children('a').attr('href');
		var title = $(this).next('#DWF_urls').children('a').attr('title');
		
		var iframe = "<h2 id='modal_title'>"+title+"</h2><iframe src='http://freewheel.labs.autodesk.com/dwf.aspx?path="+URL+"&sec=1&mode=ViewOnly' name='dwf_viewer' id='dwf_viewer' width='800' height='600' scrolling='no' frameborder='0'></iframe>";
		
		overlay_show(iframe, false);
		$('#overlay').addClass('DWFoverlay');
		
		
		//get number of dwfs
		var numDWF = $('#DWF_urls').children('a').size();
		//alert(numDWF);

		if (numDWF > 1){
			//get array
			//var arr = jQuery.makeArray($('#DWF_urls').children('a'));
			i=0;
			//add controls 
			$('#overlay').append("<div id='controls'><button id='prev'>PREV</button><button id='next'>NEXT</button></div>");
			//prev and next buttons
			$('#controls button').click(function(){
				//change array position
				
				if($(this).attr('id') == 'prev'){
					//PREV
					i--;
				} else if($(this).attr('id') == 'next') {	
					//NEXT
					i++;
				}
				//cycle array
				if (i<0){
					i=numDWF-1;	
				}
				else if (i==numDWF) {
					i=0;
				}
				//get href
				var URL = '';
				URL = $('#DWF_urls').children('a').eq(i).attr('href');
				//get title
				title = $('#DWF_urls').children('a').eq(i).attr('title');	
				//alert('DWF:'+i+' FILE:'+URL);
				$('#dwf_viewer').attr('src', "http://freewheel.labs.autodesk.com/dwf.aspx?path="+URL+"&sec=1&mode=ViewOnly");
				$('#modal_title').text(title);
			});
		}	

		
		
		//close buttons
		$(document).keyup(function(event){if (event.keyCode == 27) {removeModal();}});
		$('.close').click(function(){removeModal();});
		$('#shadow').click(function(){removeModal();});
		return false;
	});
}

