// JavaScript Document

// AJAX INIT STUFF -- START
var xmlhttp=null;

try
{
	xmlhttp = new XMLHttpRequest();
}
catch (error)
{
	try
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (error)
	{
		xmlhttp = null;
	}
}
//  END

var navBkgStart = -999;
var subNavBkgStart = -999;
var theTimer;
var windowHeight = $(window).height();
var activeSubmenu = '';

function followMouse(){
	$('#navigation').mousemove(function(e){
		var x = e.pageX - this.offsetLeft - 10;
		$('#test').html("X Axis : " + x + " | Y Axis " + e.pageY);
		$('#navigation').css({'background-position' : x + 'px 25px'});
	});
	
	$('#navigation').mouseout(function(){
		$('#navigation').css({'background-position' : navBkgStart + 'px 25px'});
	});
		
	$('#navigation a').click(function(e){
		navBkgStart = e.pageX - document.getElementById('navigation').offsetLeft - 10;
	});
	
	//Sub-navigation
	$('#sub-navigation').mousemove(function(g){
		var x2 = g.pageX - this.offsetLeft - 10;
		$('#test').html("X Axis : " + x2 + " | Y Axis " + g.pageY);
		$('#sub-navigation').css({'background-position' : x2 + 'px 19px'});
	});
	
	$('#sub-navigation').mouseout(function(){
		$('#sub-navigation').css({'background-position' : subNavBkgStart + 'px 19px'});
	});
		
	$('#sub-navigation a').click(function(h){
		subNavBkgStart = h.pageX - document.getElementById('sub-navigation').offsetLeft - 10;
	});
}

function hideMenu(whichMenu){
	//alert(whichMenu);
	divtohide=document.getElementById(whichMenu);
	divtohide.style.display = 'none';
	if(activeSubmenu != '') $(activeSubmenu).css('display','block');
}

function hideAllMenus(doNotHideMenu){
	document.getElementById('residential-doors').style.display = 'none';
	document.getElementById('commercial-doors').style.display = 'none';
	document.getElementById('support').style.display = 'none';
	document.getElementById('contact-us').style.display = 'none';
}

function changeBack(whichMenu){
	theTimer = setTimeout("hideMenu('"+whichMenu+"')", 500);
} 

function showMenu(whichMenu){	
	hideAllMenus(whichMenu);
	clearTimeout(theTimer);
	document.getElementById(whichMenu).style.display = 'block';
}

function submenus(){
	$("#navigation a").hover(
		function () { //alert('fire');
			if($(this).attr("class") != 'about-us')
				showMenu($(this).attr("class"));
		}, 
		function () {
			if($(this).attr("class") != 'about-us')
				changeBack($(this).attr("class"));
		}
	);
	
	$("#sub-navigation-container div").hover(
		function () { //alert('fire');
			if($(this).attr("id") != 'about-us')
				showMenu($(this).attr("id"));
		}, 
		function () {
			if($(this).attr("id") != 'about-us')
				changeBack($(this).attr("id"));
		}
	);
}

function hSlide(thisATag, whichContainer, whichSet){
	$('#pagination-' + whichContainer + ' a').html('<img src="/images-new/overview/dot-off.gif" width="17" height="15" alt="Off" />');
	$(thisATag).html('<img src="/images-new/overview/dot-on.gif" width="17" height="15" alt="On" />');

	var moveHere = (whichSet - 1) * -380;
	$("#windowset" + whichContainer).animate({ 
		left: moveHere + 'px'
	}, 500 );
}

function vSlide(thisH5, whichSet, whichGroup){
	$('#option-set-' + whichSet + ' h5').css('background-image','url(/images-new/overview/minus.jpg)');
	$(thisH5).css('background-image','url(/images-new/overview/plus.jpg)');
	
	switch(whichGroup){
		case 'panel':
			$('#option-set-' + whichSet + ' div.color-container').slideUp('medium');
			$('#option-set-' + whichSet + ' div.window-container').slideUp('medium');
			$('#option-set-' + whichSet + ' div.panel-container').slideDown('medium');
			break;
		case 'color':
			$('#option-set-' + whichSet + ' div.panel-container').slideUp('medium');
			$('#option-set-' + whichSet + ' div.window-container').slideUp('medium');
			$('#option-set-' + whichSet + ' div.color-container').slideDown('medium');
			break;
		case 'window':
			$('#option-set-' + whichSet + ' div.color-container').slideUp('medium');
			$('#option-set-' + whichSet + ' div.panel-container').slideUp('medium');
			$('#option-set-' + whichSet + ' div.window-container').slideDown('medium');
			break;
	}
}

function sizechooser(){
	$('a.go-size-chooser').click(function () {
		$('html, body').animate({
			scrollTop: $("#size-chooser").offset().top
		}, 650);
		var relModel = $(this).attr('rel');
		$('#chosenDoor').attr('value', relModel);
	});
}

function showLoading(){
	$('div.gray-out').css("height",windowHeight + 'px','visibility','visible');
	$('div.gray-out img').css("padding-top", (windowHeight / 2) + 'px');
	$('div.gray-out').css('visibility','visible');
	$('div.gray-out').css('display','block');
}

function closeLoading(scrollDown){
	$('div.gray-out').css('visibility','hidden');
	$('div.gray-out').css('display','none');
	if(scrollDown){
		if($('div.oops').length > 0){
			var scrollTopWhere = $("div.oops").offset().top;
		}else{
			var scrollTopWhere = $("#overview-doors").offset().top;
		}
		$('html, body').animate({
			scrollTop: scrollTopWhere
		}, 650, function (){
			$('#header').prepend('<div id="more-below"><img src="/images-new/more-below.png" width="230" height="149" alt="More Garage Doors Below" /></div>');
			var windowBottom = $(window).scrollTop() + $(window).height() - 150; //alert($(window).height());
			$('#more-below').css('top',windowBottom);
			removeMoreBelow($(window).scrollTop());
		});
	}
}

function getDoors(){ //alert('fire');
	$('#submit-span').html('<input name="Submit" type="button" class="top_btn" value="Submit" id="submit-button" />');
	$('#submit-button').click( function(){
		progressBar();
		var vars = $('#size-form').serialize(); //return false;
		showLoading();
		xmlhttp.open("GET", "/ajaxHandler.php?action=getDoors&" + vars + "&time=" + new Date().getTime());
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
				var overviewDoors = xmlhttp.responseText;
				if(overviewDoors == 'false'){
					$('#size-chooser div.text').css('display','none');
					$('#size-chooser div.form').css('display','none');
					$('#size-chooser #no-doors-avail').css('display','block');
					document.getElementById('overview-doors').innerHTML = '<div><img src="/images-new/overview/notAvail.jpg" width="931" height="648" alt="Not Door Available" /></div>';
					document.getElementById('no-doors-avail').innerHTML = '<p><strong class="font18 red"><br /> <br />Sorry, there are no doors available in this size.</strong><br /> <strong>You have two options:</strong> </p> <ul> <li><a onclick="goback()" style="cursor:pointer">Go back</a> and enter a different door style or  size.</li> <li>Contact us regarding a possible <a href="/specialorder.php">Special Order Door</a> in this size or near size</li> </ul>';
					setTimeout('closeLoading(false)', 650);
				}else{
					document.getElementById('overview-doors').innerHTML = overviewDoors;
					setTimeout('closeLoading(true)', 650);
					window.location.hash = vars;
					var chosenDoor = $('#chosenDoor').attr('value');
					if(chosenDoor != ''){ //alert(chosenDoor);
						if($('#' + chosenDoor).length > 0){
							window.location = $('#' + chosenDoor).attr('href') + '&IE_js=true';
						}else{
							$('div.oops').html('<img src="/images-new/common/oops.gif" width="936" height="58" alt="The model you’ve chosen is not available in your door size. However, the doors below are available..." />');
						}
					}
				}
				viewGallery();
				return false;
			}
		}
		xmlhttp.send(null);
	});
}

function setArrows(){
	var resMenu = new Array();
	var comMenu = new Array();
	var support = new Array();
	var contact = new Array();
	
	$("#residential-doors a").each(function() {
		var thisHref = $(this).attr('href');
		resMenu.push(thisHref);
	});	
	$("#commercial-doors a").each(function() {
		var thisHref = $(this).attr('href');
		comMenu.push(thisHref);
	});	
	$("#support a").each(function() {
		var thisHref = $(this).attr('href');
		support.push(thisHref);
	});	
	$("#contact-us a").each(function() {
		var thisHref = $(this).attr('href');
		contact.push(thisHref);
	});

	var currentURL = location.href; //alert(currentURL);
	var urlParts = currentURL.split('/');
	var arrayLength = urlParts.length;
	
	if(arrayLength > 4){
		var filename = new Array();
		var lastPart = urlParts[(arrayLength - 1)];
		filename = lastPart.split('#');
		var submenuLink = '/' + urlParts[(arrayLength - 2)] + '/' + filename[0]; //alert(submenuLink);
		
		if(jQuery.inArray( submenuLink, resMenu ) > -1){
			$('#residential-doors').css('display','block');
			activeSubmenu = '#residential-doors';
		}
		if(jQuery.inArray( submenuLink, comMenu ) > -1){
			$('#commercial-doors').css('display','block');
			activeSubmenu = '#commercial-doors';
		}
		if(jQuery.inArray( submenuLink, support ) > -1){
			$('#support').css('display','block');
			activeSubmenu = '#support';
		}
		if(jQuery.inArray( submenuLink, contact ) > -1){
			$('#contact-us').css('display','block');
			activeSubmenu = '#contact-us';
		}
	}
}

function homebutton(){
	$('#header h1 span.dol').click(function (){
		window.location = '/';
	});
}
function viewGallery(){
	$('a.viewgallery').click(function (){
		var href = $(this).attr('href');
		PicLensLite.start({feedUrl:href});
		return false;
	});
	$('a.view-gallery').click(function (){
		var href = $(this).attr('href');
		PicLensLite.start({feedUrl:href});
		return false;
	});
}

function goback(){
	$('#size-chooser div.text').css('display','block');
	$('#size-chooser div.form').css('display','block');
	$('#no-doors-avail').css('display','none');
}

function setDoorType(thisValue){
	document.getElementById('doorType').value = thisValue;
}

function resetSelect(id){
	if(id == 'windRatingCTY'){
		document.getElementById(id).value = 'CHS_CNTY';
	}else{
		document.getElementById(id).value = 'CHS_WND';
	}
}

function setDefault(){
	var locationHash = window.location.hash;
	if(locationHash != '' && locationHash.length > 25){
		progressBar();
		var vars = window.location.hash.substr(1);
		var varsArray = vars.split('&'); //alert(varsArray);
		for(x in varsArray){
			var parts = varsArray[x].split('='); //alert(parts[0]+"']"+parts[1]);
			switch(parts[0]){
				case 'doorWidth':
					$("select[name='"+parts[0]+"']").val(parts[1]);
					break;
				case 'doorHeight':
					$("select[name='"+parts[0]+"']").val(parts[1]);
					break;
				case 'windRatingCTY':
					$("select[name='"+parts[0]+"']").val(parts[1]);
					break;
				case 'windRating':
					$("select[name='"+parts[0]+"']").val(parts[1]);
					break;
			}
			if(parts[1] == 'yes'){
				document.getElementById('radio1').checked = true;
				$('#windRatingError').css('display','block');
			}
		}
		
		showLoading();
		xmlhttp.open("GET", "/ajaxHandler.php?action=getDoors&" + vars + "&time=" + new Date().getTime());
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
				var overviewDoors = xmlhttp.responseText;
				if(overviewDoors == 'false'){
					$('#size-chooser div.text').css('display','none');
					$('#size-chooser div.form').css('display','none');
					$('#size-chooser #no-doors-avail').css('display','block');
					document.getElementById('overview-doors').innerHTML = '<div><img src="/images-new/overview/notAvail.jpg" width="931" height="648" alt="Not Door Available" /></div>';
					document.getElementById('no-doors-avail').innerHTML = '<p><strong class="font18 red"><br /> <br />Sorry, there are no doors available in this size.</strong><br /> <strong>You have two options:</strong> </p> <ul> <li><a onclick="goback()" style="cursor:pointer">Go back</a> and enter a different door style or  size.</li> <li>Contact us regarding a possible <a href="/specialorder.php">Special Order Door</a> in this size or near size</li> </ul>';
					setTimeout('closeLoading(false)', 650);
				}else{
					document.getElementById('overview-doors').innerHTML = overviewDoors;
					setTimeout('closeLoading(true)', 650);
				}
				viewGallery();
				return false;
			}
		}
		xmlhttp.send(null);
	}
}

function removeMoreBelow(windowPosition){
	windowPosition = windowPosition + 10;
	$(window).scroll(function () { //alert($(window).scrollTop() +  '>' +  windowPosition);
		if($(window).scrollTop() > windowPosition){
			$('#more-below').fadeOut('slow', function (){
				$('#more-below').remove();
			});
		}
	});
}

function progressBar(){ //alert('fire');
	$('div.progress-bar span').each( function(index) {
		$(this).removeClass('on');
		if(index == 1) $(this).addClass('on');
	});
}

function noLock(){
	var windowLocation = window.location.href;
	$('input:checkbox').click( function (){
		if(windowLocation.indexOf('commercialPage.php') > -1){
			if($(this).attr('name') == 'lockOptions[]' && $(this).is(':checked') && $(this).val() != 1){
				$('input:checkbox').each( function (){
					if($(this).attr('name') == 'lockOptions[]' && $(this).val() == 1){
						$(this).attr('checked', false);
					}
				});
			}
		}else{
			if($(this).attr('name') == 'lockOptions[]' && $(this).is(':checked') && $(this).val() != 4){
				$('input:checkbox').each( function (){
					if($(this).attr('name') == 'lockOptions[]' && $(this).val() == 4){
						$(this).attr('checked', false);
					}
				});
			}
		}
	});
}

function checkVinyl(isChecked){ //alert(isChecked);
	if(isChecked){
		document.getElementById('addl_options12').checked = true;
	}
}

function noEver(thisChecked, thisValue){
	if(thisChecked){
		document.pageForm.submit();
	}
}

$(document).ready(function() { 
	$('#chosenDoor').val('');
	homebutton();
	followMouse();
	submenus();
	sizechooser();
	getDoors();
	setArrows();
	viewGallery();
	setDefault();
	noLock();
});
