$(document).ready(function(){
	
	if(typeof($().piroBox) != "undefined"){
		$().piroBox({
				my_speed: 400, //animation speed
				bg_alpha: 0.1, //background opacity
				slideShow : true, // true == slideshow on, false == slideshow off
				slideSpeed : 4, //slideshow duration in seconds(3 to 6 Recommended)
				close_all : '.piro_close,.piro_overlay'// add class .piro_overlay(with comma)if you want overlay click close piroBox
	 
		});
	}
	
	$('#newsletterSubmit')
		.bind('click', submitNewsletter);
	$('#ajaxLoader').css('opacity', 0.5);
	$('input[name=use_contact_info]').bind('change', changeShippingInfo).trigger('change');
	$('#newsletterTop').bind(
		'click',
		function() {
			nwslForm = $('#newsletter_form');
			if (nwslForm.css('display') == 'none') {
				nwslForm.show().animate({top:1}, 300);
			} else {
				nwslForm.animate({top:-400}, 300, function(){$(this).hide()});
			}
			this.blur();
			return false;
		}
	);
	$('#newsletter_cancel').bind(
		'click',
		function() {
			nwslForm = $('#newsletter_form');
			if (nwslForm.css('display') == 'block') {
				nwslForm.animate({top:-400}, 300, function(){$(this).hide()});
			}
			this.blur();
			return false;
		}
	);
	slideShow.init();
	$('#productDetailsTabs a')
		.bind('click',function(){
			if (!$(this).is('.active')) {
				$('#productDetailsTabs a.active').removeClass('active');
				$(this).addClass('active').blur();
				$('#allTabs div.tabPage')
						.removeClass('active')
					.eq($('#productDetailsTabs a').index(this))
						.addClass('active');
			}
			return false;
		});
		
	if ((el = document.getElementById('checkoutInfo'))) {
		asCompany.apply($('#as_company').bind('click', asCompany).get(0));
		useBilling.apply($('#use_billing').bind('click', useBilling).get(0));
	}
	
	cartDelEl = $('.cartDelete');
	cartDelEl.bind('click', function(){
		$('#cartForm tbody:first input:checkbox').attr('checked', false);
		$(this).closest('tr').find('input:checkbox').attr('checked', true);
		$('#cartForm tfoot:first button:first').trigger('click');
	});
	
});

var asCompany = function(ev){
	if ($(this).attr('checked')) {
		$('p.asCompany').show();
		$('p.asPerson').hide();
	} else {
		$('p.asCompany').hide();
		$('p.asPerson').show();
	}
}

var useBilling = function(){
	if ($(this).attr('checked')) {
		$(this).parent().parent().find('input, select').not(this).attr('disabled', $(this).attr('checked'));
		$(this).parent().parent().find('em').hide();
	}else{
		$(this).parent().parent().find('input, select').not(this).attr('disabled', '');
		$(this).parent().parent().find('em').show();
	}
};

var changeShippingInfo = function(e) {
	if ($(this).attr('checked') === true) {
		$('input[@name=shipping_street]')
			.val($('input[@name=street]').val())
			.attr('disabled', true);
		/*$('input[@name=shipping_suburb]')
			.val($('input[@name=suburb]').val())
			.attr('disabled', true);*/
		$('input[@name=shipping_city]')
			.val($('input[@name=city]').val())
			.attr('disabled', true);
		/*$('input[@name=shipping_state]')
			.val($('input[@name=state]').val())
			.attr('disabled', true);*/
		$('input[@name=shipping_postcode]')
			.val($('input[@name=postcode]').val())
			.attr('disabled', true);
		$('select[@name=shipping_country]')
			.val($('select[@name=country]').val())
			.attr('disabled', true);
	} else {
		$('input, select', this.parentNode.parentNode.parentNode.parentNode).attr('disabled', false);
	}
}

var slideShow = {
	timer: null,
	inAnim: false,
	el: null,
	imgs: null,
	cnt: 0,
	init: function() {
		this.el = $('#header');
		if (this.el.size() == 1) {
			this.imgs = this.el.find('>a').hide();
			this.goNext();
			this.el
				.find('>p a:first')
					.bind('click', slideShow.goPrev)
					.end()
				.find('>p a:last')
					.bind('click', slideShow.goNext);
		}
	},
	goNext: function(e) {
		if (e) {
			this.blur();
		}
		if (slideShow.inAnim === true) {
			return false;
		}
		slideShow.cnt++;
		if (slideShow.cnt > slideShow.imgs.size()) {
			slideShow.cnt = 1;
		}
		slideShow.showImage(slideShow.goNext);
		return false;
	},
	goPrev: function(e) {
		if (e) {
			this.blur();
		}
		if (slideShow.inAnim === true) {
			return false;
		}
		slideShow.cnt--;
		if (slideShow.cnt <= 0) {
			slideShow.cnt = slideShow.imgs.size();
		}
		slideShow.showImage(slideShow.goPrev);
		return false;
	},
	showImage: function(fnc) {
		clearTimeout(slideShow.timer);
		slideShow.inAnim = true;
		slideShow.imgs
			.filter(':visible')
				.fadeOut(500)
				.end()
			.slice(slideShow.cnt-1, slideShow.cnt)
				.fadeIn(500);
		setTimeout(function(){
			slideShow.inAnim = false;
			slideShow.el.css('height', this.offsetHeight + 'px');
		}, 520)
		slideShow.timer = setTimeout(fnc, 8000);
	}
};

var submitNewsletter = function(e) {
	this.blur();
	var el = document.getElementById('newsletter');
	var elPosition = $.iUtil.getPosition(el);
	$('#ajaxLoader').css({
		left: elPosition.x + 'px',
		top: elPosition.y + 'px',
		width: el.offsetWidth + 'px',
		height: el.offsetHeight + 'px',
		display: 'block'
	}).fadeTo(700, 0.8);
	
	$.ajax(
		{
			type			: 'POST',
			url				: '/ajaxserver.php',
			data			: 'request=newsletter&email=' + $('#newsletterEmail').val() + '&name=' + $('#newsletterName').val() + '&language=' + engineLanguage,
			complete		: function()
			{
				$('#ajaxLoader').fadeOut(500)
			},
			
			success			: function(xml)
			{
				errors = $('error', xml);
				okies = $('ok', xml);
				if (errors.size() > 0) {
					errors.each(
						function()
						{
							alert(this.firstChild.nodeValue);
						}
					);
				} else {
					alert($('message', xml).text());
					$('#newsletterEmail').val('');
					$('#newsletterName').val('');
				}
			}
		}
	);
	
	return false;
}

var newsletterTop = function()
{
	$('#newsletter_send, #newsletter_cancel').attr('disabled', true);
	$('#newsletter_ajax_indicator').css('display', 'inline');
	
	$.ajax(
		{
			type			: 'POST',
			url				: '/ajaxserver.php',
			data			: 'request=newsletter&email=' + $('#newsletter_email').val() + '&name=' + $('#newsletter_name').val() + '&language=' + engineLanguage,
			complete		: function()
			{
				$('#newsletter_send, #newsletter_cancel').attr('disabled', false);
				$('#newsletter_ajax_indicator').hide();
			},
			
			success			: function(xml)
			{
				errors = $('error', xml);
				okies = $('ok', xml);
				
				errors.each(
					function()
					{
						alert(this.firstChild.nodeValue);
					}
				);
								
				if (errors.size() == 0) {
					var message = $('message', xml)[0].firstChild.nodeValue;
					alert(message);
					$('#newsletter_email').val('');
					$('#newsletter_name').val('');
				}
			}
		}
	);
	this.blur();
	return false;
};
