$(document).ready(function(){

	
	
		// SCRIPT HANDLING THE MAIN NAVIGATION BUTTONS
		// *******************************************
		
		// add active class to home href initially
		$("#nav ul li#hom a").addClass('active');
		
		// the main horizontal navigation
		$("#nav ul li a").click(function(){
																		 
				// remove any active class from href
				$("#nav ul li a").removeClass('active');
				
				// add active class to clicked href
				$(this).addClass('active');
				
				// hide all the panels
				$("div.panel").css("display","none");
				
				
				// get the id  of the parent list item holding the href
				var pageId = $(this).parent().attr("id");
				
				iecatch = $.browser.msie;
				//alert(iecatch);
				
				if (!iecatch) {
					
					// show the panel corresponding to nav click
					if (pageId == "hom") {
							$("body").removeClass('bigger');
							$("div.home").animate({opacity: "show"});
							/*$("#page").animate({paddingBottom: '0px'});*/
					}
					if (pageId == "key") {
							$("body").removeClass('bigger');
							$("div.key").animate({opacity: "show"});
					}
					if (pageId == "opt") {
							$("body").removeClass('bigger');
							$("div.compare").animate({opacity: "show"});
					}
					if (pageId == "off") {
						$("body").addClass('bigger');
						$("div.complementary").animate({opacity: "show"});
							
					}
					
				} else {
					
					if (pageId == "hom") {
							$("body").removeClass('bigger');
							$("div.home").show();
					}
					if (pageId == "key") {
							$("body").removeClass('bigger');
							$("div.key").show();
					}
					if (pageId == "opt") {
							$("body").removeClass('bigger');
							$("div.compare").show();
					}
					if (pageId == "off") {
							$("body").addClass('bigger');
							$("div.complementary").show();
					}
				}
				
		});
		
		
		// SCRIPT HANDLING THE NEXT AND BACK BUTTONS
		// *****************************************
		$("div.panel a").click(function(){
																		
				// remove any active class from href
				$("#nav ul li a").removeClass('active');
				
				
				// get the class of the href to use as a page id
				var pageId = $(this).attr("class");
				//alert(pageId);
				
				// add active class to clicked href
				if (pageId == "back hom") {
					$("#nav ul li#hom a").addClass('active');
				}
				if (pageId == "next key" || pageId == "back key") {
					$("#nav ul li#key a").addClass('active');
				}
				if (pageId == "next opt" || pageId == "back opt") {
					$("#nav ul li#opt a").addClass('active');
				}
				if (pageId == "next off") {
					$("#nav ul li#off a").addClass('active');
				}
				
				
				// hide all panels on click
				$("div.panel").css("display","none");
				
				
				//show page imediately if IE6 otherwise do nice opacity thing
				iecatch = $.browser.msie;
				
				 if (!iecatch) {
					
							// not ie6 so do nicer transition
							if (pageId == "back hom") {
								$("div.home").animate({opacity: "show"});
							}
							if (pageId == "next key" || pageId == "back key") {
									$("div.key").animate({opacity: "show"});
							}
							if (pageId == "next opt" || pageId == "back opt") {
									$("div.compare").animate({opacity: "show"});
							}
							if (pageId == "next off") {
									$("div.complementary").animate({opacity: "show"});
							}
					
				 } else {
						
							// crappy IE, just show the page
							if (pageId == "back hom") {
								$("div.home").show();
							}
							if (pageId == "next key" || pageId == "back key") {
									$("div.key").show();
							}
							if (pageId == "next opt" || pageId == "back opt") {
									$("div.compare").show();
							}
							if (pageId == "next off") {
									$("div.complementary").show();
							}
					
				 }
		});											
		
		
		
		
		// SCRIPT FOR HANDLING OF FORM ELEMENTS AFTER VALIDATION
		// *****************************************************
		$("#complementary-offer ul li input#submit").click(function(){
																																
			// get browser version
			iecatch = $.browser.msie;																													
		
			var formSubmit = $("#compForm");
			formSubmit.validation();
			// submit form info via ajax if form has validated
			if(formSubmit.validate()) {
								
					var title = $("input#title").val();
					var firstName = $("input#firstName").val();
					var surname = $("input#surname").val();
					var company = $("input#company").val();
					var phone = $("input#phone").val();
					var email = $("input#email").val();
						
					var dataString = 'title='+ title + '&firstName=' + firstName + '&surname=' + surname + '&company=' + company + '&phone=' + phone + '&email=' + email;
		
					$.ajax({
							type: "POST",
							url: "mailer.php",
							data: dataString,
							success: function(){
							
								if (!iecatch) {
									$('.success').fadeIn(200).show();
									$('.errorform').fadeOut(200).hide();
								} else {
									$('.success').show();
									$('.errorform').hide();	
								}
								
							}
					});
				} else {
				$('.errorform').fadeOut(200).show();
					
				}
		
			return false;
		
			});
		
		
			// INPUT IMAGE ROLLOVER SCRIPT
			// ***************************
			var navElements = $(".inp_rollover");
			navElements.each(function(i){
						$(navElements[i]).hover(
					 function() {
						var sel = $(this).attr("class");  
						curr = $(this).attr("src");
					if(sel != "selected"){
						if(curr.indexOf("_over.gif")!=-1) return;
						overlen = curr.length;
						over = curr.substr(0, overlen-4);
						over = over+'_over.gif';
						$(this).attr({ src: over});
					}
					 },
					 function() {
						var sel = $(this).attr("class");
						if(sel != "selected"){
						$(this).attr({ src: curr});
					}
					 }
					)
		
					$(navElements[i]).each(function(i) {
						temp = this.src;
						prelen = temp.length;
						pre = temp.substr(0, prelen-4);
						pre = pre+'_over.gif';
						preload_image_object = new Image();
						preload_image_object.src = pre;
					});
				});//end each
			
			
			
			// inner links on homepage  
			$("a.comp-trial").click(function(){
				
				$("div.panel").css("display","none");
				
				iecatch = $.browser.msie;
				
				if (!iecatch) {
					
					$("div.complementary").animate({opacity: "show"});
					
				} else {
					
						$("div.complementary").show();
				}
				
				return false;
																			 
			});

		
		
		
	
		
});
