$(function(){
	
	if ($('#search-drop')) {
		nav_search = { link : $('#search-link'), drop : $('#search-drop'), box : $('#search-box-over'), form : $('#search-form') }
		initSearchBox( nav_search );
	}
	
	if ($('hero-search-drop')) {
		hero_search = { drop : $('hero-search-drop'), box : $('hero-search-box-over'), form : $('hero-search-form') }
		initSearchBox( hero_search );
	}
	
	if ($('search-results')) {
		initPulsate();
	}
	
	if ($('gallery')) {
		initLightboxes();
	}
	
	if ($('tell-a-friend')) {
		initTellFriend();
	}
	
	if ($('body.vouchers').length > 0) {
		initVoucherLightboxes();
		initVoucherForm();
	}
	
	$('.popup').openDOMWindow({ 
		height: '400px', 
		width: 506, 
		overlayOpacity: 50,
		eventType: 'click', 
		loader: 1, 
		loaderImagePath: '/images/common/ajax-loader-1.gif?', 
		loaderHeight: 16, 
		loaderWidth: 16, 
		windowSource: 'ajax', 
		windowHTTPType: 'post' 
	});
	
	if ($('#contact_form').length > 0) {
		$('#contact_form').validate();
	}
	
	if ($('#hero-flash').length > 0) {
		swfobject.embedSWF("flash/hero.swf", "hero-flash", "517", "327", "10.0.0","flash/expressInstall.swf", nm_flashvars, nm_params, nm_attributes);
	}
	
});

/* Search
----------------------------------------------------------------------------------- */
var dropTimer;
var boxTimer;

function initSearchBox( obj ) {

	if (obj.link) {
		$(obj.link).click(function (e) {
			e.preventDefault();
			toggleSearchDrop(obj.form);
		});
		$(obj.link).mouseout(function () { });
	}
	/* inner search gesture */
	$(obj.drop).mouseover(function() { toggleSearchBox( obj.box ); });
	/* drop box */
	$(obj.box).mouseover(function () { clearBoxTimer(); });
	$(obj.box).mouseout(function () { startBoxTimer( obj.box, obj.form ); })
}

function toggleSearchDrop( el ) {
	clearDropTimer();
	$(el).toggle();
}

function toggleSearchBox( el_box, el_form ) {
	var state = ($(el_box).css('display') == 'block') ? 'none' : 'block';
	$(el_box).css('display', state);
	
	if (state == 'none') {
		if ($(el_form).attr('id').indexOf('hero') < 0) {
			$(el_form).hide();
		}
	} else {
		clearDropTimer();
	}
}

function startBoxTimer( el_box, el_form ) {
	clearBoxTimer();
	boxTimer = setTimeout('toggleSearchBox(\'#'+$(el_box).attr('id')+'\',\'#'+$(el_form).attr('id')+'\')', 1500)
}

function clearBoxTimer() 	{ clearTimeout(boxTimer); 	}
function clearDropTimer() { clearTimeout(dropTimer); 	}

/* Pulsate
----------------------------------------------------------------------------------- */
function initPulsate() {
	var arrange_els = $('a.arrange-viewing','#search-results');
	arrange_els.each(function(el){
		$(this).click(function (e) {
			e.preventDefault();
			$.scrollTo( $('#arrange-viewing'), 1000, {} );
			$('#arrange-viewing').delay(1000).effect('pulsate', { times: 3 }, 800);
		});
	});
	
	var buy_els = $('a.buy','#search-results');
	buy_els.each(function(el){
		$(this).click(function (e) {
			e.preventDefault();
			$.scrollTo( $('#buy'), 1000, {} );
			$('#buy').delay(1000).effect('pulsate', { times: 3 }, 800);
		});
	});
}
/* Gallery
----------------------------------------------------------------------------------- */
function initLightboxes() {
	
	$('.lightbox', '#gallery').openDOMWindow({ 
		height: 530, 
		width: 506, 
		overlayOpacity: 50,
		eventType: 'click', 
		loader: 1, 
		loaderImagePath: '/images/common/ajax-loader-1.gif', 
		loaderHeight: 16, 
		loaderWidth: 16, 
		windowSource: 'ajax', 
		windowHTTPType: 'post' 
	});
	
}
/* Tell a friend checkbox
----------------------------------------------------------------------------------- */
function initTellFriend() {
	
	var chk = 			$('#tell-check');
	var chk_wrap = 	$('#tell-check-wrap')
	var submit = 		$('#tell-submit');
	var form = 			$('#contact_form');
	
	$(submit).click( function(e) {
		e.preventDefault();
		
		if ($(chk).attr('checked')) {
			form.submit();
		} else {
			chk_wrap.addClass('error');
		}
	});
	
	$(chk).click( function(e) {
		chk_wrap.removeClass('error')
	});
	
}
/* Vouchers
----------------------------------------------------------------------------------- */
function initVoucherLightboxes() {
	$('.voucher-img-link').openDOMWindow({ 
		height: 530, 
		width: 506, 
		overlayOpacity: 50,
		eventType: 'click', 
		loader: 1, 
		loaderImagePath: '/images/common/ajax-loader-1.gif', 
		loaderHeight: 16, 
		loaderWidth: 16
	});
}

function initVoucherForm() {
	
	$('#send-same-address').attr('checked', true);
	
	$("#redeem-form").validate({
		submitHandler: function(form) {
			
			if ($("#send-same-address").is(':checked')) {
				$('#gift_street1').val($('#street1').val())
				$('#gift_street2').val($('#street2').val())
				$('#gift_suburb').val($('#suburb').val())
				$('#gift_state').val($('#state').val())
				$('#gift_postalcode').val($('#postalcode').val())
			}
		
			form.submit();
		}
	});
	
	$('#send-same-address').change( function () {
		toggle_address_type('same');
	});
	
	$('#send-diff-address').change( function () {
		toggle_address_type('diff');
	});
	
	// reset
	$("#send-same-address").attr("checked", "checked");
	$('#gift_street1, #gift_street2, #gift_suburb, #gift_state, #gift_postalcode').val('')
	
}

function empty_fields (els) {
	
}

function toggle_address_type (type) {
	if (type === 'same') {
		$('#gift-address').hide();
	} else {
		$('#gift-address').show();
	}
}
/* Popup
----------------------------------------------------------------------------------- */
/*
function popup (url) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=476,height=464,left = 560,top = 325');");
}
*/



