/**
 *
 * Copyright (c) 2007 Tom Deater (http://www.tomdeater.com)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */

(function($) {
	/**
	 * equalizes the heights of all elements in a jQuery collection
	 * thanks to John Resig for optimizing this!
	 * usage: $("#col1, #col2, #col3").equalizeCols();
	 */

	$.fn.equalizeCols = function(){
		var height = 0,
			reset = $.browser.msie && $.browser.version < 7 ? "1%" : "auto";

		return this
			.css("height", reset)
			.each(function() {
				height = Math.max(height, this.offsetHeight);
			})
			.css("height", height)
			.each(function() {
				var h = this.offsetHeight;
				if (h > height) {
					$(this).css("height", height - (h - height));
				};
			});

	};

})(jQuery);

/*******************************************************************************

	CSS on Sails Framework
	Title: Gourmet Raw
	Author: XHTMLized (http://www.xhtmlized.com/)
	Date: March 2011

*******************************************************************************/

$(document).ready(function() {

	//
	$(".gallery-navigation").jcarousel({
		scroll: 1
	});

	//
	$(".gallery-content").cycle({
		timeout: 0,
		 pager: '.gallery .gallery-navigation',
		pagerAnchorBuilder: function(idx, slide) {
		return '.gallery .gallery-navigation li:eq(' + idx + ') a';
		}
	});

	//
	$(".home #content .slideshow .slides").cycle({
		prev: '.home #content .slideshow ul li:first-child',
		next: '.home #content .slideshow ul li:last-child',
		timeout: 0
	});

	//
	$('.gallery .gallery-navigation li a').live('hover', function(){
		$(this).click();
	})

	//
	var slides = $('.gallery-navigation li').size();
	if ( slides < 5 ) {
		$('.gallery .jcarousel-prev').hide();
		$('.gallery .jcarousel-next').hide();
	}

	//
	var slides2 = $('.home #content .slideshow .slides .slide').size();
	if ( slides2 < 2 ) {
		$('.home #content .slideshow ul li').hide();
	}

	//
	if (($.browser.mozilla)){
		//
		$('html').addClass('firefox');
	}

	//
	if (($.browser.opera)){
		//
		$('html').addClass('opera');
	}

	//
	if (($.browser.webkit)){
		//
		$('html').addClass('webkit');
	}

	//
	if (($.browser.msie) && ($.browser.version <= "8.0")){

		// CSS3PIE
		var CSS3PIE_selectors = [
			'#sidebar .button-gfx',
			'.products #content .product-details .right ul li',
			'.products #content .product img',
			'#sidebar .signup',
			'#sidebar .signup a',
			'.content-wrapper .top-wrapper .image img',
			'#sidebar ul.list li',
			'.home #content .banner',
			'.home #content .banner img',
			'.home #content .slideshow ul li',
			'.home #content .slideshow .slides .slide .description',
			'.home #content .slideshow .slides .slide .image',
			'.home #content .faq li span',
			'.home #content .products img',
			'#content h3',
			'.home #content .products ul li',
			'#sidebar ul li a',
			'#sidebar .news-wrapper img',
			'#sidebar h3',
			'#sidebar form',
			'.content-wrapper .button',
			'.gallery .jcarousel-next',
			'.gallery .jcarousel-prev',
			'.gallery .gallery-content',
			'.content-wrapper .top-wrapper',
			'#navigation ul li ul',
			'#header .gourmetraw'
		];
		$( CSS3PIE_selectors.join(',') ).each(function() {
			PIE.attach(this);
		});
	}

	//
	if (($.browser.msie) && ($.browser.version == "8.0")){
	}

	//
	if (($.browser.msie) && ($.browser.version == "7.0")){
	}

	//
	if (($.browser.msie) && ($.browser.version == "6.0")){

	}
	
	//
	$('#newslettersignup-fname').val("First name");
	$('#newslettersignup-fname').focus(function(){
		var value = $(this).val();
		if (value == "First name") {
			$(this).val("");
		}
	});
	$('#newslettersignup-fname').blur(function(){
		var value = $(this).val();
		if (value == "") {
			$(this).val("First name");
		}
	});
	
	//
	$('#newslettersignup-lname').val("Last name");
	$('#newslettersignup-lname').focus(function(){
		var value = $(this).val();
		if (value == "Last name") {
			$(this).val("");
		}
	});
	$('#newslettersignup-lname').blur(function(){
		var value = $(this).val();
		if (value == "") {
			$(this).val("Last name");
		}
	});
	
	//
	$('#newslettersignup-email').val("Email");
	$('#newslettersignup-email').focus(function(){
		var value = $(this).val();
		if (value == "Email") {
			$(this).val("");
		}
	});
	$('#newslettersignup-email').blur(function(){
		var value = $(this).val();
		if (value == "") {
			$(this).val("Email");
		}
	});
	
	$('#newslettersignup').submit(function(){
		// build POST string
		var name = $('#newslettersignup-fname').val();
		var surname = $('#newslettersignup-lname').val();
		var email = $('#newslettersignup-email').val();
		$.post('http://www.gourmetraw.com/themes/site_themes/gourmet_raw/scripts/mc_subscribe.php',{'name':name, 'surname':surname, 'email':email}, function(resp){
			if(resp == 'OK') {
				$('#newslettersignup-fname').hide();
				$('#newslettersignup-lname').hide();
				$('#newslettersignup-email').hide();
				$('#newslettersignup .button').hide();
				$('#newslettersignup-response').html("Thank you, we've just sent you a confirmation e-mail. enjoy");
			} else {
				alert(resp);
			}
		});
		
		// stop default action
		return false;
	});
	
	// create the popup window for the terms and conditions
	$('.popup_window_terms').popupWindow({
		centerBrowser:1,
		width: 800,
		scrollbars: 1
	});
});
