// JavaScript Document
var showing = false,
	shareShowing = false,
	sharebtnShowing = new Array,
    allShareBtns = new Array,
	sharebtnTimeout,
	multiShareTimeout,
	dropdownShowing = false,
	shareHover = false,

	showing1 = false,
	showing2 = false,
	showing3 = false,
	showing1Timeout,
	showing2Timeout,
	showing3Timeout,

	loginShowing = false,
	shareTimeout;

function initialize(){
/*
		allPhotos = $$('a.photos');
        allShareBtns = $$('div.pmi-ad-outer-container');
        
		allPhotos.each(
			function(e, index){
				Event.observe(e, 'mouseover', function(e){e.stop(); allShareBtns[index].show();}, false);
				Event.observe(e, 'mouseout', function(e){e.stop(); hideSharebtn(index); }, false);
			}
		)
        
		allSharebtnDropdowns = $$('a.sharebtn');
		allSharebtnDropdowns.each(
			function(e, index){
				Event.observe(e, 'click', function(e){e.stop();toggleSharebtn(index);}, false);dropdownShowing = false;
				Event.observe(e, 'mouseover', function(e){e.stop();shareStay(index);clearTimeout(sharebtnTimeout);}, false);
				//Event.observe(e, 'mouseout', function(e){ e.stop();shareHide(index); });
			}
		)
        */
		allFBbtns = $$('a.fb_dd_btn');
		allFBbtns.each(
			function(e, index){
				Event.observe(e, 'mouseover', function(e){e.stop();shareStay(index);clearTimeout(sharebtnTimeout);clearTimeout(multiShareTimeout);shareHover = true;}, false);
				Event.observe(e, 'mouseout', function(e){ e.stop();shareddHide(index); });
			}
		)
		allTWbtns = $$('a.tw_dd_btn');
		allTWbtns.each(
			function(e, index){
				Event.observe(e, 'mouseover', function(e){e.stop();shareStay(index);clearTimeout(sharebtnTimeout);clearTimeout(multiShareTimeout);shareHover = true;}, false);
				Event.observe(e, 'mouseout', function(e){ e.stop();shareddHide(index); });
			}
		)
		allMYbtns = $$('a.my_dd_btn');
		allMYbtns.each(
			function(e, index){
				Event.observe(e, 'mouseover', function(e){e.stop();shareStay(index);clearTimeout(sharebtnTimeout);clearTimeout(multiShareTimeout);shareHover = true;}, false);
				Event.observe(e, 'mouseout', function(e){ e.stop();shareddHide(index); });
			}
		)
		allEMbtns = $$('a.em_dd_btn');
		allEMbtns.each(
			function(e, index){
				Event.observe(e, 'mouseover', function(e){e.stop();shareStay(index);clearTimeout(sharebtnTimeout);clearTimeout(multiShareTimeout);shareHover = true;}, false);
				Event.observe(e, 'mouseout', function(e){ e.stop();shareddHide(index); });
			}
		)
		if ($('prev_contest_btn')){Event.observe('prev_contest_btn', 'click', showContestResults, false)};
		if ($('login_link')){Event.observe('login_link', 'click', toggleLoginPopup, false)};
		if ($('close_login')){Event.observe('close_login', 'click', toggleLoginPopup, false)};
		if ($('close_welcome')){Event.observe('close_welcome', 'click', closeWelcome, false)};
		if ($('other_categories_btn')){Event.observe('other_categories_btn', 'click', toggleOtherCategories, false)};
		if ($('by_location_btn')){Event.observe('by_location_btn', 'click', toggleByLocation, false)};
		if ($('by_product_btn')){Event.observe('by_product_btn', 'click', toggleByProduct, false)};
		if ($('share')){Event.observe('share', 'click', toggleShare, false)};
		if ($('share2')){Event.observe('share2', 'click', toggleShare, false)};
		if ($('tos')){Event.observe('tos', 'click', showTOS, false)};
		if ($('tos')){Event.observe('footer_nav_6', 'click', showTOS2, false)};


        //setup a js timeout function that can be called when the user mouses out of the share dropdown
        if ($('share_dropDown')){
            Event.observe($('share_dropDown'), 'mouseover', function(){ clearTimeout(shareTimeout); });
            Event.observe($('share_dropDown'), 'mouseout', function(){ shareTimeout = setTimeout('timeOutShareDropdown()', 1500); });
        }

        if ($('contest_prev')){
            Event.observe($('contest_prev'), 'mouseover', function(){ clearTimeout(showing1Timeout); });
            Event.observe($('contest_prev'), 'mouseout', function(){ showing1Timeout = setTimeout('timeOutShare1Dropdown()', 1500); });
        }
        if ($('by_location')){
            Event.observe($('by_location'), 'mouseover', function(){ clearTimeout(showing2Timeout); });
            Event.observe($('by_location'), 'mouseout', function(){ showing2Timeout = setTimeout('timeOutShare2Dropdown()', 1500); });
        }
        if ($('by_product')){
            Event.observe($('by_product'), 'mouseover', function(){ clearTimeout(showing3Timeout); });
            Event.observe($('by_product'), 'mouseout', function(){ showing3Timeout = setTimeout('timeOutShare3Dropdown()', 1500); });
        }
		if ($('share_dropDown_0')||$('share_dropDown_2')){
			allShareDropdowns = $$('div.sharebtn_dropDown').each(
				function(e,index){
					Event.observe(e, 'mouseover', function(e){ e.stop();shareDropdownMouseover(index); });
            		Event.observe(e, 'mouseout', function(e){ e.stop();shareDropdownMouseout(index); });
				}
			)
		}
}
function showTOS(e){
	Event.stop(e);
	if($('terms')){
		$('terms').show();
	}
}
function showTOS2(e){
	Event.stop(e);
	if($('terms')){
		$('terms').show();
	}
}
function shareDropdownMouseover(index){
	if(dropdownShowing == true){
		 clearTimeout(sharebtnTimeout);
		 $('share_btn_'+index).show();
	}else{
		clearTimeout(multiShareTimeout);
	}
}
function shareDropdownMouseout(index){
	sharebtnTimeout = setTimeout('timeOutSharebtnDropdown()', 1500);
	multiShareTimeout = setTimeout(function(){timeOutMultiShareDropdown()}, 1500);
	dropdownShowing = false;
}
function toggleLoginPopup(e){
	Event.stop(e);
	if(loginShowing==false){

		$('login_pop').show();
		loginShowing = true;

	} else if(loginShowing==true){

		$('login_pop').hide();
		loginShowing = false;
	}
}

function showContestResults(e){
	Event.stop(e);
	if(showing==false){

		$('contest_prev').show();
		$('prev_contest_btn').addClassName('sel');
		showing=true;

	} else if(showing==true) {

		$('contest_prev').hide();
		$('prev_contest_btn').removeClassName('sel');
		showing=false;
	}

}


function timeOutShareDropdown(){
	clearTimeout(shareTimeout);
	$('share_dropDown').hide();
	shareShowing = false;

}
function timeOutMultiShareDropdown(){
	clearTimeout(multiShareTimeout);
	$$('div.sharebtn_dropDown').each(function(s){
			s.hide();
	});
	//sharebtnShowing[index] = false;
	if(shareHover == false){
		timeOutSharebtnDropdown();
	}
}

function toggleShare(e){
	Event.stop(e);

	if(shareShowing == false){
		$('share_dropDown').show();
		shareTimeout = setTimeout('timeOutShareDropdown()', 1500);
		shareShowing = true;
	} else if(shareShowing == true){
		timeOutShareDropdown();
	}

}
function timeOutSharebtnDropdown(index){
	clearTimeout(sharebtnTimeout);
	$$('a.sharebtn').each(function(s){
			s.hide();
	});

}
function toggleSharebtn(index){

	if(dropdownShowing==false){
		$('share_dropDown_'+index).show();
		//shareTimeout = setTimeout('timeOutShareDropdown()', 1500);
		dropdownShowing = true;
	} else if(dropdownShowing==true) {
		$('share_dropDown_'+index).hide();
		//timeOutShareDropdown();
		dropdownShowing = false;
	}

}
function shareStay(index){
	$('share_btn_'+index).show();
}
function shareHide(index){
	if(dropdownShowing==false){
		sharebtnTimeout = setTimeout(function(){timeOutSharebtnDropdown(index)}, 1500);	
	} else if(dropdownShowing==true){
		//timeOutSharebtnDropdown(index);
		clearTimeout(timeOutSharebtnDropdown);
		clearTimeout(sharebtnTimeout);
		$('share_btn_'+index).show();
	}
}
function shareddHide(index){
	if(shareHover == true){
		multiShareTimeout = setTimeout(function(){timeOutMultiShareDropdown()}, 1500);
		shareHover = false;
	}else{
		sharebtnTimeout = setTimeout(function(){timeOutSharebtnDropdown(index)}, 1500);
	}
}
function timeOutShare1Dropdown(){
	clearTimeout(showing1Timeout);
	$('contest_prev').hide();
	if($('prev_contest_btn')){$('prev_contest_btn').removeClassName('sel');}
	if($('other_categories_btn')){$('other_categories_btn').removeClassName('sel');}
	showing1 = false;
}
function toggleOtherCategories(e){
	Event.stop(e);
	if(showing1==false){
		$('contest_prev').show();
		showing1Timeout = setTimeout('timeOutShare1Dropdown()', 1500);
		showing1=true;
		timeOutShare2Dropdown();
		timeOutShare3Dropdown();

	} else if(showing1==true) {
		timeOutShare1Dropdown();
	}

}

function timeOutShare2Dropdown(){
	clearTimeout(showing2Timeout);
	$('by_location').hide();
	showing2 = false;
}
function toggleByLocation(e){
	Event.stop(e);
	if(showing2==false){
		$('by_location').show();
		showing2Timeout = setTimeout('timeOutShare2Dropdown()', 1500);
		showing2=true;
		timeOutShare1Dropdown();
		timeOutShare3Dropdown();

	} else if(showing2==true) {
		timeOutShare2Dropdown();
	}

}

function timeOutShare3Dropdown(){
	clearTimeout(showing3Timeout);
	$('by_product').hide();
	showing3 = false;
}
function toggleByProduct(e){
	Event.stop(e);
	if(showing3==false){
		$('by_product').show();
		showing3Timeout = setTimeout('timeOutShare3Dropdown()', 1500);
		showing3=true;
		timeOutShare1Dropdown();
		timeOutShare2Dropdown();

	} else if(showing3==true) {
		timeOutShare3Dropdown();
	}

}

function showSharebtn(num){
	$('share_btn_'+num).show();
	//$('share_btn_'+num).fade({ duration: 0.8, from: 0, to: 1 });
}
function hideSharebtn(num){
	if(dropdownShowing == false){
		$('share_btn_'+num).hide();
	}
	//$('share_btn_'+num).fade({ duration: 0.8, from: 1, to: 0 });
}
Event.observe(document, 'dom:loaded', initialize);
//Event.observe(window, 'load', function(){ setTimeout('initialize()', 1000) });