
var $j = jQuery.noConflict();

$j.fn.dropdown = function() {	
	$j(this).hover(function(){
		$j(this).addClass("hover");
		$j('> .menuitem',this).addClass("open");
		$j('ul:first',this).css('visibility', 'visible');
		
		/* IE6 hack for overlay and <select> */
		var subMenuHeight = $j(this).find('ul').height();
		var subMenuWidth = $j(this).find('ul').width();
		$j(this).find('ul').append('<iframe></iframe>');
		$j(this).find('iframe').css('height', subMenuHeight);
		$j(this).find('iframe').css('width', subMenuWidth);
	},function(){
		$j(this).removeClass("hover");
		$j('.open',this).removeClass("open");
		$j('ul:first',this).css('visibility', 'hidden');
		$j(this).find('iframe').remove();
	});

};

$j(document).ready(function(){
		
	$j("ul.dropdown li").dropdown();
	
	
	/*** Scrolling Sign In box ***/
	$j("#signinControl").toggle(
		function() {				
			$j("#signinForm").show().animate({
				height: "130px"
			}, 1200, "easeOutExpo" );
			$j(this).addClass('signinControl_open');
			
			/* Hack for IE6 (iframe and select) - start */
			$j("#signinForm iframe").show().animate({
				height: "190px"
			}, 1200, "easeOutExpo" );
			/* Hack for IE6 (iframe) - end */
			
			/*
			$j("#signinBottom").animate({
				marginTop: "180px"
			}, 1200, "easeOutExpo" ); */
		},
		function () {
			$j("#signinForm").hide().animate({
				height: "0px"
			}, 1200, "easeOutExpo" );
			$j(this).removeClass('signinControl_open');
			
			
			/* Hack for IE6 (iframe and select) - start */
			$j("#signinForm iframe").hide().animate({
				height: "0px"
			}, 1200, "easeOutExpo" );
			/* Hack for IE6 (iframe) - end */
			
			/* 
			$j("#signinBottom").animate({
				marginTop: "-0px"
			}, 1200, "easeOutExpo" ); */
		}
	);
	
	
	/*** Add down arrow image to dropdown menu items ***/
	$j("li.menuitem > a").append("<span class='dropArrow'></span>");
	/***/
	
	
	/*** Dropdown Navigation Menus - start ***/
	$j('li.menuitem').hover(
		function () {
			//show its submenu
			var leftPosition = ($j(this).width() - $j('ul', this).width()) / 2;
			$j('ul', this).css("left", leftPosition);		
			//$j('ul', this).dropShadow({left: -1, top: 3, opacity: 0.8, blur: 2});
		},
		function () {
			//$j('div.dropShadow', this).remove();
		}
	);
	/*** Dropdown Navigation Menus - end ***/
	
	
	/*** Main Navigation - maintain active button on submenu hover  ***/
	$j("ul.subMenu").hover (
		function() {
			$j(this).parent().addClass('subMenuOpen');			
		},
		function() {
			$j(this).parent().removeClass('subMenuOpen');			
		}
	);
	$j("li.menuitem p").hover (
		function() {
			$j(this).parent().addClass('subMenuOpen');
		},
		function() {
			$j(this).parent().removeClass('subMenuOpen');
		}
	);
	
	
	/*** Featured Listing Tabbed Widget - start ***/
	$j("#featuredTab1 a").addClass('active');
	$j("#featuredContent2").hide();

	$j("#featuredTab1 a").click(function() {				
		$j("#featuredContent1").show();
		$j("#featuredContent2").hide();
		$j("#featuredTab1 a").addClass('active');
		$j("#featuredTab2 a").removeClass('active');
		return false;
	});
	
	$j("#featuredTab2 a").click(function() {				
		$j("#featuredContent1").hide();
		$j("#featuredContent2").show();
		$j("#featuredTab1 a").removeClass('active');
		$j("#featuredTab2 a").addClass('active');
		return false;
	});
	/*** Featured Listing Tabbed Widget - end ***/
	
	
	
	/*** Search Page Tabbed Results - start ***/
	
	/* first check if 'active' class already exists */
	var activeTab = $j("#searchResultsTabs li.active").attr('id');
	if(activeTab == "searchResultsTab1") {
		$j("#searchResults_listings").hide();		
	} else if(activeTab == "searchResultsTab2") {
		$j("#searchResults_articles").hide();		
	} else {
		$j("#searchResultsTab1").addClass('active');
		$j("#searchResults_listings").hide();
	}

	$j("#searchResultsTab1 a").click(function() {				
		$j("#searchResults_articles").show();
		$j("#searchResults_listings").hide();
		$j("#searchResultsTab1").addClass('active');
		$j("#searchResultsTab2").removeClass('active');
		return false;
	});
	
	$j("#searchResultsTab2 a").click(function() {				
		$j("#searchResults_articles").hide();
		$j("#searchResults_listings").show();
		$j("#searchResultsTab1").removeClass('active');
		$j("#searchResultsTab2").addClass('active');
		return false;
	});
	/*** Search Page Tabbed Results - end ***/

	
	/*** Reverse the Stacking Order - IE6, IE7 hack for Sign In overlay ***/

	
	
	/*** Modular popup for Manage Listings Revision ***/
	$j('div.linkReviseChanges a').click(function() {
		var modalURL = $j(this).attr("href");		
		modalpop(modalURL);
		return false;
	});
	
	$j('div.linkReviseChanges_close a').click(function() {
		window.close();
	});
	
	/*** Modular popup for Manage Listings Revision ***/
	$j('div.linkHistory a').click(function() {
		var modalURL = $j(this).attr("href");		
		modalpop(modalURL);
		return false;
	});
	
	$j('div.linkHistory_close a').click(function() {
		window.close();
	});
});

function check_file_upload(extensions, fileUploadId) {
	var filename = document.getElementById(fileUploadId).value;
	if (filename == '') {
		alert("Please select a file.");
		return false;
	} 
	var extensionTab = extensions.split(', ');
  	var ext = filename.split('.').pop().toLowerCase();
  	var extensionIndex = $j.inArray(ext, extensionTab)
  	if (extensionIndex == -1) {
    	alert("Invalid file extension, please select another file.");
        return false;
	}
  	return true;
}

function showPartialClassesOfInsurance() {
	jQuery('#allClassesOfInsurance').hide();
	jQuery('#partialClassesOfInsurance').show();
}

function showAllClassesOfInsurance() {
	jQuery('#allClassesOfInsurance').show();
	jQuery('#partialClassesOfInsurance').hide();
}


/*** Modal popup script ***/
function modalpop(url) {
	newwindow=window.open(url,'name','height=700,width=960,scrollbars=1,resizable=1');
	if (window.focus) {newwindow.focus()}
	return false;
}
	

/***Show Email popup script ***/
function showEmailPopup(productName) {
	var url = window.location; 
	document.getElementById("emailForm:productUrl").value = url;
	document.getElementById("emailForm:productName").value = productName;
	Richfaces.showModalPanel('email-panel');
	return false;
}

function hideEmailPopup() {
	document.getElementById("emailForm:emailNameField:emailName").value = '';
	document.getElementById("emailForm:friendEmailField:friendEmail").value = '';
	document.getElementById("emailForm:emailMessageField:emailMessage").value = '';
	document.getElementById("emailForm:verifyCaptchaField:verifyCaptcha").value = '';
	Richfaces.hideModalPanel('email-panel');
	return false;
}

/***Show Email popup script ***/
function showConfirmEmailPostPopup() {
	hideEmailPopup();
	Richfaces.showModalPanel('email-confirm-panel');
	return false;
}

function defineYearToDelete(year) {
	document.getElementById("financialInformationSuppressionForm:yearToDelete").value = year;
}

/***********************************************
* Textarea Maxlength script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";
	var value = obj.value;
	 
	if (value.length > 0) { 
		var valueLength = value.length;
		var subStringValue = mlength;
		//To persist data, we count \n as two char
		if (value.indexOf('\n') > -1) {
			//test new row
			var splitResult = value.split('\n');
			valueLength = valueLength + splitResult.length
			subStringValue = subStringValue - splitResult.length;
		}
		if (obj.getAttribute && valueLength>mlength) {
			obj.value=obj.value.substring(0,subStringValue);
		}

	} 
	
}

