include('validation.js')

// apply to all png images 
jQuery(document).ready(function(){
	
	jQuery = $
	
	jQuery('.hide').hide()

	jQuery('a').ifixpng()
	jQuery('img').ifixpng()
	jQuery('div').ifixpng()
	
	/* hide any open_flyout when user clicks somewhere */
	$('*').not('.share_button').click(function() {
		if(execOnNextClick != null) {
			execOnNextClick()
			execOnNextClick = null
		}
	})
	
	execOnNextClick = null
		
	// fbr flyout
	$('#fueledbyramen').click(function() {
		$('#fueledbyramen_flyout').show()
		execOnNextClick = function() { $('#fueledbyramen_flyout').hide() }
		return false
	})
	
	// share flyouts
	$('.share_button').click(function() {
		mine = false;
		if(execOnNextClick != null) {
			mine = obj[0] == $(this.parentNode).find('ul')[0]
			execOnNextClick()
			execOnNextClick = null
			
		}
		
		if(!mine) {
			obj = $(this.parentNode).find('ul')
			obj.css('left', this.offsetLeft)
			obj.show()
			execOnNextClick = function() { obj.hide() } 
			return false
		}
	})
	
	//hide mailinglist popups when mouse moves out
	$('#header_mailinglist_popup, #footer_mailinglist_popup').click(
		function() {}, function(e) {	    
	    if(e.relatedTarget) {
    		$(this).hide()
	    }
	})

	$('#popup_close').click(function() {
		$('#header_mailinglist_popup').hide()
		
	})

	$('#mailinglist_link').click(function() {
		if(jQuery('#header_mailinglist_popup').css("display") == "block")
          $('#header_mailinglist_popup').hide()
		else
		 $('#header_mailinglist_popup').show()
	})
	
	// Mailinglist
	$('.mailinglist .mailinglist_join').click(function() {
	    if(validate($(this.parentNode), 'mailinglist')) {
	    		document.mailSignupForm.submit();
			$('.mailinglist .form').hide();$('.mailinglist .thank_you').show()
	    }
	})
});

function include(url) {
    $('head').append('<script type="text/javascript" src="/components/scripts/' + url + '"/>')
}

function resizeImages(whichId, maxH) {
  if($(whichId).length) {
	  var pix=$(whichId).find('img');
	  
	  for (i=0; i<pix.length; i++) {
		w=pix[i].width;
		h=pix[i].height;
		 
		if (h > maxH) {
		  f=1-((h - maxH) / h);
		  pix[i].width=w * f;
		  pix[i].height=h * f;
		  $(pix[i]).parent().css({ "background":"#000" });
		}
		else {
		  $(pix[i]).css({ "padding-top": (maxH - h) / 2 + 'px'});	 
		}
	  }
  }
}

function fbs_click(url) 
{
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(url)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}