$(document).ready(function() {
	if ($('#map_canvas').length) {
		initializeGMaps();
	}
	$('.trading-hours, .opening-hours').click(function() {
		return false;
	});

	$('.thumbArea section').click(function() {
		if($(this).find('a').attr('href')) {
			window.location.href = $(this).find('a').attr('href');
		}
	});

	if ($('#contact-form').length) {
		$('#contact-form input[type=text]').each(function() {
			$(this).val($(this).attr('title'));
		});
		$('#contact-form input[type=text]').click(function() {
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('');
			}
		});
		$('#contact-form input[type=text]').blur(function() {
			if ($(this).val() == '') {
				$(this).val($(this).attr('title'));
			}
		});

		$('#fileUpload').customFileInput();	
	}

	$('.lnk-click4call').click(function() {
		window.open('http://c2c.avanser.com/ajax/form.php?show=html&clientId=6052&buttonId=20','_blank','height=340,width=420');
		return false;
	});

	$('ul#topnav li').mouseover(function() {
		if ($(this).attr('id') != 'active-cat') {
			$('#active-cat span').hide();
		}
	});
	$('ul#topnav li').mouseout(function() {
		if ($(this).attr('id') != 'active-cat') {
			$('#active-cat span').show();
		}
	});
});

/**
 * Homepage - Change banner image through menu
 */
function changeImage(id) {
	var d=1;
	aSlider.params.speed = 200;
	aSlider.params.delay = 0;
	if (aSlider.current<parseInt(id)) { d=-1; }
	aSlider.show(parseInt(id)-1,d);
	aSlider.autostop();	
}
	
function resetImage() {
	aSlider.params.speed = 2000;
	aSlider.params.delay = 5000;

	aSlider.autostart();	
}

/**
 * GMaps contact init
 */
function initializeGMaps() {
	var latlng = new google.maps.LatLng(-33.738295, 151.074354);
	var myOptions = {
		zoom: 16,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	var marker = new google.maps.Marker({
		position: latlng, 
		map: map, 
		title: "Pergola Land Pty Ltd"
	});
}

/**
 * Validate contact form
 */
function validate_form() {
	var _regEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	has_errors = false;
	
	$('#contact-form .req').each(function() {
		if ($(this).val() == '' || $(this).val() == $(this).attr('title')) {
			has_errors = true;
			$(this).addClass('error');
		}
		else {
			$(this).removeClass('error');
		}
	});

	if ($('#inquiry_source').val() + '' == '') {
		has_errors = true;
		$('#inquiry_source_label').addClass('error');
	}
	else {
		$('#inquiry_source_label').removeClass('error');
	}

	if(!$('.check').is(':checked')) {
		has_errors = true;
		$('#products_interest_label').addClass('error');
	}
	else {
		$('#products_interest_label').removeClass('error');
	}

	$('.validate-email').each(function() {
		if ($(this).hasClass('req') && $(this).val() == $(this).attr('title'))
		{
			has_errors = true;
			$(this).addClass('error');
		}
		else if ($(this).val() + '' != '' && $(this).val() != $(this).attr('title') && !_regEmail.test($(this).val())) {
			has_errors = true;
			$(this).addClass('error');
		}
		else {
			$(this).removeClass('error');
		}
	});

	if (!has_errors) {
		$('#contact-form input').each(function() {
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('');
			}
		});
	}

	//if (has_errors) {
	//	$('html,body').animate({scrollTop: 0}, { duration: 'slow'});
	//}
	return !has_errors;
}

function showGalleryImage(id, index) {

	$('.thumbrow li').removeClass('active');
	$('.thumbrow li.thumb-' + id).addClass('active');

	$('.imageDisplaycnt img').attr('src', $('.thumbrow li.thumb-' + id + ' a').attr('href'));

	$('#iImg').html(index);

	return false;
}

function largeGalleryNext() {
	current = $('.thumbrow li.active').index();
	total = $('.thumbrow li').length;

	$('.thumbrow li').removeClass('active');

	if (current == total - 1) {
		next = 1;
		$('.thumbrow li:first').addClass('active');
		$('.imageDisplaycnt img').attr('src', $('.thumbrow li:first a').attr('href'));
	}
	else {
		next = current + 1;
		$('.thumbrow li:eq(' + next + ')').addClass('active');
		$('.imageDisplaycnt img').attr('src', $('.thumbrow li:eq(' + next + ') a').attr('href'));
	}

	$('#iImg').html($('.thumbrow li.active').index() + 1);
	return false;
}

function largeGalleryPrev() {
	current = $('.thumbrow li.active').index();
	total = $('.thumbrow li').length;

	$('.thumbrow li').removeClass('active');

	if (current == 0) {
		next = total;
		$('.thumbrow li:last').addClass('active');
		$('.imageDisplaycnt img').attr('src', $('.thumbrow li:last a').attr('href'));
	}
	else {
		next = current - 1;
		$('.thumbrow li:eq(' + next + ')').addClass('active');
		$('.imageDisplaycnt img').attr('src', $('.thumbrow li:eq(' + next + ') a').attr('href'));
	}

	$('#iImg').html($('.thumbrow li.active').index() + 1);
	return false;
}

/**
 * jquery.simpletip 1.3.1. A simple tooltip plugin
 * 
 * Copyright (c) 2009 Craig Thompson
 * http://craigsworks.com
 *
 * Licensed under GPLv3
 * http://www.opensource.org/licenses/gpl-3.0.html
 *
 * Launch  : February 2009
 * Version : 1.3.1
 * Released: February 5, 2009 - 11:04am
 */
(function($){function Simpletip(elem,conf){var self=this;elem=jQuery(elem);var tooltip=jQuery(document.createElement('div')).addClass(conf.baseClass).addClass((conf.fixed)?conf.fixedClass:'').addClass((conf.persistent)?conf.persistentClass:'').html(conf.content).appendTo(elem);if(!conf.hidden)tooltip.show();else tooltip.hide();if(!conf.persistent){elem.hover(function(event){self.show(event)},function(){self.hide()});if(!conf.fixed){elem.mousemove(function(event){if(tooltip.css('display')!=='none')self.updatePos(event);});};}else
{elem.click(function(event){if(event.target===elem.get(0)){if(tooltip.css('display')!=='none')self.hide();else
self.show();};});jQuery(window).mousedown(function(event){if(tooltip.css('display')!=='none'){var check=(conf.focus)?jQuery(event.target).parents('.tooltip').andSelf().filter(function(){return this===tooltip.get(0)}).length:0;if(check===0)self.hide();};});};jQuery.extend(self,{getVersion:function(){return[1,2,0];},getParent:function(){return elem;},getTooltip:function(){return tooltip;},getPos:function(){return tooltip.offset();},setPos:function(posX,posY){var elemPos=elem.offset();if(typeof posX=='string')posX=parseInt(posX)+elemPos.left;if(typeof posY=='string')posY=parseInt(posY)+elemPos.top;tooltip.css({left:posX,top:posY});return self;},show:function(event){conf.onBeforeShow.call(self);self.updatePos((conf.fixed)?null:event);switch(conf.showEffect){case'fade':tooltip.fadeIn(conf.showTime);break;case'slide':tooltip.slideDown(conf.showTime,self.updatePos);break;case'custom':conf.showCustom.call(tooltip,conf.showTime);break;default:case'none':tooltip.show();break;};tooltip.addClass(conf.activeClass);conf.onShow.call(self);return self;},hide:function(){conf.onBeforeHide.call(self);switch(conf.hideEffect){case'fade':tooltip.fadeOut(conf.hideTime);break;case'slide':tooltip.slideUp(conf.hideTime);break;case'custom':conf.hideCustom.call(tooltip,conf.hideTime);break;default:case'none':tooltip.hide();break;};tooltip.removeClass(conf.activeClass);conf.onHide.call(self);return self;},update:function(content){tooltip.html(content);conf.content=content;return self;},load:function(uri,data){conf.beforeContentLoad.call(self);tooltip.load(uri,data,function(){conf.onContentLoad.call(self);});return self;},boundryCheck:function(posX,posY){var newX=posX+tooltip.outerWidth();var newY=posY+tooltip.outerHeight();var windowWidth=jQuery(window).width()+jQuery(window).scrollLeft();var windowHeight=jQuery(window).height()+jQuery(window).scrollTop();return[(newX>=windowWidth),(newY>=windowHeight)];},updatePos:function(event){var tooltipWidth=tooltip.outerWidth();var tooltipHeight=tooltip.outerHeight();if(!event&&conf.fixed){if(conf.position.constructor==Array){posX=parseInt(conf.position[0]);posY=parseInt(conf.position[1]);}else if(jQuery(conf.position).attr('nodeType')===1){var offset=jQuery(conf.position).offset();posX=offset.left;posY=offset.top;}else
{var elemPos=elem.offset();var elemWidth=elem.outerWidth();var elemHeight=elem.outerHeight();switch(conf.position){case'top':var posX=elemPos.left-(tooltipWidth/2)+(elemWidth/2);var posY=elemPos.top-tooltipHeight;break;case'bottom':var posX=elemPos.left-(tooltipWidth/2)+(elemWidth/2);var posY=elemPos.top+elemHeight;break;case'left':var posX=elemPos.left-tooltipWidth;var posY=elemPos.top-(tooltipHeight/2)+(elemHeight/2);break;case'right':var posX=elemPos.left+elemWidth;var posY=elemPos.top-(tooltipHeight/2)+(elemHeight/2);break;default:case'default':var posX=(elemWidth/2)+elemPos.left+20;var posY=elemPos.top;break;};};}else
{var posX=event.pageX;var posY=event.pageY;};if(typeof conf.position!='object'){posX=posX+conf.offset[0];posY=posY+conf.offset[1];if(conf.boundryCheck){var overflow=self.boundryCheck(posX,posY);if(overflow[0])posX=posX-(tooltipWidth/2)-(2*conf.offset[0]);if(overflow[1])posY=posY-(tooltipHeight/2)-(2*conf.offset[1]);}}else
{if(typeof conf.position[0]=="string")posX=String(posX);if(typeof conf.position[1]=="string")posY=String(posY);};self.setPos(posX,posY);return self;}});};jQuery.fn.simpletip=function(conf){var api=jQuery(this).eq(typeof conf=='number'?conf:0).data("simpletip");if(api)return api;var defaultConf={content:'A simple tooltip',persistent:false,focus:false,hidden:true,position:'default',offset:[0,0],boundryCheck:true,fixed:true,showEffect:'fade',showTime:150,showCustom:null,hideEffect:'fade',hideTime:150,hideCustom:null,baseClass:'tooltip',activeClass:'active',fixedClass:'fixed',persistentClass:'persistent',focusClass:'focus',onBeforeShow:function(){},onShow:function(){},onBeforeHide:function(){},onHide:function(){},beforeContentLoad:function(){},onContentLoad:function(){}};jQuery.extend(defaultConf,conf);this.each(function(){var el=new Simpletip(jQuery(this),defaultConf);jQuery(this).data("simpletip",el);});return this;};})();
