/* Izilla Screen Javascript Template v1.4.1 (25/08/2011) */
/* http://www.izilla.com.au */

try {
	document.execCommand('BackgroundImageCache', false, true);
}
catch(e) {}

var html = $('html');
var loc = $.query.load(''+window.location);

$(document).ready(function() {
	if (!window.main_js_called) {
		HWAccel();
		WrapClears();
		Placeholder();
		
		if (!html.hasClass('lightbox'))
			Lightbox();
		if (html.hasClass('iframe') && html.hasClass('lightbox')) {
			LightboxResizeCol2();
			LightboxAppendLinks("a", ['iframe', 'lightbox']);
		}
		
		Social();
		Collections();
		Products();
		Shop();
		
		html.addClass('jquery');
		main_js_called = true;
	}	
});

function WrapClears() {
	$('.clear').wrap('<div class="clear_wrap" />');
}

// Check if hardware acceleration is enabled on Firefox for Windows. Add a class of hwa and zoom level to html as a style hook
function hwa() {
	$(html).addClass('hwa');
}

function hwaz(num) {
	$(html).addClass('zoom' + num);
}

function HWAccel() {
	if ($(html).hasClass('ua-gecko') && $(html).hasClass('ua-win')) {
		var s = ' style="bottom: 0; line-height: normal; position: absolute; visibility: hidden; font-family: Arial; font-size: ';
		$('body').append('<div id="hwa1"' + s + '20px;">1</div>').append('<div id="hwa2"' + s + '35px;">2</div>');
		
		var hwa1 = $('#hwa1');
		var hwa2 = $('#hwa2');
		
		var f1 = parseFloat(hwa1.css('font-size'));
		var h1 = parseFloat(hwa1.css('height'));
		var h2 = parseFloat(hwa2.css('height'));
		
		hwa1.remove();
		hwa2.remove();
		
		if (f1 == 20) {
			if (h1 == 25 && h2 == 41) {
				hwaz(-2);
				hwa();
			}
			else if (h1 == 25 && h2 == 40) {
				hwaz(-1);
			}
			else if (h1 == 25 && h2 == 42) {
				hwaz(-1);
				hwaz(0);
				hwa();
			}
			else if (h1 == 24 && h2 == 40) {
				hwaz(1);
			}
			else if (h1 == 23 && h2 == 40) {
				hwaz(2);
			}
			else if (h1 == 24 && h2 == 41) {
				hwaz(2);
				hwaz(0);
			}
			else if (h1 == 24 && h2 == 42) {
				hwaz(1);
				hwaz(2);
				hwa();
			}
		}
		else if (f1 == 16) {
			hwaz(-2);
			if (h1 == 20)
				hwa();
		}
		else if (f1 == 17.9833) {
			hwaz(-1);
			if (h2 == 38)
				hwa();
		}
		else if (f1 == 22) {
			hwaz(1);
			if (h2 == 46)
				hwa();
		}
		else if (f1 == 24) {
			hwaz(2);
			if (h1 == 29)
				hwa();
		}
		else if (f1 == 26.6) {
			hwaz(3);
			if (h1 == 32)
				hwa();
		}
	}
}

function Lightbox() {
	var url_specified = ['cboxclass'];
	$.each($.fn.colorbox.settings, function(k,v) {
		url_specified.push(k);
	});
	
	LightboxAppendLinks("a[class*='lightbox'], a[href*='lb=true']", ['lightbox']);

	$('a[class*="lightbox"], a[href*="lb=true"]').each(function() {
		var opts = {close: 'Close', current: '{current} of {total}', initialHeight: 50, initialWidth: 50, opacity: 0.7, transition: 'none', innerWidth: '968', innerHeight: '391', iframe: true,
			onComplete: function() {
				$('#cboxMiddleLeft').parent().attr('id', 'cboxMiddleWrapper');
				$('#cboxTitle').wrapInner('<span />');
				html.addClass('lb_open');
			},
			onClosed: function() {
				html.removeClass('lb_open');
			}
		};
		
		var url = $.query.load($(this).attr('href'));
		
		$.each(url_specified, function() {
			if (url.get(this.toLowerCase())) opts[this] = url.get(this.toLowerCase());
			if (url.get(this)) opts[this] = url.get(this);
			if (url.get(this) == 'gallery') {
				opts.opacity =  0.5,
				opts.onLoad = function($e) {
					$('#cboxOverlay').addClass('gallery');
				};
			}
		});
		
		if (opts.cboxclass) {
			opts.onOpen = function($e) {
				$('#cboxOverlay, #colorbox').addClass(opts.cboxclass);
			};
		}
		else {
			opts.onOpen = function($e) {
				$('#cboxOverlay, #colorbox').removeClass(opts.cboxclass);
			};
		}
		
		$(this).colorbox(opts);
		
	});
}

function LightboxAppendLinks(as, params) {
	$(as).each(function() {
		if (!$(this).attr('target') && $(this).attr('href')) {
			var href = $(this).attr('href').split('#');
	
			if (href[0].indexOf('?') > 0)
				href[0] += '&';
			else
				href[0] += '?';
			
			$(params).each(function(idx) {
				if (idx != 0)
					href[0] += '&';
				href[0] += this + '=true';
			})
				
			var total = href[0];
			if (href.length > 1)
				total += '#' + href[1];
				
			$(this).attr('href', total);
		}
	});
}

function LightboxResizeCol2() {
	if ($('.master-wrapper-page').outerHeight() >= parseInt(loc.get('innerHeight'))-parseInt($(parent.document.getElementById('cboxLoadedContent')).css('margin-top')))
		$('.master-wrapper-page').addClass('longer');
}

function Placeholder() {
	if(!Modernizr.input.placeholder){
		$('[placeholder]').focus(function() {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
				input.val('');
				input.removeClass('placeholder');
			}
		}).blur(function() {
			var input = $(this);
			if (input.val() == '' || input.val() == input.attr('placeholder')) {
				input.addClass('placeholder');
				input.val(input.attr('placeholder'));
			}
		}).blur();
		$('[placeholder]').parents('form').submit(function() {
			$(this).find('[placeholder]').each(function() {
				var input = $(this);
				if (input.val() == input.attr('placeholder')) {
					input.val('');
				}
			})
		});
	}
}

function Social() {
	$('#social span').click(function(e) {
		window.location = $(this).data('href');
	});
}

function Collections() {
	var $colls = $('#colls');
	
	$('h3', $colls).each(function(index, element) {
		$(this).prependTo('#collections');
		$(this).click(function(e) {
			var curr = 'current';
			e.stopPropagation();
			$('#collections > li').removeClass(curr);
			$('#c_' + $(this).attr('id')).parent().addClass(curr);
			$(this).siblings('h3').removeClass(curr);
			$(this).addClass(curr);
		});
	});
	
	$('body').click(function(e) {
		$colls.removeClass('open');
	});
	
	$('.btn', $colls).click(function(e) {
		$colls.toggleClass('open');
		e.stopPropagation();
		e.preventDefault();
	}); 
}

function Products(offline) {
	if (offline)
		html.addClass('offline');
		
	$('#inner .p').each(function(index, element) {
		var coords = $(this).attr('data-position').split(',');
		var x = coords[0] + 'px';
		var y = coords[1] + 'px';
		var z =  $(this).attr('data-zindex');
		$(this).css({'left': x, 'top': y, 'z-index': z}).append('<span class="h" />');
		if (!$(this).find('img').attr('data-src')) {
			var hover = 'url(' + $(this).find('img').attr('src').replace(/\.png|\.jpg$/, '') + '_hover.png)';
			$('.h', $(this)).css('background-image', hover);
		}
	});
	
	var prevpage = '';
	
	var matches = window.location.href.match(/\/(\d+)\/view-/);
	if (matches != null && matches.length >= 2)
		prevpage = matches[1];
		
	$('#back').attr('href', $('#back').attr('href') + '#' + escape(prevpage));
	
	var $slides = $('#inner > .products');
	
	$slides.each(function(index, element) {
		var $price = $('.price', this);
		var price;
		if ($price.length == 1 && $price.text().length == 0) {
		var $product = $('.btn', this);
			var productID = $product.attr('href').replace('?lightbox=true', '');
			if ($.cookie(productID)) {
				$price.html($.cookie(productID));
			}
			else {
				$price.load($product.attr('href') + ' .productPrice:first', function() {
					price = 'AUD$ ' + $(this).text().replace('.00', '');
					$(this).html(price);
					$.cookie(productID, price);
				});
			}
			if (offline) {
				$product.html('Enquire').attr('href', '/contact.html').removeClass('lightbox cboxElement');
				
			}
		}
	});
	
	if ($slides.length > 1) {
		var $navPager = $('<div id="pager"><ul><li><a id="prev">Previous</a></li><li><a id="next">Next</a></li></ul><div id="page" /></div>');
		
		var speed = html.hasClass('ltie9') ? 150 : 400; 
		
		function onAfter(curr, next, opts) {
			$(next).removeClass('hide');
			var prev = (opts.currSlide == 0 ? $slides.length : opts.currSlide);
			$('#prev').attr('href', '#' + prev);
			var next = (opts.currSlide+1 == $slides.length ? 0 : opts.currSlide + 1);
			$('#next').attr('href', '#' + (next+1));
			$('#page').html((opts.currSlide + 1) + ' / ' + $slides.length);
		}
		
		function onBefore(curr, next, opts) {
			$(curr, next).addClass('hide');
			$(next).find('img').each(function() {
				var dataSrc = $(this).attr('data-src');
				if (dataSrc) {
					var dataSrcA = dataSrc.replace(/\.png|\.jpg$/, '');
					$('#inner').append('<img src="/images/loader.gif" class="loader" id="l_' + dataSrcA.replace(/\//g, '') + '"/>');
					$(this).load(function() {
						$('#l_' + dataSrcA.replace(/\//g, '')).remove();
					}).attr('src', dataSrc);
					var hover = 'url(' + dataSrcA + '_hover.png)';
					$(this).siblings('.h').css('background-image', hover);
					$(this).removeAttr('data-src');
				}
			});
		}
		
		$('#inner').after($navPager).cycle({
			activePagerClass: 'current',
			after: onAfter,
			before: onBefore,
			pause: 1,
			speed: speed,
			timeout: 0
		});
		
		var $links = $('<div id="links" />');
		$links.hide();
		for (var i = 1; i <= $slides.length; ++i) {
			$links.append($('<a href="#'+i+'">' + i + '</a>'));
		}
		$('body').append($links);
		
		$('#next, #prev, #links a').history(function(event,hash) {
			if (hash == undefined) return;
			var pa = parseInt(hash.replace('#',''));
			if (isNaN(pa)) return;
	
			$('#inner').cycle(pa - 1);
		});
	}
	else
		$('#footer').addClass('no_slide');
}

function Shop() {
	if (html.hasClass('admin'))
		$('.returning-wrapper').show();
	else
		$('#guest-checkout').trigger('click');
	
	if (html.hasClass('lightbox')) {
		var $addCart = $('.productvariantaddtocartbutton');
		if ($addCart.length == 1)
			$addCart.trigger('click');
		else if ($addCart.length > 1) {
			$variants = $('<div id="variants" />');
			$variants.prepend('<h1>Select a variant</h1>');
			$('.master-wrapper-page').append($variants);
			$('.product-variant-line').each(function(index, element) {
				var variant = {
					name: $('.productname', this).text().replace(/^\s*|\s*$/g,''),
					id: $('.productvariantaddtocartbutton', this).attr('name')
				}
				var $add = $('<a href="#add-to-cart" class="btn">'+ variant.name + '</a>');
				$add.click(function(e) {
					$('input[name="'+ variant.id + '"]').trigger('click');
					return false;
				});
				$variants.append($add);
			});
		}
			
		var $orderContinue = $('.orderprocessedcontinuebutton');
		if ($orderContinue.length > 0) {
			$orderContinue[0].onclick = null;
			$orderContinue.click(function(e) {
				if (parent.$.fn.colorbox)
					parent.$.fn.colorbox.close();
				else
					window.location = '/';
			});
		}
	}
	
	if ($('.order-summary-content').length && $('.order-summary-content').html().indexOf('empty') >= 0)
		$('.order-progress').hide();
}
