/*
	Global JS defenitions
	Copyright, 2009 ICLocator.com
*/

var ICL = {
	hash: {},
  include: function(libraryName) {
    document.write('<script type="text/javascript" src="'+libraryName+'"><\/script>');
  },
  init: function() {
		this.addShadows();
		this.addCorners();
		this.secureEmails();
		this.equalHeight($(".same-height"));

		// Default form values
		var submitForm = function(event){
			$("input.default", event.data.form).each(function(){
				if(this.value == this.title)	this.value='';
			});
			return true;
		}

		$("input.default").each(function(){
			var el = this;
			$(el).blur(function () {
				if(this.value == '') {
					$(this).removeClass('active');
					this.value = el.title;
				}
	    }).focus(function () {
				if(this.value == el.title) {
					this.value = '';
				}
				$(this).addClass('active');
			});
			if(this.value == '') this.value = el.title;
			if(this.value != el.title)$(el).addClass('active');

			// remove default values befor submission
			var form = $($(el).parents("form"));
			form.unbind("submit", submitForm).bind("submit", {form: form}, submitForm);
		});


		// Add valid attributes
		$("a.target-blank").attr({target: "_blank"});
		$(".autocomplete-off").attr({autocomplete: "off"});


		// Forms row hightlighting
		$(".form-row :input").each(function(){
			var el = $(this);
			var parent = $(el.parents(".form-row"));
			el.blur(function () {
				parent.removeClass('active');
	    }).focus(function () {
				parent.addClass('active');
			});

		});

		// Focus RFQ field
		$("#top_rfq_part, #rfq_part").focus();

		// Callout
		$(".callout").append('<em style="display:none" class="iebgfix"></em>').hover(function(){

			//hack for IE position
			if ( $.browser.msie && $.browser.version < 8 ){
				$("#getQuote").css({'z-index': 2});
				$("#header").css({'z-index': 1});
			}

			ICL.cleardelay('delayhide');

			var em = $(this).find("em");

			ICL.hash.delayhide = setTimeout(function(){
				em.css({top: -em.height()-10}).animate({
					opacity: "show",
					top: -em.height()
				}, "normal", "linear", function(){
					em.removeClass('iebgfix');
				});
			}, 500);

		}, function(){
			var em = $(this).find("em");
			ICL.cleardelay('delayhide');

			ICL.hash.delayhide = setTimeout(function(){
				em.addClass('iebgfix');
				em.animate({opacity: "hide", top: -em.height()-10}, "normal");
			}, 200);
		});
  },


  cleardelay: function(name) {
		if (this.hash[name])
			clearTimeout(this.hash[name])
  },


  load: function() {
		this.include("js/nifty.js");
		$(document).ready(function() {
			ICL.init();
		});
		$(window).load(function () {
			ICL.equalHeight($(".same-height"));
		});

	},

/* Shadows */
	addShadows: function(){
		var blocks = $(".container");
		blocks.each(function(i, el){
			$(el).addClass('shadows').wrapInner('<div class="framed"><b class="f_r"><b class="f_b"><b class="f_l"><b class="f_c"></b><b class="f_ll"><b></b></b></b><b class="f_bb"><b></b></b></b><b class="f_rr"></b></b><b class="f_tt"></b></div><b class="clear">&nbsp;</b>');
		});
	},

/* Rounded corners */
	addCorners: function(){
		$(".block-wrapper, .corners").prepend('<b class="tr"></b><b class="bl"></b><b class="br"></b><b class="tl"></b>');
		$(".corners-top").prepend('<b class="tr"></b><b class="tl"></b>');
		$(".corners-bottom").prepend('<b class="bl"></b><b class="br"></b>');
		if ($("html").hasClass("ie6")){
			$(".block-wrapper, .corners").each(function(i){
				if ( this.offsetHeight/2 != parseInt(this.offsetHeight/2))
					$(".bl:eq(0), .br:eq(0)", this).addClass("bFix");
				if ( this.offsetWidth/2 != parseInt(this.offsetWidth/2))
					$(".tr:eq(0), .br:eq(0)", this).addClass("rFix");
			});
			$(".corners-top, .corners-bottom").each(function(i){
				if ( this.offsetHeight/2 == parseInt(this.offsetHeight/2))
					$(".bl:eq(0), .br:eq(0)", this).addClass("bFix");
				if ( this.offsetWidth/2 != parseInt(this.offsetWidth/2))
					$(".tr:eq(0), .br:eq(0)", this).addClass("rFix");
			});
		}
	},

/* Cookie tools */
	getCookie: function( name ) {
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ';', len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	},
 
	setCookie: function( name, value, expires, path, domain, secure ) {
		var today = new Date();
		today.setTime( today.getTime() );
		if ( expires ) expires = expires * 1000 * 60 * 60 * 24;
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) //+ //expires.toGMTString()
		+ ';path=/;'
/*		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );*/
	},
 
	deleteCookie: function( name, path, domain ) {
		if ( getCookie( name ) ) document.cookie = name + '=' +
		( ( path ) ? ';path=' + path : '') +
		( ( domain ) ? ';domain=' + domain : '' ) +
		';expires=Thu, 01-Jan-1970 00:00:01 GMT';
	},


/* Protect emails from automatic grabbing */
	secureEmails: function () {
		$("span.change").each(function(){
			var at = / at /;
			var dot = / dot /g;
			var address = $(this).text();
			address = address.replace(at, "@");
			address = address.replace(dot, ".");
			$(this).html('<a href="mailto:'+address+'">'+address+'</a>');
		});
	},
	gotoError: function (field) {
		$(":input[name='"+field+"']").addClass('fixerror').focus();
	},
	equalHeight: function (group) {
		tallest = 0;
		group.css({height: "auto"});
		group.each(function() {
			thisHeight = $(this).height();
			if(thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		group.height(tallest);
	},
	initMenus: function (link1, link2, title) {
		var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		var catalog = '';
		var inv = '';
		var letter;
		title = title.length > 0 ? '<strong>'+title+'</strong>':'';


		ICL.hash.ul = {};
		for(i=0; i<26; i++) {
			letter = alphabet.substr(i,1);
			catalog += '<li><a href="'+link1.replace('[A-Za-z]',letter.toLowerCase())+'">'+letter+'</a></li>';
			inv += '<li><a href="'+link2.replace('{pageID}',letter)+'">'+letter+'</a></li>';
		}
		$("#top-nav .tn-quote").append('<em></em>');

		$("#top-nav .menu-catalog").after('<div class="tnm" style="display:none"><div class="bg">'+title+'<ul>'+catalog+'</ul></div></div>');

		$("#top-nav .menu-inventory").after('<div class="tnm" style="display:none"><div class="bg">'+title+'<ul>'+inv+'</ul></div></div>');

		$("#top-nav .menu").each(function(){
			var el = $(this);
			var elclass = el.attr('class').replace(/ |\./, '_');
			var ul = el.next('.tnm');
			ICL.hash.ul[elclass] = ul;

			ul.hover(function(){
				ICL.cleardelay(elclass);
			},function(){
				ICL.hash[elclass] = setTimeout(function(){
					ul.fadeOut();
				}, 100);
			});
			el.append('<em></em>').hover(function(){
				//hack for IE position
				if ( $.browser.msie && $.browser.version < 8 ){
					$("#getQuote").css({'z-index': 1});
					$("#header").css({'z-index': 2});
				}

				for(var item in ICL.hash.ul){
					if(ICL.hash.ul[item] != ul) ICL.hash.ul[item].hide();
				}
				ICL.cleardelay(elclass);
				ul.fadeIn();
			},function(){
				ICL.hash[elclass] = setTimeout(function(){
					ul.fadeOut();
				}, 300);
			});
		});
	}
}



ICL.load();




