var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
$(document).ready(function(){
	//Initialize page
	add_ready();
	
	//Apply PNG fix
	if ($.browser.msie && $.browser.version <= 6) {

		//fix images with png-source
		$('body img[src$=".png"]').each(function() {
			
			$(this).attr('width',$(this).width());
			$(this).attr('height',$(this).height());

			var prevStyle = '';
			var strNewHTML = '';
			var imgId = ($(this).attr('id')) ? 'id="' + $(this).attr('id') + '" ' : '';
			var imgClass = ($(this).attr('class')) ? 'class="' + $(this).attr('class') + '" ' : '';
			var imgTitle = ($(this).attr('title')) ? 'title="' + $(this).attr('title') + '" ' : '';
			var imgAlt = ($(this).attr('alt')) ? 'alt="' + $(this).attr('alt') + '" ' : '';
			var imgAlign = ($(this).attr('align')) ? 'float:' + $(this).attr('align') + ';' : '';
			var imgHand = ($(this).parent().attr('href')) ? 'cursor:hand;' : '';
			if (this.style.border) {
				prevStyle += 'border:'+this.style.border+';';
				this.style.border = '';
			}
			if (this.style.padding) {
				prevStyle += 'padding:'+this.style.padding+';';
				this.style.padding = '';
			}
			if (this.style.margin) {
				prevStyle += 'margin:'+this.style.margin+';';
				this.style.margin = '';
			}
			var imgStyle = (this.style.cssText);

			strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt;
			strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;'+imgAlign+imgHand;
			strNewHTML += 'width:' + $(this).width() + 'px;' + 'height:' + $(this).height() + 'px;';
			strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + $(this).attr('src') + '\', sizingMethod=\'scale\');';
			strNewHTML += imgStyle+'"></span>';
			if (prevStyle != ''){
				strNewHTML = '<span style="position:relative;display:inline-block;'+prevStyle+imgHand+'width:' + $(this).width() + 'px;' + 'height:' + $(this).height() + 'px;'+'">' + strNewHTML + '</span>';
			}

			$(this).hide();
			$(this).after(strNewHTML);

		});

		// fix css background pngs
		$('body *').each(function(){
			var bgIMG = $(this).css('background-image');
			if(bgIMG.indexOf(".png")!=-1){
				var iebg = bgIMG.split('url("')[1].split('")')[0];
				$(this).css('background-image', 'none');
				$(this).get(0).style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='crop')";
			}
		});
		
		//fix input with png-source
		$('body input[src$=".png"]').each(function() {
			var bgIMG = $(this).attr('src');
			$(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'crop\');';
   			$(this).attr('src', '/blank.gif')
		});	
	}
	
});

function add_ready(){
	//Get a quote box
	var $quote = $('.quote');
	var $quoteBtn = $('.quoteForm a:first');
	var quoteBtnW = 88;
	var quoteW = $quote.outerWidth();
	var quoteH = $quote.outerHeight();
	
	if ($.browser.msie && $.browser.version <= 8){
		$quote.css({'position': 'absolute'});
		$(window).scroll(function(){
			var sclTop = $(window).scrollTop();
			$quote.css({top: sclTop+'px'});
		});
	}else{
		$quote.css({'position': 'fixed'});
	}
	
	$quote
	.show()
	.css({'left': 0, 'top':0, 'overflow':'hidden', 'z-index': 1500, 'margin-left': -(quoteW-quoteBtnW)})
	.data('state', 0);
	
	$quoteBtn.click(function(){
		$quote.find('input, textarea').val('');
		switch($quote.data('state')){
			case 0:
				$quote
				.animate({'margin-left': 0}, {queue: false})
				.data('state', 1)
				.removeClass('thanks');
				$('#get_quote').show().next('div').hide();
			break;
			case 1:
				$quote
				.animate({'margin-left': -(quoteW-quoteBtnW)}, {queue: false})
				.data('state', 0);	
			break;
		}
		return false;
	});
	
	//Set current menu
	var currM = $('#curr-menu').val();
	$('.navContainer a:eq('+currM+')').attr('class', $('.navContainer a:eq('+currM+')').attr('class')+'Active');
	
	$(document+':not(.quote)').click(function(e){
		var mX = e.pageX;
		var mY = e.pageY;
		var $quoteOff = $quote.offset();
		var quoteT = $quoteOff.top;
		var quoteL = $quoteOff.left;
		var quoteB = quoteT+$quote.outerHeight();
		var quoteR = quoteL+$quote.outerWidth();
		
		if (mX > quoteR){
			$quote
			.animate({'margin-left': -(quoteW-quoteBtnW)}, {queue: false})
			.data('state', 0);
		}else if(mY > quoteB){
			$quote
			.animate({'margin-left': -(quoteW-quoteBtnW)}, {queue: false})
			.data('state', 0);
		}
		
	});
	
	//Get a quote form
	$('#get_quote').submit(function(){
		var error = 0;
		var name = $('#name');
		var nameErr = name.next('span').html();
		var number = $('#number');
		var numberErr = number.next('span').html();
		var email = $('#email');
		var emailErr1 = email.next('span').html();
		var emailErr2 = email.nextAll('span:eq(1)').html();
		
		if (name.val() == '' || name.val() == null || name.val() == nameErr){
			name.val(nameErr);
			name.addClass('fError');
			error = 1;				
		}else{
			name.removeClass('fError');
		}
		
		if (number.val() == '' || number.val() == null || number.val() == numberErr){
			number.val(numberErr);
			number.addClass('fError');
			error = 1;				
		}else{
			number.removeClass('fError');
		}
		
		if (email.val() == '' || email.val() == null || email.val() == emailErr1){
			email.val(emailErr1);
			email.addClass('fError');
			error = 1;				
		}else if(!pattern.test( email.val() )){
			email.val(emailErr2);
			email.addClass('fError');
			error = 1;		
		}else{
			email.removeClass('fError');
		}
		
		name.bind('keydown keyup focus click', function(){
			var currVal = name.val();
			if (currVal == nameErr){
				name
				.val('')
				.removeClass('fError');
			}
		});
		
		number.bind('keydown keyup focus click', function(){
			var currVal = number.val();
			if (currVal == numberErr){
				number
				.val('')
				.removeClass('fError');
			}
		});
		
		email.bind('keydown keyup focus click', function(){
			var currVal = email.val();
			if (currVal == emailErr1 || currVal == emailErr2){
				email
				.val('')
				.removeClass('fError');
			}
		});
		
		if (error == 0){
			$.ajax({
			  url: "/ajax/",
			  type: "POST",
			  data: "action=contForm&ref="+$('#refff').val()+"&"+$('#get_quote').serialize(),
			  success: function(data){
				if (data == 1){
					$quote.addClass('thanks');
					$('#get_quote').hide().next('div').fadeIn();					
				}else{
					alert("Something went wrong while submitting your quote, Please try again later.");
				}
			  }
			})
		}
		return false;
	});
	
	//Validate contact form
	$('#conForm').submit(function(){
		var error = 0;
		var name = $(this).find('input[type=text]:eq(0)');
		var number = $(this).find('input[type=text]:eq(2)');
		var email = $(this).find('input[type=text]:eq(3)');
		
		if (name.val() == '' || name.val() == null){
			name.addClass('fError1');
			error = 1;
		}else{
			name.removeClass('fError1');
		}
		
		if (number.val() == '' || number.val() == null){
			number.addClass('fError1');
			error = 1;
		}else{
			number.removeClass('fError1');
		}
		
		if (email.val() == '' || email.val() == null){
			email.addClass('fError1');
			error = 1;
		}else if(!pattern.test( email.val() )){
			email.addClass('fError1');
			error = 1;		
		}else{
			email.removeClass('fError1');
		}
		
		if (error){
			return false;
		}
	});
	
	//Dropdown menu
	$('.navContainer a[rel=dropdown]').each(function(){
		var $this = $(this);
		var ind = $('.navContainer a[rel=dropdown]').index($this);
		var $dropdown = $('.dropdowns-all > .dropdownbox:eq('+ind+')');
		var Timer;
		var mL = -3;
		var mT = -18;
		var iniW = 0;
		var topShadeWini = 0;
		var iefix = false;
		if ($.browser.msie && $.browser.version <= 8){
			iefix = true;
		}
		
		//Top level
		$this.hover(function(){
			$('.dropdowns-all > .dropdownbox').not($this).hide();
			clearTimeout(Timer);
			var thisOff = $this.position();
			var thisL = thisOff.left;
			var thisT = thisOff.top;
			$dropdown.css({'position': 'absolute', 'left': (thisL+mL)+'px', 'top': (thisT+mT)+'px'});
			if (iefix){
				$dropdown.show();
			}else{
				$dropdown.fadeIn();
			}
			
			iniW = $dropdown.find('.dropContentBox').outerWidth();
			topShadeWini = $dropdown.find('.topShade').outerWidth();
			
			//Make other item on top of this menu
			var navW = $('.navContainer').width();
			var navH = $('.navContainer').height();
			$('.navContainer').css({width: navW, height: navH});
			$('.navContainer > a').each(function(){
				var $obj = $(this);
				var currOff = $obj.position();
				var currT = currOff.top;
				var currL = currOff.left;
				
				if ($obj.attr('class') == $this.attr('class')){
					$obj.css({left: currL, top: currT, 'z-index': 4050});
				}else{
					$obj.css({left: currL, top: currT, 'z-index': 5050});
				}
			});
			$('.navContainer > a').css({position: 'absolute'})
			
		}, function(){
			Timer = setTimeout(function(){
				if (iefix){
					$dropdown.hide();
				}else{
					$dropdown.fadeOut();
				}
			}, 500);
		});
		
		$dropdown.hover(function(){
			clearTimeout(Timer);
		}, function(){
			Timer = setTimeout(function(){
				if (iefix){
					$dropdown.hide();
				}else{
					$dropdown.fadeOut();
				}
			}, 500);
		});
		
		//Sub level
		var Timer1;
		$dropdown.find('a[class=arrowclass]').each(function(){
			var $obj = $(this);
			var $submenu = $obj.next('.leftSubnav');
			$obj.hover(function(){
				var currW = $submenu.outerWidth();
				var textW = $dropdown.find('div:eq(0) > a:eq(0)').outerWidth();
				var topShadeW = 421-(textW+34);//421
				//Hide all subs
				$dropdown.find('.arrowclass')
					.removeClass('curr')
					.next('.leftSubnav').hide();
				var intg = ($.browser.msie) ? 357 : 365;
				var intg1 = ($.browser.msie) ? 175 : 180;
				//Cleartimeout
				clearTimeout(Timer1);
				$obj.addClass('curr');
				$dropdown.find('.topShade').animate({'width': topShadeW}, {queue: false});
				$dropdown.find('.dropContent').width(445);
				$dropdown.find('.dropContent').prev('div').width(445);
				$dropdown.find('.dropContentBox').animate({'width': intg}, {queue:false, complete: function(){//365
					$submenu.css({position: 'absolute', 'left': intg1, 'top': 0}).show();
				}});
			},function(){
				//Stop animation
				$dropdown.find('.dropContentBox').stop();
				$dropdown.find('.topShade').stop();
				Timer1 = setTimeout(function(){
					//Hide all subs
					$dropdown.find('.arrowclass').next('.leftSubnav').hide();
					$dropdown.find('.dropContentBox').animate({'width': iniW}, {queue:false});
					$dropdown.find('.topShade').animate({'width': topShadeWini});
					$obj.removeClass('curr');
				}, 400);
			});
		
			$submenu.hover(function(){
				//Cleartimeout
				clearTimeout(Timer1);
			}, function(){
				Timer1 = setTimeout(function(){
					$submenu.hide();
					$dropdown.find('.dropContentBox').animate({'width': iniW}, {queue:false});
					$dropdown.find('.topShade').animate({'width': topShadeWini});
				}, 400);
				$obj.removeClass('curr');
			});
		});
	});	
}
