function formFieldFocus(i,n) {
	f = fields_ids[i];
	t = fields_txt[i];
	o = document.getElementById(n)[f];
	if(o.value === t) o.value = '';
	return true;
}

function formFieldBlur(i,n) {
	f = fields_ids[i];
	t = fields_txt[i];
	o = document.getElementById(n)[f];
	if(obj.value === '') obj.value = t;
	return true;
}

function submitForm(n) {
	for(i = 0; i < fields_ids.length; ++i) {
		o = document.getElementById(fields_ids[i]);
		if(o.value === fields_txt[i]) o.value = '';
	}
	document.getElementById(n)[submit()];
	return false;
}

function openInNewWindow() {
	var newWindow = window.open(this.getAttribute('href'), '_blank');
	if(newWindow) newWindow.focus();
	return false;
}

function getNewWindowLinks() {
	if (document.getElementById && document.createElement && document.appendChild) {
		var objWarningText, strWarningText, link, links = document.getElementsByTagName('a'), i;
		for (i = 0; i < links.length; i++) {
			link = links[i];
			if (link.getAttribute && link.getAttribute) {
				if (link.getAttribute('href') && link.getAttribute('rel') == 'external') link.onclick = openInNewWindow;
			}
		}
		objWarningText = null;
	}
}

// Checkout functions
function billing2deliveryAddressToggle() {
	var requiredElements = '#del_ad_title, #del_ad_firstname, #del_ad_lastname, #del_ad_line1, #del_ad_line2, #del_ad_town, #del_ad_county, #del_ad_postcode, #del_ad_country, #del_ad_phone, #del_ad_email';
	var checked_bill2del = $('input[name=bill2del]:checked').attr('value');
	if (checked_bill2del == 'yes') {
		$('#form_del_add').hide();
		$(requiredElements).removeAttr('required');
	} else {
		$('#form_del_add').show();
		$(requiredElements).attr('required', 'required');
	}
}

function billingAddressToggle() {
	var requiredElements = '#ad_title, #ad_firstname, #ad_lastname, #ad_line1, #ad_line2, #ad_town, #ad_county, #ad_postcode, #ad_country, #ad_phone, #ad_email';
	var bill_ad_id = $('input[name=bill_ad_id]:checked').attr('value');
	if (!bill_ad_id) return false;
	if (bill_ad_id == 'new') {
		$('#form_bill_add').show();
		$(requiredElements).attr('required', 'required');
	} else {
		$('#form_bill_add').hide();
		$(requiredElements).removeAttr('required');
	}
}

function deliveryAddressToggle() {
	var requiredElements = '#del_ad_title, #del_ad_firstname, #del_ad_lastname, #del_ad_line1, #del_ad_line2, #del_ad_town, #del_ad_county, #del_ad_postcode, #del_ad_country, #del_ad_phone, #del_ad_email';
	var del_ad_id = $('input[name=del_ad_id]:checked').attr('value');
	if (del_ad_id == 'new') {
		$('#form_del_add').show();
		$(requiredElements).attr('required', 'required');
	} else {
		$('#form_del_add').hide();
		$(requiredElements).removeAttr('required');
	}
}

function cardToggle() {
	var requiredElements = '#cc_number, #cc_exp_month, #cc_exp_year, #cc_cvv';
	var cc_id = $('input[name=cc_id]:checked').attr('value');
	if (cc_id == 'new' || cc_id == null) {
		$('#form_card_add').show();
		$(requiredElements).attr('required', 'required');
	} else {
		$('#form_card_add').hide();
		$(requiredElements).removeAttr('required');
	}
}

function updateDeliveryOptions() {
	var bill_country_id = $('#ad_country').attr('value');
	var del_country_id = $('#del_ad_country').attr('value');
	var checked_bill2del = $('input[name=bill2del]:checked').attr('value');
	var country_id = (checked_bill2del && checked_bill2del == 'yes')  ? bill_country_id : del_country_id;
	var del_ad_id = $('input[name=del_ad_id]:checked').attr('value');
	var ad_id = (del_ad_id != undefined && del_ad_id != 'new') ? del_ad_id : null;
	$.ajax({
		url:'ajax',
		data:'&ajaxcmd=displayDeliveryOptions&country_id=' + country_id + '&ad_id=' + ad_id,
		dataType:'html',
		success:function(output){
			$('#delivery_options').html(output);
		}
	});
}

(function() {
	document.getElementsByTagName('html')[0].className += 'wf-loading';
	var wf = document.createElement('script');
	wf.src = ('https:' == document.location.protocol ? 'https' : 'http') + '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
	wf.type = 'text/javascript';
	wf.async = 'true';
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(wf, s);
})();

WebFontConfig = {
	custom: {
		families: ['CartoGothicPro-Book', 'CartoGothicPro-Bold', 'CartoGothicPro-Italic'],
		urls: [js_base_url + 'css/font-face.css']
	}
};

$(function() {
	getNewWindowLinks();
	billing2deliveryAddressToggle();
	billingAddressToggle();
	deliveryAddressToggle();
	cardToggle();
	updateDeliveryOptions();

	// FAQ accordion
    $("#accordion").accordion();

	// Product autocomplete search
	var cache = {}, lastXhr;
	$('#search').autocomplete({
		minLength: 2,
		source: function(request, response) {
			var term = request.term;
			if (term in cache) {
				response(cache[term]);
				return;
			}

			lastXhr = $.getJSON(js_base_url + 'ajax?ajaxcmd=ajaxSearch', request, function(data, status, xhr) {
				cache[term] = data;
				if (xhr === lastXhr) {
					response(data);
				}
			});
		},
		select: function(event, ui) {
			window.location = js_base_url + ui.item.id;
		}
	});

	$('.productListing .nonMicroformatWrapper .fn').equalHeights().css('overflow','hidden');
	$('.productListing .nonMicroformatWrapper .priceWrapper').equalHeights().css('overflow','hidden');
	$('.productListing .description').equalHeights().css('overflow','hidden');

	// Colorbox //
	// Examples of how to assign the ColorBox event to elements //
	$('a[class=colorbox-elastic]').colorbox();
	$('a[class=colorbox-fade]').colorbox({transition:'fade'});
	$('a[class=colorbox-slideshow]').colorbox({slideshow:true});

	$('.noAutoComplete').attr('autocomplete', 'off');
	$('#bill2del_yes, #bill2del_no').click(billing2deliveryAddressToggle);
	$('.bill_add_item').click(billingAddressToggle);
	$('.del_add_item').click(deliveryAddressToggle);
	$('.card_item').click(cardToggle);
	$('#checkout_has_javascript').show();
	
	$('#banner').nivoSlider({
		effect: 'slideInLeft', // Specify sets like: 'fold,fade,sliceDown'
		slices: 1, // For slice animations
		boxCols: 1, // For box animations
		boxRows: 1, // For box animations
		animSpeed: 600, // Slide transition speed
		pauseTime: 4500, // How long each slide will show
		startSlide: 0, // Set starting Slide (0 index)
		directionNav: false, // Next & Prev navigation
		directionNavHide: true, // Only show on hover
		controlNav: true, // 1,2,3... navigation
		controlNavThumbs: false, // Use thumbnails for Control Nav
		controlNavThumbsFromRel: false, // Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', // Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
		keyboardNav: true, // Use left & right arrows
		pauseOnHover: true, // Stop animation while hovering
		manualAdvance: false, // Force manual transitions
		captionOpacity: 0.8, // Universal caption opacity
		prevText: 'Prev', // Prev directionNav text
		nextText: 'Next', // Next directionNav text
		randomStart: false // Start on a random slide
	});

});

