/* --------------------------------------------------
   Script allows for multiple image and preview image
   swapping
   Copyright (C) Colin Mc Mahon, Protomatter Web
   Solutions 2006
   -------------------------------------------------- */
var ImageSwitcher = {
	aImage : Array,
	aThumb : Array,
	oMainImg  : Object,
	curImage : 0,
	init : function(mic, mi, ti) {
		var i = 0;
		this.aImage = mi;
		this.aThumb = ti;
		this.oMainImg = document.getElementById('main_image');
		this.oThumbConts = new Array;
		this.curImage = 0;
		this.run();
	},
	run : function() {
		var me = this;
		$('#thumb_images img').each(function(i){
			this.imgid = i;
			$(this).click(function(){
				if (this.imgid != me.curImage) {
					me.oMainImg.src = '/assets/images/products/' + me.aImage[this.imgid];
					me.curImage = this.imgid;
					me.setupThumbs();
				}
			});
		});
		this.setupThumbs();
	},
	setupThumbs : function() {
		var me = this;
		$('#thumb_images img').each(function(){
			if (this.imgid == me.curImage) {
				$(this).addClass('current');
			} else {
				$(this).removeClass('current');
			}
		});
	}
}

/* --------------------------------------------------
   Script allows for popup tooltips on elements supporting
   title and alt or p elements
   Copyright (C) Colin Mc Mahon, Protomatter Web
   Solutions 2006
   -------------------------------------------------- */
var AddToolTips = {
	starttiptimer : Object,
	tiptimer : Object,
	starttiptimer_started : Boolean,
	tiptimer_started : Boolean,
	init: function() {
		var me = this;
		me.starttiptimer_started = false;
		me.tiptimer_started = false;
		/*var offx = 20;
		var offy = -5;*/
		$('.showtooltip').each(function(i){
			if($(this).is('p')) {
				var txt = $(this).html();
			} else {
				alert('not p');
			}
			$(this).hover(function(){
				var elem = this;
				me.starttiptimer_started = true;
				me.starttiptimer = setTimeout(function(){
					me.BuildToolTip(i, txt, elem);
					$('#tooltip_msg' + i).fadeTo('fast', 0.2);
					me.tiptimer_started = true;
					me.tiptimer = setTimeout(function(){
						$('#tooltip_msg' + i).fadeTo('fast', 1);
					}, 1000);
				}, 500);
			},
			function(){
				me.ClearTimeouts();
				$('#tooltip_msg' + i).remove();
			});
			$(this).click(function(){
				var elem = this;
				me.ClearTimeouts();
				$('#tooltip_msg' + i).remove();
				me.BuildToolTip(i, txt, elem);
				$('#tooltip_msg' + i).fadeTo(1, 1);
			});
		});
	},
	BuildToolTip : function(i, txt, elem) {
		$('body').prepend('<div id="tooltip_msg' + i + '" class="tooltip"><p></p></div>')
		$('#tooltip_msg' + i + ' p').html(txt);
		var c = jQuery.iUtil.getClient();
		var t = jQuery.iUtil.getClient($('#tooltip_msg' + i));
		var e = jQuery.iUtil.getPos(elem);
		var cw = c.w;
		var ch = c.h;
		var tw = t.w;
		var th = t.h;
		var ex = e.x;
		var ey = e.y;
		var x = ex + 17;
		var y = ey + 17;
		if ((x + tw) > cw) {
			$('#tooltip_msg' + i).css({left: x-tw + 'px'});
		} else {
			$('#tooltip_msg' + i).css({left: x + 'px'});
		};

		if ((y + th) > ch) {
			$('#tooltip_msg' + i).css({top: y-th + 'px'});
		} else {
			$('#tooltip_msg' + i).css({top: y + 'px'});
		};
	},
	ClearTimeouts : function() {
		if(this.starttiptimer_started) {
			clearTimeout(this.starttiptimer);
			this.starttiptimer_started = false;
		}
		if(this.tiptimer_started) {
			clearTimeout(this.tiptimer);
			this.tiptimer_started = false;
		}
	}
}


/* --------------------------------------------------
   Script sets text on an input box and clears it
   when it gains the focus, it also prevents submission
   of the containing form if blank or default
   Copyright (C) Colin Mc Mahon, Protomatter Web
   Solutions 2006
   -------------------------------------------------- */
var Searchbox = {
	init : function(in_elem, in_text) {
		var sBox = document.getElementById(in_elem);
		if (sBox) {
			$('#' + in_elem).click(function() {
				if (this.value == in_text) {
					this.value = '';
				}
			});
			$('#' + in_elem).blur(function() {
				if (this.value == '' || this.value == ' ') {this.value = in_text;}
			});
		}	
	}
}


/* --------------------------------------------------
   Script checks for required class form elements and
   blocks form submission if blank
   Copyright (C) Colin Mc Mahon, Protomatter Web
   Solutions 2006
   -------------------------------------------------- */
var Checkform = {
	init : function(in_text) {
		$("input[@type='submit']").click(function(){
			ok = true;
			$(this).parents("form").find(".required").each(function(){
				if(this.value=='' || this.value == in_text || this.value == '-1') { 
					ok = false;
					$(this).parents('label').addClass('field_error');
					$(this).parents('.form_row').addClass('field_error');
				} else {
					$(this).parents('label').removeClass('field_error');
					$(this).parents('.form_row').removeClass('field_error');
				};                    
			});
			if(ok==false) {
				alert('Please complete all required fields!');
				return false;                    
			};
		});
	}
}


StartList = function() {
	if (document.all && document.getElementById) {
		$("#primary_navigation li").hover(
			function(){
				$(this).addClass('hover');
			},
			function(){
				$(this).removeClass('hover');
			}
		);
	}
}


DOMManip = {
	externallinks : function () {
		if (!document.getElementsByTagName) return;
		var anchors = document.getElementsByTagName("a");
		for (var i=0; i<anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
				anchor.target = "_blank";
		}
	},
	init : function() {
		DOMManip.externallinks();
	}
};


var ToggleOrderButton = {
	init : function() {
		$('#wpsend_termsagreed').click(function() {
			if(this.checked) {
				$('#wpsend_submit').removeClass('disabled');
				$('#wpsend_submit').removeAttr('disabled');
			} else {
				$('#wpsend_submit').addClass('disabled');
				$('#wpsend_submit').attr({disabled: 'disabled'});
			}
		});
	}
}


var Forms = {
	highlightFields : function (in_fields) {
		$(document).ready(function(){
			Forms._highlightFields(in_fields)
		});
	},
	_highlightFields : function(in_fields) {
		var fldAr = in_fields.split(",");
		for (var i=0; i<fldAr.length; i++) {
			this.highlightField(fldAr[i]);
		}
	},
	highlightField : function(in_elem) {
		var tmpElem = in_elem;
		if (in_elem.indexOf('file_')!=-1 || in_elem.indexOf('img_')!=-1) {
			tmpElem = in_elem.split("__");
			tmpElem = tmpElem[1];
		} else {
			tmpElem = in_elem;
		}
		var fld = document.getElementById(tmpElem);
		var lbl = fld.parentNode;
		$(fld).parents('label').addClass('field_error');
		$(fld).parents('.form_row').addClass('field_error');
	}
};


var countdown = {
	init : function(in_seconds) {
		$("#session_timer span").html(in_seconds);
	},
	
	render_countdown : function(outformat,countdn,spanid) {
		if (countdn <= 1) {
			document.getElementById(spanid).innerHTML = ":Countdown Completed";
		} else {
		var secs = countdn % 60;
		if (secs < 10) secs = '0'+secs;
		var countdn1 = (countdn - secs) / 60;
		var mins = countdn1 % 60;
		if (mins < 10) mins = '0'+mins;countdn1 = (countdn1 - mins) / 60;
		var hours = (countdn1 % 24);
		var days = (countdn1 - hours) / 24;
		
		var outhtml = new String(outformat);
		outhtml = outhtml.replace(/%days%/,days);
		outhtml = outhtml.replace(/%hours%/,hours);
		outhtml = outhtml.replace(/%mins%/,mins);
		outhtml = outhtml.replace(/%secs%/,secs);
		
		if ( days < 2 ) {
		outhtml = outhtml.replace(/days/,'day');
		}
		document.getElementById(spanid).innerHTML = outhtml;
		setTimeout('display_countdown(\''+ outformat + '\','+(countdn-1)+',\''+spanid+'\');',999);
		}
	}
}


/* --------------------------------------------------
   General page initialisation
   -------------------------------------------------- */
$(function(){
	StartList();
	Checkform.init();
});