$j = jQuery.noConflict();
$j(document).ready(function() {
	
	Cufon.replace('.header h2', {textShadow: '0px 1px #d7779d'});
	Cufon.replace('.navigation div > ul > li > a', {textShadow: '0px 1.5px #ffffff'});
	Cufon.replace('.navigation div > ul > li   ul > li > a', {textShadow: '0px 1px #000000'});
	Cufon.replace('.main-contents h1', {textShadow: '0px 1px #ffffff'});
	Cufon.replace('.no-results h2.entry-title', {textShadow: '0px 1px #ffffff'});
	Cufon.replace('#content-clients-single .entry-content .client-left h2', {textShadow: '0px 1px #ffffff'});
	Cufon.replace('p.coming-soon', {textShadow: '-1px -1px #c8688e'});
	
	
	Cufon.now();
	
	$j('.navigation div > ul > li').each(function() {
		$j(this).find('> ul').css('display', 'block').hide();
		$j(this).hoverIntent({
			interval: 200,
			over: showNav,
			timeout: 400,
			out: hideNav
		});
		$j(this).mouseenter(function() {
			Cufon.replace($j(this).find('> a'), {color:'#b92f77', textShadow: '0px 1.5px #ffffff'});
		});
		$j(this).mouseleave(function() {
			if (!$j(this).hasClass("current-menu-item") && !$j(this).hasClass("current-menu-ancestor")) {
				Cufon.replace($j(this).find('> a'), {color:'#63055b', textShadow: '0px 1.5px #ffffff'});
			}
		});
	});
	
	function showNav() {
		$j(this).find('> ul').show();
	}
	function hideNav() {
		$j(this).find('> ul').hide();
	}
	
	$j('.footer-contents .logos-slideshow .images').cycle({
		fx: 'fade',
		pause:  1,
		speed:  500,
		timeout: 2000
	});
	
	$j('.main-contents').css('min-height', $j(window).height() - $j('.header').height() - $j('.footer').height() - $j("#wpadminbar").height());
	$j('.footer div > ul > li:not(.footer div > ul > li:last-child)').after('<span class="separator"> &bull; </span>');
	
	// HOMEPAGE
	
	$j("#home-container .home-slideshow .images").cycle({
		fx: 'fade',
		speed:  1000,
		timeout: 2000
	});
	
	$j(".main-contents #home-container .home-feeds #home-events .home-entry .more-link").before("<br />");
	
	// CONTACT US
	
	function refreshQuoteCakes() {
		if ($j("p.contactus-subject select").val() == "Request for Quotation") {
			$j("p.quote-reference").show();
			$j("#quoteCakes a").each(function() {
				var newCake = $j(this).attr("title");
				var newCakeURL = $j(this).attr("rel");
				$j("p.quote-reference textarea").val($j("p.quote-reference textarea").val() + newCake + " [" + newCakeURL + "]\n");
				$j(this).mouseenter(function(e) {
					$j(this).find("img").stop(true, false).animate({ opacity: 0.5 }, 200);
				});
				$j(this).mouseleave(function(e) {
					$j(this).find("img").stop(true, false).animate({ opacity: 1 }, 200);
				});
				$j(this).click(function(e) {
					e.preventDefault();
					var newNum = $j(this).attr("id");
					var quoteCakes = new Array();
					if ($j.cookie('quoteCakes') && $j.cookie('quoteCakes').length > 0) {
						quoteCakes = $j.cookie('quoteCakes').split(",");
						quoteCakes.splice(newNum,1);
						$j.cookie('quoteCakes',quoteCakes,{expires: 7, path: '/', domain: 'heartsnbells.com'});
					}
					$j(this).remove();
					refreshQuoteClick();
				});
			});
			if ($j("#quoteCakes a").size() == 0) {
				$j("p.quote-reference textarea").show();
			}
		} else {
			$j("p.quote-reference").hide();
			$j("p.quote-reference textarea").val("");
		}
	}
	function refreshQuoteClick() {
		var newID = 0;
		$j("p.quote-reference textarea").val("");
		$j("#quoteCakes").find("a").each(function() {
			$j(this).attr("id", newID);
			var newCake2 = $j(this).attr("title");
			var newCakeURL2 = $j(this).attr("rel");
			$j("p.quote-reference textarea").val($j("p.quote-reference textarea").val() + newCake2 + " [" + newCakeURL2 + "]\n");
			newID++;
		});
		if ($j("#quoteCakes a").size() == 0) {
			$j("p.quote-reference textarea").show();
		}
	}
	
	$j("#contact-form-eventdate").datepicker({ minDate: 1, dateFormat:'DD, d MM, yy' });
	$j("p.quote-reference .form-right").append($j(".contact-details #quoteCakes"));
	$j("p.contactus-subject select").change(function() {
		refreshQuoteCakes();
	});
	refreshQuoteCakes();
	
	// PAST EVENTS
	
	$j("#content-events-past .past-entry-thumbnail").each(function() {
		var pastThumbHeight =  ($j(this).height() - ($j(this).find("img").height()+6)) / 2;
		$j(this).find("img").css("margin-top", pastThumbHeight);
	});
	
	// PRESS
	
	$j("#content-press .pressitem-thumbnail").each(function() {
		var pastThumbHeight =  ($j(this).height() - ($j(this).find("img").height()+6)) / 2;
		$j(this).find("img").css("margin-top", pastThumbHeight);
	});
	
	$j("#content-press-single .press-pages .images").cycle({
		fx: 'scrollHorz',
		speed:  500,
		timeout: 0,
		startingSlide: 1,
		pager:  '#content-press-single .press-pages .navigation #pagexof',
		pagerAnchorBuilder: function(idx, slide) { 
			return '<span>' + (idx+1) + '</span>'; 
		},
		prev:   '#content-press-single .press-pages .navigation #prev',
		next:   '#content-press-single .press-pages .navigation #next'
	});
	
	$j("#content-press-single .press-pages .navigation #pageofx").text($j("#content-press-single .press-pages .images img").size());
	$j("#content-press-single .press-pages .navigation #prev, #content-press-single .press-pages .navigation #next").each(function() {
		$j(this).animate({ opacity:0 }, 0);
		$j(this).bind("mouseenter", function() {
			$j(this).stop(true, false).animate({ opacity: 1 }, 300);
		});
		$j(this).bind("mouseleave", function() {
			$j(this).stop(true, false).animate({ opacity: 0 }, 700);
		});
	});
	
	// GALLERY
	
	$j(".small-cake").find("a").each(function() {
		$j(this).click(function(e) {
			e.preventDefault();
			var cakeid = $j(this).attr("id");
			var caketitle = $j(this).attr("title");
			var cakeimage = $j(this).attr("rel");
			$j(".large-cake .images img").attr("src", cakeimage);
			$j(".large-cake .mask .cake-code").text(caketitle);
			$j(".large-cake .mask .gocookie a").attr("rel", cakeid);
		});
	});
	$j(".small-cake").find("a:first-child").click();
	
	$j(".large-cake .mask .gocookie a.req-quote").click(function(e) {
		e.preventDefault();
		if ($j(this).attr("rel") != "") {
			var cakeid = $j(this).attr("rel");
			var quoteCakes = new Array();
			if ($j.cookie('quoteCakes') && $j.cookie('quoteCakes').length > 0) {
				quoteCakes = $j.cookie('quoteCakes').split(",");
			}
			var isNotYetAdded = true;
			for (var i=0; i<quoteCakes.length; i++) {
				if (quoteCakes[i] == cakeid) {
					isNotYetAdded = false;
				}
			}
			if (isNotYetAdded == true) {
				quoteCakes.push(cakeid);
				//var quoteCakes = new Array();
				$j.cookie('quoteCakes',quoteCakes,{expires: 7, path: '/', domain: 'heartsnbells.com'});
				var cookieConfirm = confirm("" + $j(".cake-code").text() + " has been added to your list.\nWould you like to proceed to the contact form?");
				if (cookieConfirm == true) {
					window.location = "http://www.heartsnbells.com/contact/?subject=quote";
				}
			} else {
				alert("This item has already been added.");
			}
			//$j(".edit-link").append(" <p>" + quoteCakes + "</p> ");
		}
	});
	$j(".large-cake .mask .gocookie a.add-order").click(function(e) {
		e.preventDefault();
		if ($j(this).attr("rel") != "") {
			var cakeid = $j(this).attr("rel");
			var quoteCakes = new Array();
			if ($j.cookie('orderCakes') && $j.cookie('orderCakes').length > 0) {
				quoteCakes = $j.cookie('orderCakes').split(",");
			}
			var isNotYetAdded = true;
			for (var i=0; i<quoteCakes.length; i++) {
				if (quoteCakes[i] == cakeid) {
					isNotYetAdded = false;
				}
			}
			if (isNotYetAdded == true) {
				quoteCakes.push(cakeid);
				//var quoteCakes = new Array();
				$j.cookie('orderCakes',quoteCakes,{expires: 7, path: '/', domain: 'heartsnbells.com'});
				var cookieConfirm = confirm("" + $j(".cake-code").text() + " has been attached to your order form.\nWould you like to proceed to the order form?");
				if (cookieConfirm == true) {
					window.location = "http://www.heartsnbells.com/contact/order-form";
				}
			} else {
				alert("This item has already been attached to your order form.");
			}
			//$j(".edit-link").append(" <p>" + quoteCakes + "</p> ");
		}
	});
	$j(".large-cake .mask .gocookie a.add-order-Minicakes").click(function(e) {
		e.preventDefault();
		if ($j(this).attr("rel") != "") {
			var cakeid = $j(this).attr("rel");
			var quoteCakes = new Array();
			if ($j.cookie('orderMinicakes') && $j.cookie('orderMinicakes').length > 0) {
				quoteCakes = $j.cookie('orderMinicakes').split(",");
			}
			var isNotYetAdded = true;
			for (var i=0; i<quoteCakes.length; i++) {
				if (quoteCakes[i] == cakeid) {
					isNotYetAdded = false;
				}
			}
			if (isNotYetAdded == true) {
				quoteCakes.push(cakeid);
				//var quoteCakes = new Array();
				$j.cookie('orderMinicakes',quoteCakes,{expires: 7, path: '/', domain: 'heartsnbells.com'});
				var cookieConfirm = confirm("" + $j(".cake-code").text() + " has been attached to your order form.\nWould you like to proceed to the order form?");
				if (cookieConfirm == true) {
					window.location = "http://www.heartsnbells.com/contact/order-form";
				}
			} else {
				alert("This item has already been attached to your order form.");
			}
			//$j(".edit-link").append(" <p>" + quoteCakes + "</p> ");
		}
	});
	$j(".large-cake .mask .gocookie a.add-order-Cupcakes").click(function(e) {
		e.preventDefault();
		if ($j(this).attr("rel") != "") {
			var cakeid = $j(this).attr("rel");
			var quoteCakes = new Array();
			if ($j.cookie('orderCupcakes') && $j.cookie('orderCupcakes').length > 0) {
				quoteCakes = $j.cookie('orderCupcakes').split(",");
			}
			var isNotYetAdded = true;
			for (var i=0; i<quoteCakes.length; i++) {
				if (quoteCakes[i] == cakeid) {
					isNotYetAdded = false;
				}
			}
			if (isNotYetAdded == true) {
				quoteCakes.push(cakeid);
				//var quoteCakes = new Array();
				$j.cookie('orderCupcakes',quoteCakes,{expires: 7, path: '/', domain: 'heartsnbells.com'});
				var cookieConfirm = confirm("" + $j(".cake-code").text() + " has been attached to your order form.\nWould you like to proceed to the order form?");
				if (cookieConfirm == true) {
					window.location = "http://www.heartsnbells.com/contact/order-form";
				}
			} else {
				alert("This item has already been attached to your order form.");
			}
			//$j(".edit-link").append(" <p>" + quoteCakes + "</p> ");
		}
	});
	
	$j('.pseudosearch .cakecode').each(function() {
		var default_value = this.value;
		$j(this).focus(function() {
			$j(this).addClass("focusTextbox");
			if(this.value == default_value) {
				this.value = '';
			}
		});
		$j(this).blur(function() {
			if(this.value == '') {
				this.value = default_value;
				$j(this).removeClass("focusTextbox");
			}
		});
	});
	$j(".pseudosearch .cakecodesubmit").click(function(e) {
		e.preventDefault();
		if ($j(".pseudosearch .cakecode").val() != "Search by Cake Code" && $j(".pseudosearch .cakecode").val() != "") {
			window.location = "http://www.heartsnbells.com/cakes/"+$j(".pseudosearch .cakecode").val();
		}
	});
	$j("form#pseudosearch").submit(function() {
		if ($j(".pseudosearch .cakecode").val() != "Search by Cake Code" && $j(".pseudosearch .cakecode").val() != "") {
			window.location = "http://www.heartsnbells.com/cakes/"+$j(".pseudosearch .cakecode").val();
		}
		return false;
	});
	
	// ORDER FORM 
	
	$j(".order-form .order-cake-date").datepicker({ minDate: 14, dateFormat:'DD, d MM, yy' });
	$j(".numberonly").live('blur',function() {
	  $j(this).val(stripAlphaChars(jQuery(this).val()));
	});
	function getTotalMinicakes() {
		var totalminicakes = 0;
		$j(".minicake-add").each(function() {
			if($j(this).val() != "" && !isNaN($j(this).val())) {
				totalminicakes = totalminicakes + parseInt($j(this).val());
			}
		});
		return totalminicakes;
	}
	$j(".minicake-add").blur(function() {
		$j(".minicake-total").val(getTotalMinicakes());
	});
	$j(".minicake-total").blur(function() {
		$j(".minicake-total").val(getTotalMinicakes());
	});
	function stripAlphaChars(pstrSource) {
    	var m_strOut = new String(pstrSource); 
    	m_strOut = m_strOut.replace(/[^0-9]/g, ''); 
    	return m_strOut;
	}
	$j("#for-candle-stick, #for-candle-number").hide();
	$j("input:radio[name=order-candles-type]").click(function() {
		if ($j("input:radio[name=order-candles-type]:checked").val() == "Stick-type") {
			$j("#for-candle-stick").show();
			$j("#for-candle-number").hide();
		} else if ($j("input:radio[name=order-candles-type]:checked").val() == "Number type") {
			$j("#for-candle-stick").hide();
			$j("#for-candle-number").show();
		} else {
			$j("#for-candle-stick").hide();
			$j("#for-candle-number").hide();
			$j("#for-candle-stick input[type=text]").val("");
			$j("#for-candle-number input[type=text]").val("");
		}
	});
	
	function refreshOrderCakes() {
		$j("#orderCakes a").each(function() {
			var newCake = $j(this).attr("title");
			var newCakeURL = $j(this).attr("rel");
			$j("li.orderCakesForm textarea").val($j("li.orderCakesForm textarea").val() + newCake + " [" + newCakeURL + "]\n");
			$j(this).mouseenter(function(e) {
				$j(this).find("img").stop(true, false).animate({ opacity: 0.5 }, 200);
			});
			$j(this).mouseleave(function(e) {
				$j(this).find("img").stop(true, false).animate({ opacity: 1 }, 200);
			});
			$j(this).click(function(e) {
				e.preventDefault();
				var newNum = $j(this).attr("id");
				var quoteCakes = new Array();
				if ($j.cookie('orderCakes') && $j.cookie('orderCakes').length > 0) {
					quoteCakes = $j.cookie('orderCakes').split(",");
					quoteCakes.splice(newNum,1);
					$j.cookie('orderCakes',quoteCakes,{expires: 7, path: '/', domain: 'heartsnbells.com'});
				}
				$j(this).remove();
				refreshOrderClick();
			});
		});
		if ($j("#orderCakes a").size() == 0) {
			$j("li.orderCakesForm input[type=file]").show();
		}
	}
	function refreshOrderMinicakes() {
		$j("#orderMinicakes a").each(function() {
			var newCake = $j(this).attr("title");
			var newCakeURL = $j(this).attr("rel");
			$j("li.orderMinicakesForm textarea").val($j("li.orderMinicakesForm textarea").val() + newCake + " [" + newCakeURL + "]\n");
			$j(this).mouseenter(function(e) {
				$j(this).find("img").stop(true, false).animate({ opacity: 0.5 }, 200);
			});
			$j(this).mouseleave(function(e) {
				$j(this).find("img").stop(true, false).animate({ opacity: 1 }, 200);
			});
			$j(this).click(function(e) {
				e.preventDefault();
				var newNum = $j(this).attr("id");
				var quoteCakes = new Array();
				if ($j.cookie('orderMinicakes') && $j.cookie('orderMinicakes').length > 0) {
					quoteCakes = $j.cookie('orderMinicakes').split(",");
					quoteCakes.splice(newNum,1);
					$j.cookie('orderMinicakes',quoteCakes,{expires: 7, path: '/', domain: 'heartsnbells.com'});
				}
				$j(this).remove();
				refreshOrderMiniclick();
			});
		});
		if ($j("#orderMinicakes a").size() == 0) {
			$j("li.orderMinicakesForm input[type=file]").show();
		}
	}
	function refreshOrderCupcakes() {
		$j("#orderCupcakes a").each(function() {
			var newCake = $j(this).attr("title");
			var newCakeURL = $j(this).attr("rel");
			$j("li.orderCupcakesForm textarea").val($j("li.orderCupcakesForm textarea").val() + newCake + " [" + newCakeURL + "]\n");
			$j(this).mouseenter(function(e) {
				$j(this).find("img").stop(true, false).animate({ opacity: 0.5 }, 200);
			});
			$j(this).mouseleave(function(e) {
				$j(this).find("img").stop(true, false).animate({ opacity: 1 }, 200);
			});
			$j(this).click(function(e) {
				e.preventDefault();
				var newNum = $j(this).attr("id");
				var quoteCakes = new Array();
				if ($j.cookie('orderCupcakes') && $j.cookie('orderCupcakes').length > 0) {
					quoteCakes = $j.cookie('orderCupcakes').split(",");
					quoteCakes.splice(newNum,1);
					$j.cookie('orderCupcakes',quoteCakes,{expires: 7, path: '/', domain: 'heartsnbells.com'});
				}
				$j(this).remove();
				refreshOrderCupclick();
			});
		});
		if ($j("#orderCupcakes a").size() == 0) {
			$j("li.orderCupcakesForm input[type=file]").show();
		}
	}
	
	function refreshOrderClick() {
		var newID = 0;
		$j("li.orderCakesForm textarea").val("");
		$j("#orderCakes").find("a").each(function() {
			$j(this).attr("id", newID);
			var newCake2 = $j(this).attr("title");
			var newCakeURL2 = $j(this).attr("rel");
			$j("li.orderCakesForm textarea").val($j("li.orderCakesForm textarea").val() + newCake2 + " [" + newCakeURL2 + "]\n");
			newID++;
		});
		if ($j("#orderCakes a").size() == 0) {
			$j("li.orderCakesForm input[type=file]").show();
		}
	}
	function refreshOrderMiniclick() {
		var newID = 0;
		$j("li.orderMinicakesForm textarea").val("");
		$j("#orderMinicakes").find("a").each(function() {
			$j(this).attr("id", newID);
			var newCake2 = $j(this).attr("title");
			var newCakeURL2 = $j(this).attr("rel");
			$j("li.orderMinicakesForm textarea").val($j("li.orderMinicakesForm textarea").val() + newCake2 + " [" + newCakeURL2 + "]\n");
			newID++;
		});
		if ($j("#orderMinicakes a").size() == 0) {
			$j("li.orderMinicakesForm input[type=file]").show();
		}
	}
	function refreshOrderCupclick() {
		var newID = 0;
		$j("li.orderCupcakesForm textarea").val("");
		$j("#orderCupcakes").find("a").each(function() {
			$j(this).attr("id", newID);
			var newCake2 = $j(this).attr("title");
			var newCakeURL2 = $j(this).attr("rel");
			$j("li.orderCupcakesForm textarea").val($j("li.orderCupcakesForm textarea").val() + newCake2 + " [" + newCakeURL2 + "]\n");
			newID++;
		});
		if ($j("#orderCupcakes a").size() == 0) {
			$j("li.orderCupcakesForm input[type=file]").show();
		}
	}
	
	$j("li.orderCakesForm > span:last-child").append($j("#orderCakes"));
	$j("li.orderMinicakesForm > span:last-child").append($j("#orderMinicakes"));
	$j("li.orderCupcakesForm > span:last-child").append($j("#orderCupcakes"));
	refreshOrderCakes();
	refreshOrderMinicakes();
	refreshOrderCupcakes();
	
	// CLIENTS
	
	$j("#content-clients-single .client-left").append($j("#content-clients-single p.audioplayer_container"));
	
});

$j(window).resize(function() {
	$j('.main-contents').css('min-height', $j(window).height() - $j('.header').height() - $j('.footer').height() - $j("#wpadminbar").height());
});

