jQuery(document).ready(function(){
		
	//connect to links
	jQuery("a.start-popup").click(function(){
	
		loadStart();
		
		return false;
		
	});
	
	jQuery("a.quick-product-search").click(function(){
	
		loadProductsearch();
		
		return false;
	
	})
	
	jQuery(".fast-quote").click(function(){
		
		loadFastquote();
		
		return false;
	
	})
	
	jQuery(".showroom-link").click(function(){
	
		loadShowroom();
		
		return false;
	
	})
	
	jQuery(".range-popup").click(function(){
	
		loadRange();
		
		return false;
	
	})
	
});

function loadRange() {

	jQuery('body').append(jQuery("<div id='start_overlay'></div>").css({'display': 'block', 'opacity': '0', 'position': 'fixed', 'width': '100%', 'height': '100%', 'background': '#000', 'top': '0px', 'left': '0px', 'z-index': '100'}));
	
	jQuery('#start_overlay').click(function(){
		
		hideRange();
		
	})
	
	jQuery('#start_overlay').animate({'opacity': '0.5'}, 1000);
	
	theTop = getPageScroll()[1] + (getPageHeight() / 10);
	
	//do the modal
	jQuery("<div id='start_popup2'>"+jQuery('#range-wrapper').clone().show().html()+"</div>").appendTo(jQuery('body'));
	
	jQuery('#start_popup2').css({'z-index': '100', 'position': 'absolute', 'top': theTop})
		
	theLeft = jQuery(window).width() / 2 - (jQuery('#start_popup2').width() / 2);
		
	jQuery('#start_popup2').css({'left': theLeft});
	
	$('#start_popup2 .showroom ul').cycle();
	
	jQuery('#start_popup2 a.close-link').click(function(){
	
		hideRange();
	
		return false
	
	});
	
	jQuery('#start_popup2 .fast-quote').click(function(){
	
		jQuery('#start_popup2').remove();
		jQuery('#start_overlay').remove();
		
		loadFastquote()
	
		return false;
	
	})

}

function hideRange() {

	jQuery('#start_popup2').remove();
	
	jQuery('#start_overlay').fadeOut(1000, function(){
		
		jQuery(this).remove();
		
	})

}

function loadShowroom() {

	jQuery('body').append(jQuery("<div id='start_overlay'></div>").css({'display': 'block', 'opacity': '0', 'position': 'fixed', 'width': '100%', 'height': '100%', 'background': '#000', 'top': '0px', 'left': '0px', 'z-index': '100'}));
	
	jQuery('#start_overlay').click(function(){
		
		hideShowroom();
		
	})
	
	jQuery('#start_overlay').animate({'opacity': '0.5'}, 1000);
	
	theTop = getPageScroll()[1] + (getPageHeight() / 10);
	
	//do the modal
	jQuery("<div id='start_popup3'>"+jQuery('#showroom-wrapper').clone().show().html()+"</div>").appendTo(jQuery('body'));
	
	jQuery('#start_popup3').css({'z-index': '100', 'position': 'absolute', 'top': theTop})
		
	theLeft = jQuery(window).width() / 2 - (jQuery('#start_popup3').width() / 2);
		
	jQuery('#start_popup3').css({'left': theLeft});
	
	$('#start_popup3 .showroom ul').cycle();
	
	jQuery('#start_popup3 a.close-link').click(function(){
	
		hideShowroom();
	
		return false
	
	})

}

function loadFastquote() {

	hideRange();

	jQuery('body').append(jQuery("<div id='start_overlay'></div>").css({'display': 'block', 'opacity': '0', 'position': 'fixed', 'width': '100%', 'height': '100%', 'background': '#000', 'top': '0px', 'left': '0px', 'z-index': '100'}));
	
	jQuery('#start_overlay').click(function(){
		
		hideFastquote();
		
	})
	
	jQuery('#start_overlay').animate({'opacity': '0.5'}, 1000);
	
	theTop = getPageScroll()[1] + (getPageHeight() / 10);
	
	//do the modal
	jQuery("<div id='start_popup2'>"+jQuery('#fast-quote-wrapper').clone().show().html()+"</div>").appendTo(jQuery('body'));
	
	jQuery('#start_popup2').css({'z-index': '100', 'position': 'absolute', 'top': theTop})
		
	theLeft = jQuery(window).width() / 2 - (jQuery('#start_popup2').width() / 2);
		
	jQuery('#start_popup2').css({'left': theLeft});
	
	$('input.datetime').datetimepicker({
		dateFormat: 'dd/mm/yy'
	});
	
	jQuery('#start_popup2 a.close-link').click(function(){
	
		hideFastquote();
	
		return false
	
	})

}

function loadProductsearch() {

	jQuery('body').append(jQuery("<div id='start_overlay'></div>").css({'display': 'block', 'opacity': '0', 'position': 'fixed', 'width': '100%', 'height': '100%', 'background': '#000', 'top': '0px', 'left': '0px', 'z-index': '100'}));
	
	jQuery('#start_overlay').click(function(){
		
		hideProductsearch();
		
	})
	
	jQuery('#start_overlay').animate({'opacity': '0.5'}, 1000);
	
	theTop = getPageScroll()[1] + (getPageHeight() / 10);
	
	//do the modal
	jQuery("<div id='start_popup2'>"+jQuery('#product-specials-wrapper').clone().show().html()+"</div>").appendTo(jQuery('body'));
	
	//work some magic on the specials links
	$('ul.home-specials li').each(function(){
		
		if($(this).find('a span').size() == 0) {
		
		newSpan = $('<span>'+$(this).find('a > img').attr('alt')+'</span>');
		
		$(this).find('a').prepend(newSpan);
		
		}
	
	})
	
	jQuery('#start_popup2').css({'z-index': '100', 'position': 'absolute', 'top': theTop})
		
	theLeft = jQuery(window).width() / 2 - (jQuery('#start_popup2').width() / 2);
		
	jQuery('#start_popup2').css({'left': theLeft});
	
	jQuery('#start_popup2 a.close-link').click(function(){
	
		hideProductsearch();
	
		return false
	
	})

}

function loadStart() {
	
	//do the overlay
	jQuery('body').append(jQuery("<div id='start_overlay'></div>").css({'display': 'block', 'opacity': '0', 'position': 'fixed', 'width': '100%', 'height': '100%', 'background': '#000', 'top': '0px', 'left': '0px', 'z-index': '100'}));
	
	jQuery('#start_overlay').click(function(){
		
		hideStart();
		
	})
	
	jQuery('#start_overlay').animate({'opacity': '0.5'}, 1000);
	
	theTop = getPageScroll()[1] + (getPageHeight() / 10);
	
	
	//do the modal
	jQuery("<div id='start_popup'>"+jQuery('#start').clone().show().html()+"</div>").appendTo(jQuery('body'));
	
	jQuery('#start_popup').css({'z-index': '100', 'position': 'absolute', 'top': theTop})
		
	theLeft = jQuery(window).width() / 2 - (jQuery('#start_popup').width() / 2);	
	
	jQuery('#start_popup').css({'left': theLeft});
	
	//tooltips
	/*jQuery("#start_popup a#price-list, #start_popup a#ebook, #start_popup a#catalogue").qtip({
		style: { name: 'dark', tip: true, fontSize: 13, padding: '10px', lineHeight: '21px', border: {radius: 5} },
		position: { target: 'mouse' }
	})*/
	
	jQuery('#start_popup a.close-link').click(function(){
	
		hideStart();
	
		return false
	
	})
		
	jQuery("#start_popup > div > div > div > a:not(.inactive, .non)").click(function(){
	
		if(jQuery(this).hasClass('active')) {
		
			//this one is active
			jQuery(this).parent().find('div.form_container').slideUp(800, function(){
				
				jQuery(this).find("form").hide();
				jQuery(this).find("div.results").hide();
								
			});
			
			jQuery(this).removeClass('active');
		
		} else {
				
			//hide all
			jQuery("#start_popup > div > div > div > a.active").each(function(){
			
				jQuery('div.form_container').slideUp(800, function(){
					
					
				});
			
				jQuery(this).removeClass('active');
				
				//hide forms
				jQuery("form.start_form:not(.top_form)").fadeOut(800);
				jQuery("div.results").fadeOut(800);
			
			});	
			
			//display the right form
						
			if(jQuery(this).attr('id') == 'price-list') {
				
				jQuery(this).parent().find('div.form_container').find('#results_'+jQuery(this).attr('id')).fadeIn(800);
				
			} else if(jQuery(this).attr('id') == 'ebook') {
				
				jQuery(this).parent().find('div.form_container').find('#results_'+jQuery(this).attr('id')).fadeIn(800);
				
			} else if(jQuery(this).attr('id') == 'catalogue') {
				
				jQuery(this).parent().find('div.form_container').find('#results_'+jQuery(this).attr('id')).fadeIn(800);
				
			} else {
				
				jQuery(this).parent().find('div.form_container').find('#form_'+jQuery(this).attr('id')).fadeIn(800);
				
			}
			
		
			jQuery(this).addClass('active');
			
			//set the form_container to the right height (that of the form)
			
			jQuery(this).parent().find('div.form_container').slideDown(800);
		
		}
				
		return false;
		
	})
}

function hideStart() {
	
	jQuery('#start_popup').remove();
	
	jQuery('#start_overlay').fadeOut(1000, function(){
		
		jQuery(this).remove();
		
	})
	
}

function hideProductsearch() {
	
	jQuery('#start_popup2').remove();
	
	jQuery('#start_overlay').fadeOut(1000, function(){
		
		jQuery(this).remove();
		
	})
	
}

function hideFastquote() {

	jQuery('#start_popup2').remove();
	
	jQuery('#start_overlay').fadeOut(1000, function(){
		
		jQuery(this).remove();
		
	})

}

function hideShowroom() {

	jQuery('#start_popup3').remove();
	
	jQuery('#start_overlay').fadeOut(1000, function(){
		
		jQuery(this).remove();
		
	})

}


// getPageScroll() by quirksmode.com
function getPageScroll() {
	
	var xScroll, yScroll;
    
    if (self.pageYOffset) {
    	yScroll = self.pageYOffset;
      	xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
      	yScroll = document.documentElement.scrollTop;
      	xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      	yScroll = document.body.scrollTop;
      	xScroll = document.body.scrollLeft;	
    }
    
    return new Array(xScroll,yScroll) 
}

// Adapted from getPageSize() by quirksmode.com
function getPageHeight() {
    
    var windowHeight
    
    if (self.innerHeight) {	// all except Explorer
    	windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      	windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      	windowHeight = document.body.clientHeight;
    }
    	
    return windowHeight
}

function makeButtonsActive() {
	
	jQuery("#start_popup > div > div > div > a.inactive").removeClass('inactive');
	
	jQuery("#start_popup > div > div > div > a").click(function(){
	
		if(jQuery(this).hasClass('active')) {
		
			//this one is active
			jQuery(this).parent().find('div.form_container').slideUp(800, function(){
				
				jQuery(this).find("form").hide();
				jQuery(this).find("div.results").hide();
								
			});
			
			jQuery(this).removeClass('active');
		
		} else {
				
			//hide all
			jQuery("#start_popup > div > div > div > a.active").each(function(){
			
				jQuery('div.form_container').slideUp(800, function(){
					
					
				});
			
				jQuery(this).removeClass('active');
				
				//hide forms
				jQuery("form.start_form:not(.top_form)").fadeOut(800);
				jQuery("div.results").fadeOut(800);
			
			});	
			
			//display the right form
			
			if(jQuery(this).attr('id') == 'price-list') {
				
				jQuery(this).parent().find('div.form_container').find('#results_'+jQuery(this).attr('id')).fadeIn(800);
				
			} else if(jQuery(this).attr('id') == 'ebook') {
				
				jQuery(this).parent().find('div.form_container').find('#results_'+jQuery(this).attr('id')).fadeIn(800);
				
			} else if(jQuery(this).attr('id') == 'catalogue') {
				
				jQuery(this).parent().find('div.form_container').find('#results_'+jQuery(this).attr('id')).fadeIn(800);
				
			} else {
				
				jQuery(this).parent().find('div.form_container').find('#form_'+jQuery(this).attr('id')).fadeIn(800);
				
			}
			
		
			jQuery(this).addClass('active');
			
			//set the form_container to the right height (that of the form)
			
			jQuery(this).parent().find('div.form_container').slideDown(800);
		
		}
				
		return false;
		
	})
	
}
