$(document).ready(function() {

	$('ul.productlist li:last-child').css({'border-bottom':'none'});
	
	//Table Striping
	$('.tab_2 table tr:nth-child(odd)').addClass('alt'); 

	//Menu dropdown hover
	$('#header #menu .menu li.drop').mouseenter(function(){
		clearTimeout($(this).data('timeoutId'));
		$(this).children('div').slideDown(200);
	}).mouseleave(function(){
		var menuItem = this;
		var timeoutId = setTimeout(function(){ $(menuItem).children('div').slideUp(200);}, 300);
		$(menuItem).data('timeoutId', timeoutId);
	});


	$("#otherproducts").easySlider();

	$('#contact-frm-btn').click(function(event) {
		event.preventDefault();
		$('#contact-frm').submit();
		return false;
	});

	$(".scrollable").scrollable().navigator();

	$(".items img").mouseover(function() {
		$(this).fadeTo('fast', 1);
	});

	$(".items img").mouseout(function() {
		if ( !$(this).hasClass('active') ) $(this).fadeTo('medium', 0.5);
	});
	

	$(".items img").click(function() {
	
		// see if same thumb is being clicked
		if ($(this).hasClass("active")) { return; }
	
		// get handle to element that wraps the image and make it semi-transparent
		var wrap = $("#galImageContainer").fadeTo("fast", 0.2);
		
		$('.loader').slideDown("medium");
	
		// calclulate large image's URL based on the thumbnail URL (flickr specific)
		var url = $(this).attr("src").replace("w=60", "w=610");
		url = url.replace("h=60", "h=610");
	
		// the large image from www.flickr.com
		var img = new Image();
	
	
		// call this function after it's loaded
		img.onload = function() {
	
			// make wrapper fully visible
			wrap.fadeTo("fast", 1);
	
			// change the image
			wrap.find("img").attr("src", url);
			
			$('.loader').hide();
	
		};
	
		img.src = url;
	
		// activate item
		$(".items img").removeClass("active");
		$(".items img").fadeTo('medium', 0.5);
		$(this).addClass("active");
		$(this).fadeTo('fast', 1);
	
	// when page loads simulate a "click" on the first image
	}).filter(":first").click();

	
	//Login
	$('#menuLoginFrmBtn').click(function(event) {
		event.preventDefault();
		$('#menuLoginFrm').submit();
		return false;
	});
	
	//Newsletter Signup
	$('#newslettersignup').click(function(event) {
		event.preventDefault();
		$('#subForm').submit();
		return false;
	});

	//Sharethis Controller
	$('#shareTrigger').click(function(event) {
		event.preventDefault();
		$('#shareWrapper').slideToggle('fast');
		return false;
	});

	//Homepage Tabs
	$('#slideshowcontent .tab:first, #slideshowcontent .tab div:first').show();
	$('#categorylist li').click(function() {
		var thisTop = $(this).position().top+15;
        $('.pointer').animate( {'top': thisTop} );
        $('.pointer2').animate( {'top': '15px'} );
		$('#categorylist li').removeClass('current');
		$(this).addClass('current');
		var id = $("#categorylist li.current a").attr('href');
	    $("#slideshowcontent > div").fadeOut(500).hide();
		$("#slideshowcontent .tab > div").css({'display':'none'});
		$(id).fadeIn();
		var firstTab = id+' ul + div';
		$(firstTab).fadeIn();
		return false;
    });

	$('.productlist li').click(function() {
		var thisTop = $(this).position().top+15;
        $('.pointer2').animate( {'top': thisTop} );
		$('.productlist li').removeClass('current');
		$(this).addClass('current');
		var id = $(".productlist li.current a").attr('class');
	    $("#slideshowcontent .tab > div").fadeOut(500).hide();
		$('div.'+id).fadeIn();
		return false;
    });


	
	//Product Tabs
	$('#productinfowrap .tab:first').show();	
	$('#subselect li').click(function() {
		if (!$(this).hasClass('current')){
				var thisTop = $(this).position().top;
				$('#subselect li').removeClass('current');
				var li = (this);
				$('.pointer').animate( {'top': thisTop}, function() { 
					$(li).addClass('current');
				});
				var id = $(this).find("a").attr('href');
				$("#productinfowrap > div").fadeOut(500).hide();        
				$(id).fadeIn();
		}
		return false;
    });
	
		
		
		////////////// CALLBACK MODAL FORMS


		$('#reveal').click(function(e) {
			e.preventDefault();
			$('#myModal').reveal({
				 animation: 'fadeAndPop',
				 animationspeed: 300,
				 closeonbackgroundclick: true,
				 dismissmodalclass: 'close-reveal-modal'
			});
		});
		
		// CALLBACK FORM	
		$('#callBack').click(function(e){
			e.preventDefault();

			var name = $('#callMe input[name=name]');
			var telno = $('#callMe input[name=tel]');
			var email = $('#callMe input[name=email]');
			
			var formName = $('#callMe input[name=formName]');
			var enquiry = $('#callMe input[name=product]');
			var extraInfo = $('#callMe input[name=extraInfo]');
			
			if (name.val()=='Name...') {
				name.addClass('highlight');
				return false;
			} else name.removeClass('highlight');

			if (telno.val()=='Telephone...') {
				telno.addClass('highlight');
				return false;
			} else telno.removeClass('highlight');
			
			if (email.val()=='Email...') {
				email.addClass('highlight');
				return false;
			} else email.removeClass('highlight');
			
			var data = 'name=' + name.val() + '&telno=' + telno.val() + '&email=' + email.val() + '&enquiry=' + enquiry.val() + '&formName=' + formName.val() + '&extraInfo=';
			data += extraInfo.is(':checked') ? 'checked' : 'unchecked';

			$(this).attr("disabled", "disabled");
			setTimeout(enableButton, 1500);
			
			function enableButton(){
			   $('#callBack').removeAttr('disabled');
			}

			$.ajax({
				url: "http://www.multipaneluk.co.uk/assets/php/callBackProcess.php",
				type: "POST",
				data: data,     
				cache: false,
				success: function(html){
					$('#callMe .done').show(200).html(html);
					if( html.indexOf("email") !== -1 ) { email.addClass('highlight') }
					if( html.indexOf("phone") !== -1 ) { telno.addClass('highlight') }
					if( html.indexOf("Thank") !== -1 ) {
						setTimeout(function(){
							$('.close-reveal-modal').click();
						}, 1300);
					}
				}       
			});
			 
		});
		
		
		// FREE SAMPLE FORM	
		$('#freeSample').click(function(e){
			e.preventDefault();

			var name = $('#sampleForm input[name=name]');
			var email = $('#sampleForm input[name=email]');
			var address = $('#sampleForm textarea[name=address]');
			var postcode = $('#sampleForm input[name=postcode]');
			var country = $('#country :selected');
			
			var formName = $('#sampleForm input[name=formName]');
			var enquiry = $('#sampleForm input[name=product]');
			var extraInfo = $('#sampleForm input[name=extraInfo]');
			
			if (name.val()=='Name...') {
				name.addClass('highlight');
				return false;
			} else name.removeClass('highlight');

			if (email.val()=='Email...') {
				email.addClass('highlight');
				return false;
			} else email.removeClass('highlight');
			
			if (address.val()=='Address...') {
				address.addClass('highlight');
				return false;
			} else address.removeClass('highlight');

			if (country.text()=='Country') {
				$('#country').addClass('highlight');
				return false;
			} else $('#country').removeClass('highlight');

			var data = 'name=' + name.val() + '&email=' + email.val() + '&address=' + address.val() + '&postcode=' + postcode.val() + '&country=' + country.text() + '&enquiry=' + enquiry.val() + '&formName=' + formName.val() + '&extraInfo=';
			data += extraInfo.is(':checked') ? 'checked' : 'unchecked';

			$(this).attr("disabled", "disabled");
			setTimeout(enableButton, 1500);
			
			function enableButton(){
			   $('#freeSample').removeAttr('disabled');
			}
			
			$.ajax({
				url: "http://www.multipaneluk.co.uk/assets/php/callBackProcess.php",
				type: "POST",
				data: data,     
				cache: false,
				success: function(html){
					$('#sampleForm .done').show(200).html(html);
					if( html.indexOf("email") !== -1 ) { email.addClass('highlight') }
					if( html.indexOf("Thank") !== -1 ) {
						setTimeout(function(){
							$('.close-reveal-modal').click();
						}, 1300);
					}
				}       
			});
			 
		});
		
		
		
		
		

});

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'btn-arrow-lft',
			prevText: 		'<img src="assets/images/btn-arrow-lft.png" width="8" height="13" />',
			nextId: 		'btn-arrow-rht',	
			nextText: 		'<img src="assets/images/btn-arrow-rht.png" width="8" height="13" />',
			controlsShow:	true,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	false,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		false,
			speed: 			800,
			auto:			false,
			pause:			2000,
			continuous:		false, 
			numeric: 		false,
			numericId: 		'controls'
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = $("li", obj).width(); 
			var h = $("li", obj).height(); 
			var clickable = true;
			obj.width(w); 
//			obj.height(h);
			obj.height('25px');
			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			$("ul", obj).css('width',s*w);			
			
			if(options.continuous){
				$("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
				$("ul", obj).append($("ul li:nth-child(2)", obj).clone());
				$("ul", obj).css('width',(s+1)*w);
			};				
			
			if(!options.vertical) $("li", obj).css('float','left');
								
			if(options.controlsShow){
				var html = options.controlsBefore;				
				if(options.numeric){
					html += '<ol id="'+ options.numericId +'"></ol>';
				} else {
					if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
					html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
					html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
					if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';				
				};
				
				html += options.controlsAfter;						
				$(obj).after(html);										
			};
			
			if(options.numeric){									
				for(var i=0;i<s;i++){						
					$(document.createElement("li"))
						.attr('id',options.numericId + (i+1))
						.html('<a rel='+ i +' href=\"javascript:void(0);\">'+ (i+1) +'</a>')
						.appendTo($("#"+ options.numericId))
						.click(function(){							
							animate($("a",$(this)).attr('rel'),true);
						}); 												
				};							
			} else {
				$("a","#"+options.nextId).click(function(){		
					animate("next",true);
				});
				$("a","#"+options.prevId).click(function(){		
					animate("prev",true);				
				});	
				$("a","#"+options.firstId).click(function(){		
					animate("first",true);
				});				
				$("a","#"+options.lastId).click(function(){		
					animate("last",true);				
				});				
			};
			
			function setCurrent(i){
				i = parseInt(i)+1;
				$("li", "#" + options.numericId).removeClass("current");
				$("li#" + options.numericId + i).addClass("current");
			};
			
			function adjust(){
				if(t>ts) t=0;		
				if(t<0) t=ts;	
				if(!options.vertical) {
					$("ul",obj).css("margin-left",(t*w*-1));
				} else {
					$("ul",obj).css("margin-left",(t*h*-1));
				}
				clickable = true;
				if(options.numeric) setCurrent(t);
			};
			
			function animate(dir,clicked){
				if (clickable){
					clickable = false;
					var ot = t;				
					switch(dir){
						case "next":
							t = (ot>=ts) ? (options.continuous ? t+1 : ts) : t+1;						
							break; 
						case "prev":
							t = (t<=0) ? (options.continuous ? t-1 : 0) : t-1;
							break; 
						case "first":
							t = 0;
							break; 
						case "last":
							t = ts;
							break; 
						default:
							t = dir;
							break; 
					};	
					var diff = Math.abs(ot-t);
					var speed = diff*options.speed;						
					if(!options.vertical) {
						p = (t*w*-1);
						$("ul",obj).animate(
							{ marginLeft: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);				
					} else {
						p = (t*h*-1);
						$("ul",obj).animate(
							{ marginTop: p }, 
							{ queue:false, duration:speed, complete:adjust }
						);					
					};
					
					if(!options.continuous && options.controlsFade){					
						if(t==ts){
							$("a","#"+options.nextId).hide();
							$("a","#"+options.lastId).hide();
						} else {
							$("a","#"+options.nextId).show();
							$("a","#"+options.lastId).show();					
						};
						if(t==0){
							$("a","#"+options.prevId).hide();
							$("a","#"+options.firstId).hide();
						} else {
							$("a","#"+options.prevId).show();
							$("a","#"+options.firstId).show();
						};					
					};				
					
					if(clicked) clearTimeout(timeout);
					if(options.auto && dir=="next" && !clicked){;
						timeout = setTimeout(function(){
							animate("next",false);
						},diff*options.speed+options.pause);
					};
			
				};
				
			};
			// init
			var timeout;
			if(options.auto){;
				timeout = setTimeout(function(){
					animate("next",false);
				},options.pause);
			};		
			
			if(options.numeric) setCurrent(0);
		
			if(!options.continuous && options.controlsFade){					
				$("a","#"+options.prevId).hide();
				$("a","#"+options.firstId).hide();				
			};				
			
		});
	  
	};
	
	
})(jQuery);
