//Miscellaneuous Functions

$(document).ready(function(){
	
	if((location.pathname.indexOf('site_manage')<0) && (location.pathname.indexOf('login')<0) && (location.pathname.indexOf('logout')<0)) $("#container").css('height', '625px'); //FOR IE
	if(location.pathname.indexOf('login')>0 || location.pathname.indexOf('logout')>0) $("#container").css('height', '250px'); //FOR IE
	
	var i, $this;
	var query, q_value, links, l_id;
	var height=parseInt($("#container").css('height'));
	var width=parseInt($("#container").css('width'));
	var w_height, w_width, m_top, m_left;
	
	var FF_corrector=5;
	
	var field_width;
	
	//Change Color of link for current page.
	function currentPage() {
		query=window.location.search.split('=');
		q_value=query[1];
		
		updateLinkDisplay(q_value);
		
		return q_value;
	}
	
	function updateLinkDisplay(qry){
		links=$("a").get();

		for(i=0; i<links.length; i++){
			l_id=$(links[i]).attr("id");
			if(l_id==qry) links[i].style.color="#EE400F";
		}
	}
	
	//Toggle location_type for retailer page
 	function toggleLoc(){
	var locSelect=$("select.locSelect");
	var zipInput = $("input#zip");
	var curLoc;
	
	$("input[type='radio']").change(function(evt){
		curLoc = $(evt.target).attr('id');
		
		if(curLoc=='loc_country'){
			zipInput.attr('disabled', 'disabled');
		} else {
			zipInput.removeAttr('disabled');
		}
		
		locSelect.each(function(i){
			$this=$(this);

			if($this.attr('disabled')){
				$this.removeAttr('disabled');
			} else {
				$(this).attr('disabled', 'disabled');
			}
		});
		
		
	});
}
	
	//Center DIV both vertically and horizontally
	function centerDIV(div) {
		if(!($("#manage").is('div'))) {
			w_height=$(window).height();
			w_width=$(window).width()
			m_top = (w_height - height)/2;
			if(w_height<=height) m_top = 0;
			m_left = (w_width - width)/2;
			if(w_width<=width) m_left = 0;
		
			div.css({
				'margin-top' : m_top + 'px',
				'margin-left' : m_left + 'px' 
			});
		} //ONLY FOR FRONTEND
	}
	
	
	function spaceLinks(e){
		var elems=e.get();
		if(elems.length>0){
		var wi=parseInt($("#footer ul").css('width'));
		var m_right;
		var f=0;
		for(i=0; i<elems.length; i++){
			f+=$(elems[i]).width();
			}
			//alert($(elems[0]).css('width'));
		m_right=Math.floor((wi-f)/(elems.length-1));
		//alert(wi + '/' + f + '/' + (elems.length-1) + '/' + m_right);
		for(i=0; i<elems.length; i++){
		if(i<elems.length-1)$(elems[i]).css({'margin-right' : m_right + 'px'});	
			}
		//Margin-right must be adjusted on FF. I think this is because FF doesn't round the width values for text.
		//** CHECK ALL MAJOR BROWSERS ON PC PLATFORM
		//Make Margin-Right slightly smaller for 'Links' link on FF.
		if(navigator.userAgent.indexOf("Firefox") >=0) $(elems[8]).css('margin-right', (m_right - FF_corrector) + "px");
		}
	}
	
	//Anchor a DIV to the bottom of another DIV, additional offset
	function anchorDIV(anchor, floor, offset) {
	if($(anchor).is('div')) {
		var a_height=$(anchor).height();
		var f_height=$(floor).height();
		$(anchor).css('margin-top', (f_height-a_height + offset) + 'px');
		};
	}
	
	//Find number of fields for various categories and divide column width accordingly
	function getFieldNumber(){
		if($("#manage").is('div')){
			var h4s=$("div.listings h4").get();
			field_width=Math.floor(100/h4s.length);
			$(".listings h4").css('width', field_width + "%");
			$(".listings p").css('width', field_width + "%");
		}
	}

	var cp = currentPage();
	
	if(cp=='retailer') toggleLoc();
	 
	centerDIV($("#container")); 
	spaceLinks($("#footer li")); 
	anchorDIV($("#contactLeft"), $("#contact"), -25); 
	anchorDIV($("#joinUs"), $("#contact"), -55);
	getFieldNumber();
	$(window).resize(function(){
		centerDIV($("#container"));
	});
		
}); 
