// ================== 
// ! Main Functions   
// ================== 

	function pageload() {
		$('a.newwindow').click(function(){
			window.open(this.href);
			return false;
		}); 
		$("#search #criteria").inputhint(); 
		$("li.navopt, li.navcur").mouseover(function(){
		    $(this).find(".nav_dropdown").show();					    
			}).mouseout(function(){
		  	$(this).find(".nav_dropdown").hide();
		});

		var searchQuery = "";
		$("#criteria").keyup(function() { 
			searchQuery = $('#criteria').val();
			$('#livesearch').fadeIn();	         
		    livesearch(searchQuery, base_url); 
		});
		$("#criteria").blur(function () { $("#livesearch").fadeOut(); });
		$("#criteria").focus(function () { 
			searchQuery = $('#criteria').val();
			livesearch(searchQuery, base_url); 
		});	

		$('.startform').live('click', function() {
			popup_quote();
			return false;
		});
		$('a.chatwindow').click(function(){
			window.open(this.href,'Live Chat', 'width=448, height=360');
			return false;
		});		
		



// ==============
// ! Quote Form  
// ==============

		var quote_name, quote_phone, quote_email, quote_comment, quote_phone_ext;
		$("#quote_name, #quote_phone, #quote_phone_ext, #quote_email, #quote_comment").inputhint();	
		$("#quote_name, #quote_phone, #quote_email, #quote_comment").blur ( function() { $(this).form_required(); });	

        $("#quote_name").blur (function() { if ($("#quote_name").val() != "Name") { $(this).isValidateFirstNameAndSecondName(); }}); 
		$("#quote_email").blur (function() { if ($("#quote_email").val() != "Company Email") { $(this).form_realemail(); }});

		$("#quote_name, #quote_phone, #quote_phone_ext, #quote_email, #quote_comment").change ( function() { 
			userinteraction($("#quote_name").val(), $("#quote_email").val(), $("#quote_phone").val(), $("#quote_comment").val(), base_url, $("#quote_visitorid").val(), $("#quote_pageid").val(), "13");
		});	

		$('#quote .quotesubmit').click(function() { 
				$("quote_name, #quote_phone, #quote_email, #quote_comment").form_required();
                                $("#quote_name").blur (function() { if ($("#quote_name").val() != "Name") { $(this).isValidateFirstNameAndSecondName(); }});
				$("#quote_email").form_realemail();
				
				if ($("#quote_name").val() == "Name") { $("#quote_name").css("border-color", "#db0000").parent().find('.error').show(); }
				if ($("#quote_phone").val() == "Phone Number") { $("#quote_phone").css("border-color", "#db0000").parent().find('.error').show(); }
				if ($("#quote_email").val() == "Company Email") { $("#quote_email").css("border-color", "#db0000").parent().find('.error').show(); }
				if ($("#quote_comment").val() == "Please describe the system or upgrade you need here.") { $("#quote_comment").css("border-color", "#db0000").parent().find('.error').show(); }	
				
				if ( $(this).parent().find('.error').is(':visible') ) { return false; } else {
                    var nameValue = $("#quote_name").val();
                    var firstNameValue = nameValue.split(" ")[0];
                    var lastNameValue = nameValue.substring(firstNameValue.length + 1);
                    $("#quote_firstname").val(firstNameValue);
                    $("#quote_lastname").val(lastNameValue);
                    $(this).parent().append($('<input type="hidden" name="valid" value="1"/>'));
                    sendPardotRequest(firstNameValue, lastNameValue, $("#quote_email").val());
                    return false;
                }
		});


// ===================== 
// ! Pop-Up Quote Form   
// ===================== 


		var quote_name2, quote_phone2, quote_email2, quote_comment2;
		$("#quote_name2, #quote_phone2, #quote_email2, #quote_comment2").inputhint();	
		$("#quote_name2, #quote_phone2, #quote_email2, #quote_comment2").blur ( function() { $(this).form_required(); });	
	
        $("#quote_name2").blur (function() { if ($("#quote_name2").val() != "Name") { $(this).isValidateFirstNameAndSecondName(); }});  
		$("#quote_email2").blur (function() { if ($("#quote_email2").val() != "") { $(this).form_realemail(); }});

		$("#quote_name2, #quote_phone2, #quote_email2, #quote_comment2").change ( function() { 
			userinteraction($("#quote_name2").val(), $("#quote_email2").val(), $("#quote_phone2").val(), $("#quote_comment2").val(), base_url, $("#quote_visitorid2").val(), $("#quote_pageid2").val(), "63");
		});	

		$('.submitquote').click(function() { 
				$("#quote_name2, #quote_phone2, #quote_email2, #quote_comment2").form_required(); 
                                $("#quote_name2").blur (function() { if ($("#quote_name2").val() != "Name") { $(this).isValidateFirstNameAndSecondName(); }});
				$("#quote_email2").form_realemail();
				
				if ($("#quote_name2").val() == "Name") { $("#quote_name2").css("border-color", "#db0000").parent().find('.error').show(); }
				if ($("#quote_phone2").val() == "Phone Number") { $("#quote_phone2").css("border-color", "#db0000").parent().find('.error').show(); }
				if ($("#quote_email2").val() == "Company Email") { $("#quote_email2").css("border-color", "#db0000").parent().find('.error').show(); }
				if ($("#quote_comment2").val() == "Please describe the system or upgrade you need here.") { $("#quote_comment2").css("border-color", "#db0000").parent().find('.error').show(); }	
				
				if ( $(this).parent().find('.error').is(':visible') ) { return false; } else {
                    var nameValue = $("#quote_name2").val();
                    var firstNameValue = nameValue.split(" ")[0];
                    var lastNameValue = nameValue.substring(firstNameValue.length + 1);
                    $("#quote_firstname2").val(firstNameValue);
                    $("#quote_lastname2").val(lastNameValue);
                    $(this).parent().append('<input type="hidden" name="valid" value="1"/>');
                    sendPardotRequest(firstNameValue, lastNameValue, $("#quote_email2").val());
                    return false;
                }
		});

		
	}

    function sendPardotRequest(firstName, lastName, email) {
        $.ajax({
          type: 'POST',
          url: 'http://www2.reliant-technology.com/l/9042/2011-10-14/4YE3',
          data: {ContactFirstName: firstName,
                  ContactLastName: lastName,
                  Contact0Email: email},
          success: function() {
              $('form:has(input[name=valid])').submit();
          }






        });
    }

// ================ 
// ! Equal Heights   
// ================ 


function equalTabHeights() {
	var height = 0;
	$('.tabs').children().each(function () {
		var thisheight = $(this).height(); 
		var last = $('.tabs li:last').index('.tabs li');
		var current = $(this).index('.tabs li'); 
		if (thisheight > height) { height = thisheight; }
		if (current == last) { $('.tabs li').css('height', height + 'px'); }
	});
}			


// ======== 
// ! Tabs   
// ======== 

		jQuery.fn.tabs = function () {
		  return this.each(function (){
		  	$(this).find('ul.content li.tab').hide();
		  	var selected = $(this).find('ul.tabs').attr('title');
		  	$(this).find('ul.tabs').attr('title', '');
			var tabnum = 1;
				$(this).find('ul.tabs li').each(function () {
					$(this).addClass("tab" + tabnum);
					if (tabnum == selected) { $(this).removeClass().addClass('selected'); } 
					tabnum++;
				});
				tabnum = 1;
				$(this).find('ul.content li.tab').each(function () {
					$(this).addClass('tab' + tabnum);
					if (tabnum == selected) { $(this).show(); }
					tabnum++; 
				});		  	
				$('ul.tabs li a').click(function() {
					tabnum = 1;
					var tabName = $(this).parent();
					$(this).parent().parent().find("li").each(function () {
							$(this).removeClass().addClass('tab' + tabnum);
							if ($(this).attr('class') == tabName.attr('class')) { 
								selected = tabnum; 
								$(this).removeClass().addClass('selected');	
							}
							tabnum++;
					});
					tabnum = 1;
					$(this).parent().parent().parent().parent().find('li.tab').each(function () {
						if (tabnum == selected) { $(this).show(); } 
						else { $(this).hide(); }
						tabnum++;
					});					
					return false;
				});	
				
				var pageHash = window.location.hash;
				var hashTab;
				if (pageHash == "#Overview") { gototab("Overview"); return false; }
				if (pageHash == "#Technical_specs") { gototab("Specs"); return false; }
				if (pageHash == "#Upgrade_options") { gototab("Upgrades"); return false; }
				if (pageHash == "#Current_specials") { gototab("Specials"); return false; }
				if (pageHash == "#Warranty") { gototab("Warranty"); return false; }
				
				$('a.tab_overview').click(function() { gototab("Overview"); return false; });			
				$('a.tab_specs').click(function() { gototab("Specs"); return false; });			
				$('a.tab_upgrades').click(function() { gototab("Upgrades"); return false; });			
				$('a.tab_specials').click(function() { gototab("Specials"); return false; });			
				$('a.tab_warranty').click(function() { gototab("Warranty"); return false; });			
		 	});
		}

		jQuery.fn.fasttabs = function () {
			return this.each(function (){
		  		$('body').find('ul#tab_content li').hide();
		  		var currentSelection = $(this).find('li.selected').index('li');
		  		// alert (currentSelection);
	  			$('body').find('ul#tab_content li').hide();
	  			$('body').find('ul#tab_content li:eq(' + currentSelection + ')').show();
		  		
				$(this).find('li a').click(function() {
		  			currentSelection = $(this).parent().index('li');
		  			// alert (currentSelection);
		  			$(this).parent().parent().find('li.selected').removeClass('selected');
		  			$(this).parent().addClass('selected');
		  			$('body').find('ul#tab_content li').hide();
		  			$('body').find('ul#tab_content li:eq(' + currentSelection + ')').show();
		  			return false;
				});
		  		
			});
		}




// ============== 
// ! Input Hint   
// ============== 


		jQuery.fn.inputhint = function () {
			return this.each(function (){
				var hintvalue = $(this).attr('value');
				$(this).css('color', '#777');
				$(this).focus(function() {
					$(this).css('color', '#333');
					if ($(this).attr('value') == hintvalue) { $(this).attr('value', ''); }
				});
				$(this).blur(function() {
					if ($(this).attr('value') == "") { $(this).attr('value', hintvalue); $(this).css('color', '#777'); }
					if ($(this).attr('value') == hintvalue) { $(this).css('color', '#777'); }
				});
			});
		}

		jQuery.fn.inputhint2 = function () {
		  return this.each(function (){
				var hintvalue = $(this).attr('value');
				$(this).css('color', '#666');
				$(this).focus(function() {
					$(this).css('color', '#333');
					if ($(this).attr('value') == hintvalue) { $(this).attr('value', ''); }
				});
		   });
		}
        
// ================= 
// ! Form FirstName and SecondName Required   
// ================= 
        jQuery.fn.isValidateFirstNameAndSecondName = function () {
            return this.each(function (){
            
             var _value = $(this).attr('value');
             var pattern = new RegExp(/[A-Za-z0-9_\-\.]+[ \t]{1}[A-Za-z0-9_\-\.]/);      
                if (pattern.test(_value)) {
                     $(this).css("border-color", "#bbb").parent().find('.error').hide(); 
                }else
                {   
                    $(this).css("border-color", "#db0000").parent().find('.error').text("FirstName and SecondName are requred.").show();   
                }        
            });
        }
// ================= 
// ! Form Required   
// ================= 


		jQuery.fn.form_required = function () {
			return this.each(function (){
				if ($(this).attr('value') != "") { $(this).css("border-color", "#bbb").parent().find('.error').hide(); }
				else {$(this).css("border-color", "#db0000").parent().find('.error').show();	}  
			});
		}
		
// ================ 
// ! Verify Email   
// ================ 
				
		jQuery.fn.form_realemail = function () {
			return this.each(function (){
				if ($(this).attr('value') != "") { 
						error2 = 0;
						
						if ($(this).is("[value$='@hotmail.com']")) { error2 = 1; }
						if ($(this).is("[value$='@gmail.com']")) { error2 = 1; }
						if ($(this).is("[value$='@yahoo.com']")) { error2 = 1; }
						if ($(this).is("[value$='@msn.com']")) { error2 = 1; }
						if ($(this).is("[value$='@bellsouth.net']")) { error2 = 1; }
						if ($(this).is("[value$='@comcast.net']")) { error2 = 1; }
						if ($(this).is("[value$='@aol.com']")) { error2 = 1; }
						if ($(this).is("[value$='@earthlink.net']")) { error2 = 1; }
						if ($(this).is("[value$='@everymail.com']")) { error2 = 1; }
						if ($(this).is("[value$='@lycos.com']")) { error2 = 1; }
						if ($(this).is("[value$='@netzero.net']")) { error2 = 1; }
						if ($(this).is("[value$='@HOTMAIL.COM']")) { error2 = 1; }
						if ($(this).is("[value$='@GMAIL.COM']")) { error2 = 1; }
						if ($(this).is("[value$='@YAHOO.COM']")) { error2 = 1; }
						if ($(this).is("[value$='@MSN.COM']")) { error2 = 1; }
						if ($(this).is("[value$='@BELLSOUTH.NET']")) { error2 = 1; }
						if ($(this).is("[value$='@COMCAST.NET']")) { error2 = 1; }
						if ($(this).is("[value$='@AOL.COM']")) { error2 = 1; }
						if ($(this).is("[value$='@EARTHLINK.NET']")) { error2 = 1; }
						if ($(this).is("[value$='@EVERYMAIL.COM']")) { error2 = 1; }
						if ($(this).is("[value$='@LYCOS.COM']")) { error2 = 1; }
						if ($(this).is("[value$='@NETZERO.NET']")) { error2 = 1; }							
						if ($(this).is("[value*='@']")) { } else { error2 = 2; }
						if ($(this).is("[value*='.']")) { } else { error2 = 2; }
						
						if (error2 == "0") { $(this).css("border-color", "#bbb").parent().find('.error').hide(); }
						if (error2 == "1") { $(this).css("border-color", "#db0000").parent().find('.error').text("Company Emails Only.").show(); }
						if (error2 == "2") { $(this).css("border-color", "#db0000").parent().find('.error').text("Enter A Valid Email Address.").show(); }
				} 
				if ($(this).attr('value') == "") {	
					$(this).parent().find('.error').text("Email Address is Required.").show();
				}
			});
		}


		jQuery.fn.form_realemail2 = function () {
			return this.each(function (){
				if ($(this).attr('value') != "") { 
					error2 = 0;
					if ($(this).is("[value*='@']")) { } else { error2 = 1; }
					if ($(this).is("[value*='.']")) { } else { error2 = 1; }
					if (error2 == "0") { $(this).css("border-color", "#bbb").parent().find('.error').hide(); }
					if (error2 == "1") { $(this).css("border-color", "#db0000").parent().find('.error').show(); $(this).parent().find('.error').text("Enter A Valid Email Address."); }
				} else {	
					$(this).parent().find('.error').hide();
				}		  
			});
		}

// ==================== 
// ! User Interaction   
// ==================== 

	function userinteraction(name, email, phone, notes, siteAddr, userid, pageid, formid) {  
		$.post(siteAddr + "userinteraction", {name: ""+name+"", email: ""+email+"", phone: ""+phone+"", notes: ""+notes+"", userid: ""+userid+"", pageid: ""+pageid+"", formid: ""+formid+""}, function(data) {});
	}

// =============== 
// ! Live Search   
// =============== 

	function livesearch(searchQuery, siteAddr) {
		if (searchQuery.length == 0) {
			$('#livesearch').fadeOut();
		} else {
			$.post(siteAddr + "livesearch", {q: ""+searchQuery+""}, function(data) { 
				$('#livesearch').fadeIn();	         
				$('#livesearch').html(data);
	      	});
		}
	}

// ============= 
// ! Go to Tab   
// ============= 

	function gototab(tabTitle) {	
		tabnum = 1;
		$("ul.tabs li").each(function () {
			$(this).removeClass().addClass('tab' + tabnum);
			if ($(this).attr('title') == tabTitle) { 
				selected = tabnum; 
				$(this).removeClass().addClass('selected');	
			}
			tabnum++;
		});
		tabnum = 1;
		$('ul.content li.tab').each(function () {
			if (tabnum == selected) { $(this).show(); } 
			else { $(this).hide(); }
			tabnum++;
		});					
		return false;
	}

// =============== 
// ! FAQs Slider   
// =============== 
		
	function toggleFaqs() {
		if ($(this).hasClass('closed')) {
			$(this).removeClass().addClass('open')
			$(this).parent().find('.catlist').slideDown('fast');
		} else {
			$(this).removeClass().addClass('closed')
			$(this).parent().find('.catlist').slideUp('fast');
		}
		return false;
	}


// ================ 
// ! Timed Pop-Up   
// ================ 

	function timedPopUp(seconds, popupname) {
		var waittime = seconds * 1000;
		if (popupname == "popup/requestaquote") { setTimeout ("popup_quote()", waittime); }
		if (popupname == "popup/help") { setTimeout ("popup_help()", waittime); }
		if (popupname == "popup/chat") { setTimeout ("popup_chat()", waittime); }
	}


// ===================== 
// ! Pop-Up Quote Form   
// ===================== 


	function popup_help() {
		popUpLoad(base_url + 'popup/help');
		lightsOff();
		return false;		
	}
	function popup_chat() {
		popUpLoad(base_url + 'popup/chat');
		lightsOff();
		return false;		
	}

 
	function popup_quote() {
		popUpLoad(base_url + 'popup/requestaquote');
		lightsOff();
		return false;		
	}
			
	function x_popup_quote() {
		lightsOn();
		return false;
	}
	
	//$('#button5, #lightsoff').live('click', function() {
	$('#button5').live('click', function() {
		lightsOn();
		return false;
	});

	function lightsOff() {
		var pageHeight = $(document).height();
		if ($('#lightsoff').length == 0) {
			$('body').prepend('<div id="lightsoff"></div>');
			$('#lightsoff').css({opacity: 0.8}).css('height', '' + pageHeight + 'px');
		}
	}
	
	function lightsOn() {
		$('#lightsoff').remove();
		$('#popup-module').hide();
		return false;
	}
	
	function popUpLoad(url) {
		if ($('#popup-module').length == 0) {
			$('body').prepend('<div id="popup-module"></div>');
		} 
		if ($('#popup-module').is(":hidden")) {
			if ($('#popup-module').data('url') != url) { 
				$('#popup-module').load(url, function() {
					$('#popup-module').show();
					pageload();
				});
				$('#popup-module').data('url', url);
			} else { $('#popup-module').show(); }
		}
	}
	

// ================== 
// ! Highlight Name   
// ================== 
	
	function highlight_name() {	
		$("#quote_name").focus(); 
		return false; 
	}


 
	/*
	 * jQuery ifixpng plugin
	 * (previously known as pngfix)
	 * Version 3.1.2  (2008/09/01)
	*/	 
		 
	;(function($) {
	
		/**
		 * helper variables and function
		 */
		$.ifixpng = function(customPixel) {
			$.ifixpng.pixel = customPixel;
		};
		
		$.ifixpng.regexp = {
			bg: /^url\(["']?(.*\.png([?].*)?)["']?\)$/i,
			img: /.*\.png([?].*)?$/i
		},
		
		$.ifixpng.getPixel = function() {
			return $.ifixpng.pixel || 'http://www.reliant-technology.com/img/pixel.gif';
		};
		
		var hack = {
			base	: $('base').attr('href'),
			ltie7	: $.browser.msie && $.browser.version < 7,
			filter	: function(src) {
				return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='"+src+"')";
			}
		};
		
		/**
		 * Applies ie png hack to selected dom elements
		 *
		 * $('img[@src$=.png]').ifixpng();
		 * @desc apply hack to all images with png extensions
		 *
		 * $('#panel, img[@src$=.png]').ifixpng();
		 * @desc apply hack to element #panel and all images with png extensions
		 *
		 * @name ifixpng
		 */
		 
		$.fn.ifixpng = hack.ltie7 ? function() {
			function fixImage(image, source, width, height, hidden) {
				image.css({filter:hack.filter(source), width: width, height: height})
				  .attr({src:$.ifixpng.getPixel()})
				  .positionFix();
			}
			
	    	return this.each(function() {
				var $$ = $(this);
				if ($$.is('img') || $$.is('input')) { // hack image tags present in dom
					var source, img;
					if (this.src && this.src.match($.ifixpng.regexp.img)) { // make sure it is png image
						// use source tag value if set 
						source = (hack.base && this.src.substring(0,1)!='/' && this.src.indexOf(hack.base) === -1) ? hack.base + this.src : this.src;
						// If the width is not set, we have a problem; the image is not probably visible or not loaded
						// and we need a work around.
						if (!this.width || !this.height) {
							$(new Image()).one('load', function() {
								fixImage($$, source, this.width, this.height);
								$(this).remove();
							}).attr('src', source);
						// If the image already has dimensions (it's loaded and visible) we can fix it straight away.
						} else fixImage($$, source, this.width, this.height);
					}
				} else if (this.style) { // hack png css properties present inside css
					var imageSrc = $$.css('backgroundImage');
					// Background repeated images we cannot fix unfortunately
					if (imageSrc && imageSrc.match($.ifixpng.regexp.bg) && this.currentStyle.backgroundRepeat == 'no-repeat') {
						imageSrc = RegExp.$1;
						var x = this.currentStyle.backgroundPositionX || 0, y = this.currentStyle.backgroundPositionY || 0;
						if (x || y) {
							var css = {}, img;
							if (typeof x != 'undefined') {
								if (x == 'left') css.left = 0; 
								// if right is 0, we have to check if the parent has an odd width, because of an IE bug
								else if (x == 'right') css.right = $$.width() % 2 === 1 ? -1 : 0;
								else css.left = x;
							}
							if (typeof y != 'undefined') {
								// if bottom is 0, we have to check if the parent has an odd height, because of an IE bug
								if (y == 'bottom') css.bottom = $$.height() % 2 === 1 ? -1 : 0; 
								else if (y == 'top') css.top = 0;
								else css.top = y;
							}
							img = new Image();
							$(img).one('load', function() {
								var x,y, expr = {}, prop;
								// Now the image is loaded for sure, we can see if the background position needs fixing with an expression (in case of percentages)
								if (/center|%/.test(css.top)) {
									expr.top = "(this.parentNode.offsetHeight - this.offsetHeight) * " + (css.top == 'center' ? 0.5 : (parseInt(css.top) / 100));
									delete css.top;
								}
								if (/center|%/.test(css.left)) {
									expr.left = "(this.parentNode.offsetWidth - this.offsetWidth) * " + (css.left == 'center' ? 0.5 : (parseInt(css.left) / 100));
									delete css.left;
								}
								// Let's add the helper DIV which will simulate the background image
								$$.positionFix().css({backgroundImage: 'none'}).prepend(
									$('<div></div>').css(css).css({
										width: this.width,
										height: this.height,
										position: 'absolute',
										filter: hack.filter(imageSrc)
									})
								);
								if (expr.top || expr.left) {
									var elem = $$.children(':first')[0];
									for (prop in expr) elem.style.setExpression(prop, expr[prop], 'JavaScript');
								}
								$(this).remove();
							});
							img.src = imageSrc;
						} else {
							$$.css({backgroundImage: 'none', filter:hack.filter(imageSrc)});
						}
					}
				}
			});
		} : function() { return this; };
		
		/**
		 * positions selected item relatively
		 */
		$.fn.positionFix = function() {
			return this.each(function() {
				var $$ = $(this);
				if ($$.css('position') != 'absolute') $$.css({position:'relative'});
			});
		};
	
	})(jQuery);
	/* End ifixpng plugin */
