$('body').ready( function(){
	
	$("#industry-updates, #pump-tweets").css("display", "none");
	
	if($(".switch_to_industry_updates").length > 0) {
		$(".switch_to_industry_updates").click(function(){
			$("#industry-updates").css("display", "block");
			$("#expert-advice, #pump-tweets").css("display", "none");
			SwitchLinksHandler();
			return false;
		});
	}
	
	if($(".switch_to_expert_advice").length > 0) {
		$(".switch_to_expert_advice").click(function(){
			$("#expert-advice").css("display", "block");
			$("#industry-updates, #pump-tweets").css("display", "none");
			SwitchLinksHandler();
			return false;
		});
	}
	
	if($(".switch_to_pump_tweets").length > 0) {
		$(".switch_to_pump_tweets").click(function(){
			$("#pump-tweets").css("display", "block");
			$("#expert-advice, #industry-updates").css("display", "none");
			SwitchLinksHandler();
			return false;
		});
	}
	
	$("#inline").live("click",  function(){

		if($("#helpContent").html() == '')
		{
			$.fancybox.showActivity();
			
			$.ajax({
				type: "POST",
				url: '/en/loadHelpContent/',
				success: function(result){
					$("#helpContent").html(result);
					
					$("#inline").fancybox({
						'autoDimensions': false,
						'width'    		: 1000,
						'height'   		: 'auto',
						'transitionIn'	: 'none',
						'transitionOut'	: 'none'
					});
					
					$("#inline").trigger('click');
					
				}
			
			});
			return false;
		}
	});
	
	$("#inline1").live("click",  function(){
		
		if($("#rateHelpContent").html() == '')
		{
			$.fancybox.showActivity();
			
			$("#inline1").fancybox({
				'autoDimensions': false,
				'width'    		: 1000,
				'height'   		: 'auto',
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'onStart'		: function(){
					$("#rateHelpContent").html(dictionary_rate_help);
				},
				'onCleanup'		: function(){
					$("#rateHelpContent").html("");
				}
			});
			
			$("#inline1").trigger('click');
			
			return false;
		}
		
		return false;
	});
	
	

	function  applySearchSelections() {
		/*$(".searchSelect, .searchSelectSmall").each( function(){
			if($(this).parents(".filterForm").hasClass("filterForm")){
				var selectVal = $(this).find(".selected").attr("value");
				var displayVal = $(this).find(".selected").html();
			}
			else{
				var selectVal = $(this).find(".selected").html();
				var displayVal = selectVal;
			}
			
			if(!selectVal){
				var selectVal = $(this).find(".option:first").html();
				var displayVal = selectVal;
			}
			
			selectVal = selectVal.replace("&amp;", "&");
			displayVal= displayVal.replace("&amp;", "&");
			
			$(this).find("input").val(selectVal);
			$(this).find(".selectedValue").html(displayVal);
		});*/

        $(".inputWrapper .initValue").each( function(){
            var defaultVal = this.value;
            $(this).attr("defaultVal", defaultVal);
        });
	}

	$("#captcha").each( function(){
            var defaultVal = this.value;
            $(this).attr("defaultVal", defaultVal);
        });
		
	$(document).click(function() {
		$(".searchSelect, .searchSelectSmall").find(".selectValues:visible").hide().removeClass("visible");
		$(".searchSelect, .searchSelectSmall").css("z-index",0);
	 });
	
	$(".searchSelect, .searchSelectSmall").live("click", function(event){
		event.stopPropagation();
		$(this).siblings(".searchSelect, .searchSelectSmall").find(".selectValues").hide().removeClass("visible");
		if(!$(this).find(".selectValues").hasClass("visible"))
		{
			$(".selectValues").hide().removeClass("visible");
			$(this).siblings().css("z-index", 0);
			$(this).css("z-index",100000);
			$(".scoutGuideBox .boxMiddle").css("z-index", 0);
			$(this).find(".selectValues").show().addClass("visible");
			
		}
		else
		{
			$(this).find(".selectValues").hide().removeClass("visible");
			$(this).css("z-index",0);
		}
	});
	
	$(".searchSelect .group, .searchSelectSmall .group").live("click", function(){
		return false;
	});
	
	$(".TW-feed").hover( function(){
		$(this).find('ul').show();
	}, function(){
		$(this).find('ul').hide();
	});
	
	$("#filterForm select").live("change", function(){
		var selectVal = $(this).html();
		var useVal = $(this).attr("value");
		$(this).addClass("selected");
		
		if( $(this).parent().siblings("input").val() != useVal){
			$(this).parent().siblings("input").val(useVal);
			if($(this).parents(".filterForm").hasClass("filterForm")){
				$("#filterForm").submit();
			}
		}
		
		$(this).parent().siblings(".selectedValue").html(selectVal);
		
		$(this).parent().hide().removeClass("visible");
		$(this).parent().parent().css("z-index", 0);
		
		if($(this).parents(".selectInput").hasClass("hasDependencies")){
			var useID = $(this).parent().siblings("input").attr("id");
			if(useID){
				$("#"+useID+"_dependent .multiSelectAvailable option[dependencyid!="+useVal+"]").hide();
				$("#"+useID+"_dependent .multiSelectAvailable option[dependencyid="+useVal+"]").show();
			}
		}
		
		return false;
	});
	
	$(".hasDependencies").each( function(){
		useID = $(this).find("select").attr("id");
		if(useID){
			var optionsContainer = $("#"+useID+"_dependent").find(".multiSelectAvailable");
			var hiddenContainer = $("#"+useID+"_dependent").find(".multiSelectHidden");
			
			if($("#"+useID).val()){
				var useValue = $("#"+useID).val();
				
				if(hiddenContainer.hasClass('multiSelectHidden')){
					var $options = $("option", hiddenContainer);
				}
				else{
					var $options = $("option", optionsContainer);
				}
				
				for (var i = 0, l = $options.length; i < l; i++) {
					var dependency = $($options[i]).attr("dependencyid");
					if(dependency == useValue){
						$($options[i]).remove();
						optionsContainer.append($($options[i]));
					}
			    }
				
			}
			else{
				$("#"+useID+"_dependent").find(".multiSelectAvailable").find("option").hide();
			}
		}
	});
	
	$(".radioGroup input[type=radio]").each( function(){
		if($(this).hasClass("enableDependecies")){
			
			items = $(this).siblings(".dependencyItems").find("select");
			
			if($(this).attr("checked") == true){
				items.removeClass("disabled");
				items.find("option").attr("disabled",false);
			}
			else{
				items.addClass("disabled");
				items.css("color","#000");
				items.find("option[value!=0]").attr("disabled",true);
			}
		}
	});
	$(".radioGroup input[type=radio]").click( function(){
		
		items = $(this).siblings(".dependencyItems").find("select");
		
		if($(this).hasClass("enableDependecies")){
			
			if($(this).attr("checked") != "checked"){
				items.removeClass("disabled");
				items.find("option").attr("disabled",false);
			}
			else{
				items.addClass("disabled");
				items.css("color","#000");
				items.find("option[value!=0]").attr("disabled",true);
			}
		}
		else{
			items.addClass("disabled");
			items.find("option[value!=0]").attr("disabled",true);
		}
	});
	
	$(".searchSelect .option, .searchSelectSmall .option").live("hover", function(){
		$(this).siblings(".selected").removeClass("selected");
	});
	
		
	$(".selectWrapper select").live("change", function(){
		var selectVal = $(this).find("option[value="+this.value+"]").html();
		selectVal = selectVal.replace("&amp;", "&");
		$(this).siblings("input").val(selectVal);
	});
	

	$(".inputWrapper .initValue").live("focus", function(){
		var defaultVal = $(this).attr("defaultVal");
		if(this.value == defaultVal)
			$(this).val("");
	});
	
	$(".inputWrapper .initValue").live("blur", function(){
		if(!$(this).val()){
			var defaultVal = $(this).attr("defaultVal");
			$(this).val(defaultVal);
		}
	});
	
	$(".inputWrapper .initValue").live("change", function(){
		if(!$(this).val()){
			var defaultVal = $(this).attr("defaultVal");
			$(this).val(defaultVal);
		}
	});

     $(".inputWrapper .initValue").each( function(){
            var defaultVal = this.value;
            $(this).attr("defaultVal", defaultVal);
        });
    
	$(".hasDependencies select").change( function(){
		var useID = this.id;
		if(useID){
			
			var availableSelect = $("#"+useID+"_dependent .multiSelectAvailable");
			var hiddenSelect = $("#"+useID+"_dependent .multiSelectHidden");
			
			var useValue = this.value;
			
			var $availableOptions = $("option", availableSelect);
			
			for (var i = 0, l = $availableOptions.length; i < l; i++) {
				$($availableOptions[i]).remove();
				hiddenSelect.append($($availableOptions[i]));
			}
			
			var $hiddenOptions = $("option", hiddenSelect);
		
			for (var i = 0, l = $hiddenOptions.length; i < l; i++) {
				var dependency = $($hiddenOptions[i]).attr("dependencyid");
				if(dependency == useValue){
					$($hiddenOptions[i]).remove();
					availableSelect.append($($hiddenOptions[i]));
				}
		    }
			SortSelectBoxOptions(availableSelect.attr("id"));
		}
	});
	
	$(".addButton").click(function() {
		var availableSelect = $(this).parent().siblings(".multiSelectAvailable");
		var activeSelect = $(this).parent().siblings(".multiSelectActive");
		
		if(availableSelect.attr("disabled") == true || activeSelect.attr("disabled") == true)
			return false;
		
		availableSelect.find("option:selected").appendTo(activeSelect);
		availableSelect.find("option:selected").remove();
		
		SortSelectBoxOptions(activeSelect.attr("id"));
		availableSelect.scrollTop(0);
		activeSelect.find("option").attr("selected", "selected");
		return false;
	});

	$(".multiSelectAvailable, .multiSelectActive").each(function(){
		SortSelectBoxOptions(this.id);
		if($(this).hasClass("multiSelectActive")){
			$(this).find("option").attr("selected", "selected");
		}
	});
	
	$(".removeButton").click(function() {
		var availableSelect = $(this).parent().siblings(".multiSelectAvailable");
		var activeSelect = $(this).parent().siblings(".multiSelectActive");
		
		if(availableSelect.attr("disabled") == true || activeSelect.attr("disabled") == true)
			return false;
		
		var hiddenSelect = $(this).parent().siblings(".multiSelectHidden");
		activeSelect.find("option:selected").appendTo(availableSelect).attr("selected", false);
		activeSelect.find("option:selected").remove();
		activeSelect.find("option").attr("selected", true);
		
		var containerID = $(this).parent().parent().parent().attr("id");
		var pos = strpos(containerID, "_dependent");
		
		var mainSelectorID = containerID.substring(0, pos);

		if(mainSelectorID){
			availableSelect.find("option").attr("selected", false);
			
			var useValue = $("#"+mainSelectorID).val();
			
			var $options = $("option", availableSelect);
			
			for (var i = 0, l = $options.length; i < l; i++) {
				var dependency = $($options[i]).attr("dependencyid");
				if(dependency != useValue){
					$($options[i]).remove();
					hiddenSelect.append($($options[i]));
				}
		    }
		}
		
		SortSelectBoxOptions(availableSelect.attr("id"));
		
		activeSelect.scrollTop(0);
		
		return false;
	});
	
	function strpos (haystack, needle, offset) {
		var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
	    return i === -1 ? false : i;
	}
	
	$(".socialLink").click(function() {
		if ( $("#s_" + this.id).is(":visible") )
		$("#s_" + this.id).hide();
		else
			$("#s_" + this.id).show();
				
			return false;
	});
	
	$(".socialLinkBoxClose").click( function() {
		$(this).parent().hide();
	});
	
	$(".searchFilter").change(function() {
		$("#filterForm").submit();
	});
	
	if($(".artPrint").length > 0)
	{
		$(".artPrint").click( function() {
			 window.print();
			 return false;
		});
	}
	
	 $("#fldcopy_details_from_company").click( function() {
		if ($(this).attr("checked"))
		{
			$("#fldadmin_address").val($("#fldaddress").val());
			$("#fldadmin_city").val($("#fldcity").val());
			
			$("#fldadmin_state_id option:selected").attr("selected", "");
			
			$("#fldadmin_state_id option[value=" + $("#fldstate_id").val() + "]").attr("selected", "selected");
			selectVal = $("#fldadmin_state_id option[value=" + $("#fldstate_id").val() + "]").html();
			$("#fldadmin_state_id").siblings("input[type=text]").val(selectVal);
			
			$("#fldadmin_zip_code").val($("#fldzip_code").val());
			$("#fldadmin_phone").val($("#fldphone").val());
		}
		else
		{
			$("#fldadmin_address").val("");
			$("#fldadmin_city").val("");
			$("#fldadmin_zip_code").val("");
			$("#fldadmin_phone").val("");
		}
	 });
	
	$(".landingPageItemsMoreLink").click(function() {
		if ( $("#s_" + this.id).is(":visible") )
			$("#s_" + this.id).hide();
		else
			$("#s_" + this.id).show();
				
		return false;
	});
	
	$("#delete_fldlogo").click(function () {
		$("#delete_fldlogo").parent(0).load("/en/deletelogoimage/");
	});
	
	if($('#emailToFriend').length > 0)
	{
		windowWidth = 730; windowHeight = 480;
		
		$('#emailToFriend').openDOMWindow({ 
				height:windowHeight, 
				width:windowWidth, 
				positionType:'centered', 
				eventType:'click', 
				windowSource:'iframe', 
				windowPadding:0, 
				loader:1, 
				loaderImagePath:'/resources/images/loader.gif', 
				loaderHeight:19, 
				loaderWidth:220 
			});
	}	
	
	$("#submit_fldsubmit, #submit_fldlast_tab, #submit_fldsave, #submit_fldnext_tab").click( function(){
			$("select.multiSelectActive option").attr("selected", "selected");
			$("body").addClass("loading");
	});
	
	$(".formTabs a[href!='#']").click( function(){
		$("body").addClass("loading");
		$("select.multiSelectActive option").attr("selected", "selected");
	});	
	
	$(".sidebarSearchBox").load( "/content/search-box", "", function() {	
		applySearchSelections();		
	});
});

function SwitchLinksHandler()
{
	if($(".switch_to_industry_updates").length > 0) {
		$(".switch_to_industry_updates").click(function(){
			$("#industry-updates").css("display", "block");
			$("#expert-advice, #pump-tweets").css("display", "none");
			return false;
		});
	}
	
	if($(".switch_to_expert_advice").length > 0) {
		$(".switch_to_expert_advice").click(function(){
			$("#expert-advice").css("display", "block");
			$("#industry-updates, #pump-tweets").css("display", "none");
			return false;
		});
	}
	
	if($(".switch_to_pump_tweets").length > 0) {
		$(".switch_to_pump_tweets").click(function(){
			$("#pump-tweets").css("display", "block");
			$("#expert-advice, #industry-updates").css("display", "none");
			return false;
		});
	}
}

function closeIframe() {
	$("#DOMWindowOverlay, #stwrapper, #DOMWindow").fadeOut().remove();
}
function showDiv(objectID) {
	var theElementStyle = document.getElementById(objectID);
	
	if(theElementStyle.style.display == "block"){
		theElementStyle.style.display = "none";
	}else{
		theElementStyle.style.display = "block";
	}
	
	if(objectID == 'TW-Pop_4' &&  jQuery.browser.msie && jQuery.browser.version == 7.0)
	{
		$(".boxBottom").css('z-index', '-1');
	}
}
function textCounter(field, maxlimit) {
	var infoText = " characters used";
	infoText = "/" + maxlimit.toString() + infoText;
	
	charsCountField = document.getElementById(field.name + "_leftChars");
	
	if (field.value.length > maxlimit) 
	{	
		field.value = field.value.substring(0, maxlimit);
	}	
	else
	{	
		charsCountField.innerHTML = (field.value.length).toString() + infoText;
	}	
}

function SortSelectBoxOptions(field) {
	var $useID = $('#'+ field +'');
	if ($useID.length > 0) {
		var $options = $('option, optgroup option', $useID);
	    var arrVals = [];
	    $options.each(function(){
	        arrVals.push({
	        	position: $(this).attr("position"),
	            val: $(this).val(),
	            text: $(this).text(),
	            parentID: $(this).attr("parentid"),
	            optClass: $(this).attr("class"),
	            dependencyID: $(this).attr("dependencyid")
	        });
	    });
	
	    arrVals.sort(function(a, b){
	        return a.position - b.position;
	    });
	    for (var i = 0, l = arrVals.length; i < l; i++) {
	        $($options[i]).val(arrVals[i].val);
	        $($options[i]).html(arrVals[i].text);
	        $($options[i]).attr("position", arrVals[i].position);
	        
	        
	        if(arrVals[i].optClass){
	        	$($options[i]).addClass(arrVals[i].optClass);
	        }
	        else{
	        	$($options[i]).removeClass("generalOption");
	        }
	        if(arrVals[i].dependencyID){
        		$($options[i]).attr("dependencyid", arrVals[i].dependencyID);
        	}
        	
	        if(arrVals[i].parentID){
	        	var group = $("optgroup[value="+ arrVals[i].parentID+"]", $useID);
	        	group.show();
	        	if(group && group.attr("value") == arrVals[i].parentID){
		        	$($options[i]).attr("parentid", arrVals[i].parentID);
		        	$($options[i]).remove();
	        		group.append($($options[i]));
	        	}
	        }
	    }
	}
	
	
}


