var myVerticalSlide, catVerticalSlide
window.addEvent('domready', function() { 
  	if ($('login_form_block'))  initLoginForm($('login_form_block'))
  	if ($('ifr')) $('ifr').src="" ;   
	if ($('search_cats')){
	 	$('search_cats').isopen = 0 ;
		$('search_cats').setStyles({"opacity":"0"})
		$('search_cats').setStyles({"display":"block"})  
		// href="javascript:void(0)" onclick="toggle_search_cats()"
	  	if ($('bus_cat_select_box'))$('bus_cat_select_box').addEvent("mousedown", function(e) {  
		    e.stop();
		     e.stopPropagation();   
			toggle_search_cats();
		})
	  
		
	}   
	
	if ($('search_cats_l')){
	 	$('search_cats_l').isopen = 0 ;
		$('search_cats_l').setStyles({"opacity":"0"})
		$('search_cats_l').setStyles({"display":"block"})  
		// href="javascript:void(0)" onclick="toggle_search_cats()"
	  	if ($('bus_cat_select_box_l'))$('bus_cat_select_box_l').addEvent("mousedown", function(e) {  
		    e.stop();
		     e.stopPropagation();   
			toggle_search_cats_l();
		})


	}
	
	
	if ($('search_names')){
	 	$('search_names').isopen = 0 ;
		$('search_names').setStyles({"opacity":"0"})
		$('search_names').setStyles({"display":"block"})  
		// href="javascript:void(0)" onclick="toggle_search_cats()"
	  	if ($('bus_name_select_box'))$('bus_name_select_box').addEvent("mousedown", function(e) {  
		    e.stop();
		     e.stopPropagation();   
			toggle_search_names();
		})
	  
		
	} 
	
	
	
	   
	
	if ($('email_form_container')) {
		$('email_form_container').setStyles({"visibility":"visible"})
			myVerticalSlide = new Fx.Slide('email_form_container',{ 	
				duration: 1000,
				transition: Fx.Transitions.Pow.easeOut
				});

			myVerticalSlide.hide()
		   $('email_form_container').setStyles({"display":"block"})

			myVerticalSlide.addEvent('complete', function() {
		 //    	alert($('email_form_container').get("classname"));
			});
	} 
	
	//tooltips
	
	//when the dom is ready

		//store titles and text
		$$('a.tipz').each(function(element,index) {   
			var content = element.get('title').split('::');
			element.store('tip:title', content[0]);
			element.store('tip:text', content[1]);
		});

		//create the tooltips
		var tipz = new Tips('.tipz',{
			className: 'tipz',
			fixed: true,  
			offsets: {'x':0, 'y':-60},
			hideDelay: 50,
			showDelay: 50
		});  
		

		tipz.addEvents({
			'show': function(tip) {   
				 tip.fade('in');
			},
			'hide': function(tip) {
			  tip.fade('out');
			}
		});
		
		  
/* */

});   
          

 
function toggle_search_names(){
   // var h = $('search_name').getSize();   
	var p = $('bus_name_select_box').getPosition();   
	var s = $('bus_name_select_box').getSize();   
	var pd = $('sidebar_r').getPosition();   
	var sc = $('search_names').getSize(); 
	
   // alert("height: " )                                       
   
 $('search_names').setStyles({
	"margin-left": (p.x - pd.x - sc.x + 177),
	"margin-top": 27
	});
	
	
 
/**/ 

	    if (!$('search_names').isopen) {   
		
			$('search_cats').fade("out") ;  
			 $('bus_cat_select_box').setStyles({
					"background-position":"0 0px", 
					"color" : "#000"
					})        
			$('search_cats').isopen = 0
			
			
			$('search_names').fade("in") ;
			$('search_names').isopen = 1   
			$('bus_name_select_box').setStyles({
				"background-position":"0 -30px", 
				"color" : "#fff"
				})
		}
	 	else {
			$('search_names').fade("out") ; 
			$('bus_name_select_box').setStyles({
					"background-position":"0 0px", 
					"color" : "#000"
					})        
			$('search_names').isopen = 0
		}
	if ($('search_names').isopen == 1) {
		$('doc').addEvent("mousedown", toggle_search_names)
	}      else {
		 $('doc').removeEvent("mousedown", toggle_search_names);
	}
	//catVerticalSlide.toggle() 
}
      



function toggle_search_cats(){
	var p = $('bus_cat_select_box').getPosition();   
	var sc = $('search_cats').getSize(); 
	var s = $('bus_cat_select_box').getSize();   
	var pd = $('sidebar_r').getPosition();   
	                                       
	
   $('search_cats').setStyles({
	"margin-left": (p.x - pd.x - sc.x + 177),
	"margin-top": 27
	});
   // alert(l)
	   // $('search_cats').setStyles({margin-left:"200px"})
	    if (!$('search_cats').isopen) {   
		
			$('search_names').fade("out") ; 
			$('bus_name_select_box').setStyles({
					"background-position":"0 0px", 
					"color" : "#000"
					})        
			$('search_names').isopen = 0
			
			
			$('search_cats').fade("in") ;  
			$('bus_cat_select_box').setStyles({
				"background-position":"0 -30px", 
				"color" : "#fff"
				})
			$('search_cats').isopen = 1
		}
	 	else {
			$('search_cats').fade("out") ;  
			 $('bus_cat_select_box').setStyles({
					"background-position":"0 0px", 
					"color" : "#000"
					})        
			$('search_cats').isopen = 0
		}
	if ($('search_cats').isopen == 1) {
		$('doc').addEvent("mousedown", toggle_search_cats)
	}      else {
		 $('doc').removeEvent("mousedown", toggle_search_cats);
	}
	//catVerticalSlide.toggle() 
}   


function toggle_search_cats_l(){
	var p = $('bus_cat_select_box_l').getPosition();   
	var w = window.getSize();   
	var sc = $('search_cats_l').getSize(); 
	var s = $('bus_cat_select_box_l').getSize();   
	var pd = $('d_center_content').getPosition();   
	                                       
	
   $('search_cats_l').setStyles({
	"margin": "27px auto 0 auto" 
	});
   // alert(l)
	   // $('search_cats').setStyles({margin-left:"200px"})
	    if (!$('search_cats_l').isopen) {   
		
			$('search_cats_l').fade("in") ;  
			$('bus_cat_select_box_l').setStyles({
				"background-position":"0 -30px", 
				"color" : "#fff"
				})
			$('search_cats_l').isopen = 1
		}
	 	else {
			$('search_cats_l').fade("out") ;  
			 $('bus_cat_select_box_l').setStyles({
					"background-position":"0 0px", 
					"color" : "#000"
					})        
			$('search_cats_l').isopen = 0
		}
	if ($('search_cats_l').isopen == 1) {
		$('doc').addEvent("mousedown", toggle_search_cats_l)
	}      else {
		 $('doc').removeEvent("mousedown", toggle_search_cats_l);
	}
	//catVerticalSlide.toggle() 
}


function numbersOnly(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode                  
         if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
            return false;

         return true;
      }


function setReminder(id, loggedIn){ 
	if (!loggedIn) {alert("You must be logged in to set reminders."); return;}
	var req = new Request({  
	            method: 'post',  
	            url: '/auctions/setReminder/'+id,  
	            data: {  },  
	            onComplete: function(response) {
		 		   if (!response) alert('Didnt work');
					else {
				   	var div = new Element('div', {   
						 'id': 'rem_'+id,
					     'class': 'reminded',
						 'html' : "Reminder Set"
					    }); 
					if ($('rem_'+id)) div.replaces($('rem_'+id))
					} 
			    }        
	 }).send();
} 


function setWatch(id, loggedIn){ 
	if (!loggedIn) {alert("You must be logged in to watch auctions."); return;}
	var req = new Request({  
	            method: 'post',  
	            url: '/auctions/setWatch/'+id,  
	            data: {  },  
	            onComplete: function(response) {
		 		   if (!response) alert('Didnt work');
					else {
				   	var span = new Element('span', {   
						 'id': 'watch_'+id,  
						 'class' : 'watching',
						 'html' : "<div class='eye'></div>You are watching this auction"
					    });
					if ($('watch_'+id)) span.replaces($('watch_'+id))
					} 
			    }        
	 }).send();
} 


function removeReminder(id){ 
	var req = new Request({  
	            method: 'post',  
	            url: '/auctions/removeReminder/'+id,  
	            data: {  },  
	            onComplete: function(response) {
		 		   if (!response) alert('Didnt work');
					else {
				   	var li = "r_"+id; 
					var el = $(li) ;
					el.set("class","deleted");
					
					var li = "ra_"+id; 
					var el = $(li) ;
					el.destroy();
					} 
			    }        
	 }).send();
}



function removeWatch(id){ 
	var req = new Request({  
	            method: 'post',  
	            url: '/auctions/removeWatch/'+id,  
	            data: {  },  
	            onComplete: function(response) {
		 		   if (!response) alert('Didnt work');
					else {
				   	var li = "w_"+id; 
					var el = $(li) ;
					el.set("class","deleted");
					
					var li = "wa_"+id; 
					var el = $(li) ;
					el.destroy();
					} 
			    }        
	 }).send();
}     

function processLoginForm(f){
  // if () 
}

function initLoginForm(f){  
	f.username.value="email address" ;
	f.password.value="password" ;
	f.username.onfocus = function(){
		if (f.username.value == "email address") f.username.value = "" ;
	}
	f.username.onblur = function(){
		if (f.username.value == "") f.username.value = "email address" ;
	}
	
	if (!document.all){
		// password - toggle between input types
		f.password.type = "text" ;
		f.password.onfocus = function(){ 
			f.password.type = "password" ;
			if (f.password.value == "password") f.password.value = "" 
		}
	
		f.password.onblur = function(){
			if (f.password.value == "") {
				f.password.type = "text" ;
				f.password.value = "password" ;
			}
		}
	} else {
		f.password.onfocus = function(){ 
			if (f.password.value == "password") f.password.value = "" 
		}
	
		f.password.onblur = function(){
			if (f.password.value == "") {
				f.password.value = "password" ;
			}
		}
	}
}    



// script by Josh Fraser (http://www.onlineaspect.com)

function calculate_time_zone() {
	var rightNow = new Date();
	var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);  // jan 1st
	var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st
	var temp = jan1.toGMTString();
	var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	temp = june1.toGMTString();
	var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
	var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
	var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
	var dst;
	if (std_time_offset == daylight_time_offset) {
		dst = "0"; // daylight savings time is NOT observed
	} else {
		// positive is southern, negative is northern hemisphere
		var hemisphere = std_time_offset - daylight_time_offset;
		if (hemisphere >= 0)
			std_time_offset = daylight_time_offset;
		dst = "1"; // daylight savings time is observed
	}
	var i;
	// check just to avoid error messages 
	return (tz_convert(std_time_offset)) ;
	if (document.getElementById('timezone')) {
		for (i = 0; i < document.getElementById('timezone').options.length; i++) {
			if (document.getElementById('timezone').options[i].value == convert(std_time_offset)+","+dst) {
				document.getElementById('timezone').selectedIndex = i;
				break;
			}
		}
	}
}

function tz_convert(value) {
	var hours = parseInt(value);
   	value -= parseInt(value);
	value *= 60;
	var mins = parseInt(value);
   	value -= parseInt(value);
	value *= 60;
	var secs = parseInt(value);
	var display_hours = hours;
	// handle GMT case (00:00)
	if (hours == 0) {
		display_hours = "00";
	} else if (hours > 0) {
		// add a plus sign and perhaps an extra 0
		//display_hours = (hours < 10) ? "+0"+hours : "+"+hours;
		display_hours = hours;
	} else {
		// add an extra 0 if needed 
		display_hours = (hours > -10) ? "-"+Math.abs(hours) : hours;
	}
	
	mins = (mins < 10) ? "0"+mins : mins;
	return display_hours // +":"+mins;
}

//onload = calculate_time_zone;


function expandSELECT(sel) {
//  $(sel).set("className","selectWide");
}


function contractSELECT(sel) {
 // $(sel).set("className","selectNarrow");
}

function sendEmailToAFriend(f, loggedin){ 
	if (!loggedin) {
		alert("You need to be logged in to send hot deals.");
		return false;
	}
   if (f.share_name.value != "" && f.share_email.value !="" && isValidEmail(f.share_email.value) ) {
	var req = new Request({  
	            method: 'post',  
	            url: '/auctions/sendToFriend/',  
	            data: {share_name: f.share_name.value,
		  				share_email: f.share_email.value,
						auction_id: f.auction_id.value},  
	            onComplete: function(response) {
		 		   
				   	var div = new Element('div', {   
						 'id': 'email_response',  
						 'class' : 'email_response',
						 'html' : "<div class='ok'></div>Thanks for sharing with "+f.share_name.value+"."
					    });
					if ($('email_response')) div.replaces($('email_response')) ;
					 togSlide()
					} 
			           
	 }).send();
	
}    else {
	  alert("Both the name and email need to be filled in");
} 
return false;
}       

var couponWin
function printCoupon(id) {
	couponWin = window.open('/directories/printCoupon/'+id, "Print-Coupon", "width=500,height=300,top=100,left=100,status=no,toolbars=no,location=no") ;
	if (couponWin) couponWin.focus();  
} 

var topen=0;
function togSlide(){
	topen = topen == 1 ? 0 : 1 ;
    if (open) $('hlink').setStyles({"background-color":"#ffffff"})  ;
    else $('hlink').setStyles({"background-color":"#f0f0f0"})  ;
     myVerticalSlide.toggle()
}

function googlize(str) {
	return str.replace(' ','+');
	return str;
}
function isValidEmail(str) {

   var cool =  (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
   if (!cool) {
		alert("You need to be logged in to send hot deals.");
		return false;
	}
	return true;
}      
function getMap(address_data){
	var src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=";
	src += googlize(address_data[0]) + "," + googlize(address_data[2]) + "," + googlize(address_data[3]);
   src += "&amp;sll=32.012739,-80.843583&amp;sspn=0.048034,0.052357&amp;gl=us&amp;ie=UTF8&amp;z=14&amp;iwloc=addr&amp;ll=32.023359,-80.839376&amp;output=embed&amp;s=AARTsJqQTLc6GGFlLHhP9eWGOwapGoSy8Q" ;
  
 var html ='<iframe width="580" height="300" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=1200+market+street+san+francisco;iwloc=addr&amp;output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=1200+market+street+san+francisco&amp;sll=37.773508,-122.421188&amp;sspn=0.09376,0.126858&amp;ie=UTF8&amp;ll=37.787539,-122.412758&amp;spn=0.023741,0.036478&amp;z=14&amp;iwloc=addr" style="color:#0000FF;text-align:left">View Larger Map</a></small>';
     
 



  	var i = new Element('div', {   
		 'id': 'mapit',  
		 'class' : 'map',
		 'html' : html
	    });
	i.replaces($('mapit')) 
}




Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}
