$(document).ready(function(){

	/* Generic image fader */

	if($(".fade").length) {
		$(".fade").innerfade({
			speed: "normal",
			timeout: "6000",
			type: 'random' // replace with 'sequence' for consecutive order
		});
	}

	/* PNG fix for IE6 */
	$('img[src$=.png]').ifixpng();


	/* Quotations */

	if($("#quotations ul").length) {
		$("#quotations ul").innerfade({
			speed: "normal",
			timeout: "8000"
		});
	}

	/* Multiple pages */

	if($(".sheet4").length) {
		$(".sheet4 .tabs li").click(function(){
			var num = $(this).attr("class");
			$(".sheet4 .pageon").removeClass("pageon");
			$(".sheet4 .page"+num).addClass("pageon");
		});
	}

	/* Ingredients list */

	if($(".listB").length) {
		$(".listB li").hover(function(){
			var sheet = $(this).parents(".sheet5");
			$(".msgbar",sheet).addClass("msgbar-over");
			var imsrc = $("img", this).attr("src");
			$("img", this).css("visibility", "hidden");
			$(this).css("background-image", "url("+imsrc.slice(0,-4)+"_on."+imsrc.slice(-3)+")");
			$(".msgbar .inner").prepend("<h2><img src='"+imsrc+"' alt='' /></h2>");
			$(".msgbar .inner").append("<p>"+$("span", this).text()+"</p>");
		},function(){
			$("img", this).css("visibility", "visible");
			$(this).css("background-image", "none");
			$(".msgbar .inner *").remove();
			$(".msgbar").removeClass("msgbar-over");
		});
	}

	/* fix join form background */
	if ( $('#suplimentar').length ) {
		$('#suplimentar').height( $('.bottombg').height()-181 );
	}

	/* fix cart background */
	if ( $('#fixbg').length ) {
			$('#fixbg').height( $('.bottombgcart').height()-120 );
	}

	/* fix popup form background */
	if ( $('#popupsuplimentar').length ) {
		$('#popupsuplimentar').height( $('.bottombg').height()-70 );
	}

	/* skinable select boxes */
	if ( $("select.skinable").length ) {
		$("select.skinable").selectbox({animationSpeed: 200});
	}

	/* add new pets in join page */
	$('a.addNewPet').click(function(){
		var petscounter = parseFloat($('#petscounter').val()); // read current pets counter
		var newPet = '<div class="additionalpet_'+petscounter+'"><fieldset><label class="clean">Pet</label><input type="radio" class="radio" name="data[pet]['+petscounter+']" id="dog_'+petscounter+'" value="dog" /><label for="dog_'+petscounter+'" class="inline">Dog</label><input type="radio" class="radio" name="data[pet]['+petscounter+']" id="cat_'+petscounter+'" value="cat" /><label for="cat_'+petscounter+'" class="inline">Cat</label><br /> <label for="petname_'+petscounter+'">Pet\'s name</label><input type="text" class="text" name="data[petname]['+petscounter+']" id="petname_'+petscounter+'" value="" /><br /> <label>Pet\'s birth day</label> <select name="data[birth_day]['+petscounter+']" id="birth-day_'+petscounter+'" class="skinable day"> <option value="">DD</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </select> <select name="data[birth_month]['+petscounter+']" id="birth-month_'+petscounter+'" class="skinable month"> <option value="">MM</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> </select> <select name="data[birth_year]['+petscounter+']" id="birth-year_'+petscounter+'" class="skinable year"> <option value="">YYYY</option> ';
		var today = new Date();
		for(var i=1979;i<=today.getFullYear();i++)
			newPet += '<option value="'+i+'">'+i+'</option> ';
		newPet += '</select><br /> <p class="addmore"><a href="#" class="deleteObj" onclick="deletePet(\'additionalpet_'+petscounter+'\');return false;">Delete</a></p></fieldset> <div class="hr"><hr /></div></div>';

		$('#morepets').append(newPet);
//		$('.additionalpet_'+parseFloat(petscounter-1)).prepend(newPet);
		___redraw();

		$('#petscounter').val(petscounter+1); // set new value for pets

		return false;
	});

	/* add new address in join page */
	$('a.addNewAddress').click(function(){
		var addressescounter = parseInt($('#addressescounter').val()); // read current addresses counter
		var newAddress = '<div class="newAddress_'+addressescounter+'"><fieldset><label for="delivery-address">Delivery address*</label><textarea name="data[address]['+addressescounter+']" id="delivery-address" cols="30" rows="5"></textarea><br /><br /> <label for="postcode">Postcode*</label><input type="text" class="text" name="data[postcode]['+addressescounter+']" id="postcode" value="" /><br /> <label for="telephone">Telephone</label><input type="text" class="text" name="data[phone]['+addressescounter+']" id="telephone" value="" /><br /> <label for="notathome">If not at home</label> <select name="data[not_at_home]['+addressescounter+']" class="skinable notathome">'+$('#notathome').html()+'</select><br /> <p class="addmore"><a href="#" class="deleteObj" onclick="deleteAddress(\'newAddress_'+addressescounter+'\'); return false;">Delete</a></p></fieldset> <div class="hr"><hr /></div></div>'

		$('#moreaddresses').append(newAddress);
		___redraw();

		$('#addressescounter').val(addressescounter+1); // set new value for pets

		return false;
	});

	/* launch popups */
	$('a.popupWindowPlease').click(function(){
		try {
			popup('index.php?fuseaction=user.edit','updateaccount',428,600);
		} catch(err) {
			var text = 'There was an error while trying to open a page.\n\n';
			text += 'Please disable any popup blockers!\n\n';
			alert(text);
		}

		return false;
	});

	// new accounts
	if ( $('#check-new-account').length ) {
		$('#check-new-account, #passwords').hide();
		___redraw();
	}
	$('#newaccount').click(function(){
		$('#check-new-account, #passwords, #remember_details').toggle();
		___redraw();
	});
	if($('#newaccount').attr("checked")) {
		$('#check-new-account, #passwords, #remember_details').toggle();
		___redraw();
	}

	// new address in checkout 4
	if ( $('#new-address').length ) {
		$('#new-address').hide();
		___redraw();
	}

	$("#same-delivery").change(function(){
		if($(this).attr("checked")) {
			$("#billing-address").val($("#delivery-address1").val());
			$("#billing-postcode").val($("#postcode").val());
			$("#billing-name").val($("#first_name").val()+" "+$("#surname").val());
		}
	});

	$('#delivery-address').change(function(){
		if ( $(this).val() == 'new' ) {
			$('#new-address').toggle();
		} else {
			$('#new-address').hide();
		}
		___redraw();
	});
	$('#delivery-address').change();

	// new payment in checkout 4
	if ( $('#new-payment').length ) {
		$('#new-payment, #cvv-on-run').hide();
		___redraw();
	}
	$('#payment-method').change(function(){
		if ( $(this).val() == 'new' ) {
			$('#new-payment').toggle();
			$('#cvv-on-run').hide();
		} else if ( $(this).val() ) {
			$('#cvv-on-run').hide();
			$('#new-payment').hide();
		} else {
			$('#cvv-on-run,#new-payment').hide();
		}
		___redraw();
	});

	$('#payment-method').change();

	// clear pet from the form
	$('a.clearPet').click(function(){
		$('#dog, #cat').attr('checked','');
		$('#petname').val('');
		$('#birth-day').prev().text('DD');
		$('#birth-month').prev().text('MM');
		$('#birth-year').prev().text('YYYY');

		return false;
	});

	// clear address from the form
	$('a.clearAddress').click(function(){
		$('#delivery-address, #postcode, #telephone').val('');
		$('#notathome').prev().text('Select');

		return false;
	});

	// faq popup
	$('a#faq').click(function(){
		try {
			popup($(this).attr("href"),'updateaccount',418,600);
		} catch(err) {
			var text = 'There was an error while trying to open a page.\n\n';
			text += 'Please disable any popup blockers!\n\n';
			alert(text);
		}

		return false;
	});

	// preload images on press page
	if ( $('a.fullstory').length ){
		$('body').append('<div class="hide preloader"></div>');

		$('a.fullstory').each(function(){
			var img = $(this).attr('href');
			var actImg = img.split('=');
			//$(this).attr('rel',actImg[1]);

			$('.preloader').append('<img src="'+actImg[1]+'" />');
		});

		//$.preload('.preloader img');
	}

	// full press story
	$('a.fullstory').click(function(){
		popup($(this).attr('href'),'fullpress',100,100);

		return false;
	});

	$('.deleteAddr').click(function(){
		var parent = $(this).parents('fieldset');
		parent.next('div.hr').remove();
		parent.remove();
		return false;
	});
	
	// recipe
	$('li#navi_recipe a').click(function(){
		popup('/recipe.php','recipe',418,1010);

		return false;
	});
});


/* Popup */

function popup(mypage,myname,w,h){
	winl = (screen.width-w)/2;
	wint = (screen.height-h)/2;
	settings='height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=yes,toolbar=no,resizable=1'
	window.open(mypage,myname,settings);
}

function ___redraw(){
	$(".jquery-selectbox").unselectbox();
	$("select.skinable").selectbox({animationSpeed: 200});
	$('#suplimentar').height( $('.bottombg').height()-181 );
}

function deletePet(pet){
	var petscounter = parseFloat($('#petscounter').val()); // read current pets counter
	$('div.'+pet).remove();
	$('#petscounter').val(petscounter-1); // set new value for pets

	___redraw();

	return false;
}

function deleteAddress(address){
	var addressescounter = parseFloat($('#addressescounter').val()); // read current addresses counter
	$('div.'+address).remove();
	$('#addressescounter').val(addressescounter-1); // set new value for addresses

	___redraw();

	return false;
}

jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};