$(document).ready(function() { 

$(".container_12").fadeIn(500);

$("a[rel='gallery']").colorbox({transition:'fade', preloading: true});

$("a.termsofuse").colorbox({iframe:true, innerWidth:600, innerHeight:800});

function submitNewsletterForm(value)
{
		var value = $("input#newsletterEmail").val();
				
		var formelement = $("input#newsletterEmail");
		
		$.ajax({  
			type: "POST",  
			url: locallink + 'modules/newsletter/ajax/subscribe.php',  
			data: 'email='+value,
			success: function(val) 
				{
					$(formelement).parent("fieldset").fadeOut('fast', function() { $(this).parent().hide().append('<div class="feedback" id="newsletter-form-feedback">' + val + '</div>').fadeIn(); });
				}
			
		});
}

function newsletterValidate()
{

	$("form#newsletterRegistration").validate({ 
								rules: { newsletterEmail: { required: true, email: true } },
								messages: { newsletterEmail: "Invalid E-mail" },
								submitHandler: function() { submitNewsletterForm(); }
								
						 });
}

$("form#newsletterRegistration").click( function(event) 
{

	newsletterValidate();

});


$("input#newsletterEmail").live('keydown', function(event) 
{

	newsletterValidate();

});

$("#newsletter-form-feedback a").live('click', function() 
{ 
	$(this).parent().fadeOut('fast', function() 
		{ 
		$("input#newsletterEmail").val();
		$("form#newsletterRegistration fieldset").fadeIn(); 
		});
} );

$("input#newsletterEmail").focus( function() 
	{ 
	$(this).siblings("label.signup").fadeOut(50);	
	} );
	
$("input#newsletterEmail").blur( function() 
	{ 
	if( $(this).val() == "")
		{ 
		$(this).siblings("label.signup").show();
		}	
	} );
	

$(".text, input[type='text'], textarea").focus(function() { $(this).addClass("focus"); }); 

$(".text, input[type='text'], textarea").blur(function() { $(this).removeClass("focus"); }); 

$(".quicklink").append('<div class="overlay"></div>');

$(".overlay").live('click', function() 

	{
	 	var url = $(this).siblings("a").attr("href");
	 	window.location = url;
	}
	
);
	
$(".quicklink").mouseover( function()
{
	$(this).children("h2").addClass('hover');
}
); 	



$(".quicklink").mouseleave( function()
{
	$(this).children("h2").removeClass('hover');
}
); 	

$("#products .product:even").addClass("suffix_1");

$("#products .product:odd").addClass("prefix_1");

$("input#checkoutimage").mouseover( function() 
{
$(this).siblings("label").fadeIn(300);
});

$("input#checkoutimage").mouseleave( function() 
{
$(this).siblings("label").hide()
});

});

function ajaxSubmitForm(form, responseFieldId, extraOptions){
	//prepare options
	if(typeof(extraOptions) == 'undefined') var extraOptions = {};
	if(typeof(extraOptions.type) == 'undefined') extraOptions.type = ($(form).attr('method'))?$(form).attr('method'):'get';
	if(typeof(extraOptions.responseMessageBoxId) == 'undefined') extraOptions.responseMessageBoxId = responseFieldId;
	//hide response box
	$(responseFieldId).removeClass('error').slideUp('fast');
	//form
	ajaxGetContent($(form).attr('action'), $(form).serialize(), extraOptions);
	return false;
}

//renders the inline content from a link
//responseCallback = function to be called on success
//responseMessageBoxId = id (with leading #) of the DOM element which will contain the value
//responseMessageBoxShowDuration = duration of the box in miliseconds that is supposed to be shown
//	status is detected for html by empty message or not (or response.status for json)
//	message will be everything receive in html case (or response.message for json)
function ajaxGetContent(link, extraData, extraOptions){
	if(typeof(extraData) == 'undefined') var extraData = '';
	if(typeof(extraOptions) == 'undefined') var extraOptions = {};
	if(typeof(extraOptions.type) == 'undefined') extraOptions.type = 'get';
	if(typeof(extraOptions.dataType) == 'undefined') extraOptions.dataType = 'json';
	if(typeof(extraOptions.responseMessageBoxShowDuration) == 'undefined') extraOptions.responseMessageBoxShowDuration = 10000;
	
	
	showLoading();
	$.ajax({
	  url: encodeURI(link),
	  type: extraOptions.type,
	  data: 'ajax=1'+(extraData.length>0?('&'+extraData):''),
	  dataType: extraOptions.dataType,
	  success: function(response) {
		  //callback function
		  if(typeof(extraOptions.responseCallback) == 'function'){
			  extraOptions.responseCallback(response);
		  }
		  //redirect
		  else if(extraOptions.dataType == 'json' && typeof(response.redirectUrl) != 'undefined'){
			  document.location.href = response.redirectUrl;
		  }
		  //or show message in the box
		  else if(typeof(extraOptions.responseMessageBoxId) != 'undefined'){
			  var boxId = extraOptions.responseMessageBoxId;
			  var htmlMessage = $.trim(extraOptions.dataType=='html'?response:response.message);
			  var status = extraOptions.dataType=='html'?(htmlMessage==''?'error':'success'):(response.status);
			  $(boxId).html(htmlMessage);
			  if(status == 'error') $(boxId).addClass('error'); 
			  $(boxId).slideDown().html(htmlMessage);
			  $('html,body').animate({scrollTop: $(boxId).offset().top});//scroll to response message			  
			  if(extraOptions.responseMessageBoxShowDuration > 0) setTimeout("if($('"+boxId+"').is(':visible')) $('"+boxId+"').slideUp('fast');", extraOptions.responseMessageBoxShowDuration);
		  }
		  hideLoading();
	  }
	});
}

function showLoading(){
	//$('#vine_loading_overlay,#vine_loading_box').fadeIn();	
	$('#loading_overlay').fadeTo(400, 0.5);	
	$('#loading_box').fadeIn();	
}

function hideLoading(){
	$('#loading_overlay,#loading_box').fadeOut('fast');
}

function number_format (number, decimals, dec_point, thousands_sep) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // Strip all characters but numerical ones.
    number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}
