/**** Add application wide javascripts below this point  ******/
var slide = 1, autotime=false, _auto;
$(document).ready(function() {
	_auto = function(){ $('#article_switcher .next a').trigger("click");};
	if($('#js-news') && $('#js-news').length) $('#js-news').ticker();
	
	$("#quick_contact_form").dialog({autoOpen:false, modal:true, width:320, resizable:false});
	$("a.dialog").bind("click", function(e){
	  e.preventDefault();
	  $("#quick_contact_form").dialog("option", "title", $(this).text()).dialog("open");
	});
	
	$("#contact_form").validate();
	
	/** ARTICLE SWITCHER **/
	if($('#article_images').length){
	  
	  
		/*hide all then show the first*/
		$('#article_images img').hide();
		$('#article_images img:first').show();
		$('#articles .article').hide();
		$('#articles .article:first').show();
		$('#article_switcher li.1').addClass('active');
		/*on click of links get ready to swap stuff*/
		$('#article_switcher a.article_link').click(function(){
			article_switch($(this).parent().attr('class'), $('#article_switcher li.active').attr('class').replace('active', '') );
			$(this).parent().addClass('active');
			return false;
		});
		$('#article_switcher li.previous a, #article_switcher li.next a').click(function(){
			current = $('#article_switcher li.active').attr('class').replace('active', '');
			max = 0;
			$('#article_switcher ul').children().each(function(){max = max +1;});
			if(max>0) max = max - 3;
			if(current == "1"){
				use = max;
			}else if(current == max){
				use = "1";
			}else{
				use = parseInt(current)+1;
			}
			article_switch(use, current);
			return false;
		});
    
    
    autotime = setInterval(_auto, 8000);
    
	}
	
	/** CATEGORY SLIDING IMAGE **/
	if($('.big_image').length){
		$('.big_image .tab').click(function(){
			if(slide == 1){
				$('.big_image .section_info').slideUp("slow");
				slide = 2;
				$(this).addClass('tab_down');
				$(this).removeClass('tab_up');				
				$('#sub_navigation .active').addClass('restricted');
			}else {
				$('.big_image .section_info').slideDown("slow");
				slide = 1;
				$(this).addClass('tab_up');
				$(this).removeClass('tab_down');
				$('#sub_navigation .active').removeClass('restricted');				
			}
			return false;
		})
	}
	
	/** TABS **/
	if($('#product_extras').length){
		$('#product_extras > ul').tabs();
	}
	
	/** image switching **/
	if($('.image_list').length){
		$('.image_list li a').click(function(){
			rel = $(this).attr('rel');
			src = "/show-image/"+rel+"/300.gif";
			$('#main_product_image').fadeTo("slow", 0.2);
			$('#main_product_image').attr('src', src);
			$('#main_product_image').fadeTo("slow", 1);
			$('.image_list li').removeClass("active");
			$(this).parent().addClass("active");
			
			return false;
		});
	}
	
	/** slide down "area" iamges **/
	if($('.box_content').length){
		$('.box_content li.clickable img').hide();
		$('.box_content li.clickable a.clickable').click(function(){
			$(this).parent().find('img').slideToggle("slow");
			
			return false;
		});
	}
	
	/** google map **/
	if($("#gmap").length) { 
    map = new GMap2(document.getElementById("gmap"));
    maplocation = new GLatLng(52.673247, -2.118988);
    map.setCenter(maplocation, 15);
		map.addControl(new GSmallMapControl());
		var marker = new GMarker(maplocation);
    map.addOverlay(marker);
  }
	
	/********* Auto Submit Forms ********/
  $(".autosubmit").change(function(){
    $(this).parents('form').submit();
  });

	/* check all button */
	if($('.checkall').length){
		$('.checkall').click(function(){
			$(this).parent().parent().find('input').attr('checked', 'checked');
			return false;
		});
	}
	if($('.uncheckall').length){
		$('.uncheckall').click(function(){
			$(this).parent().parent().find('input').attr('checked', '');
			return false;
		});
	}
	
	/* form validation */
	if($('#under_contract').length){
		$('#under_contract').hide();
		$('#under_contract img.next, #under_contract img.skip').click(function(){
			$('#step_by_step ul li').addClass("disabled");
			$('#under_contract').fadeOut("fast", function(){				
																						$('#contact_details').fadeIn("fast");
																						$('#step_by_step ul li#step3').removeClass('disabled');
																						});
			return false;
		});
	}
	if($('#contact_details').length){
		$('#contact_details').hide();
		$('#contact_details .submit').click(function(){
			valid = 0;
			required = $('#contact_details .required').length;
			$('#contact_details .required').each(function(){
				if(this.value) valid = valid+1;
			});
			if(valid == required){
				$(this).parents('form').submit();
			}else{
				$('#contact_details .error').css('display', 'block');
				$('#contact_details .required').addClass('missing');
			}
			return false;
		});
	}
	if($('#services_fieldset').length){
		$('#services_fieldset img.next').click(function(){
			valid = false;
			if($('#services_fieldset input:checked').length > 0){
				valid = true;
			}
			if(valid){
				$('#step_by_step ul li').addClass("disabled");
				$('#services_fieldset').fadeOut("fast", function(){
																									$('#under_contract').fadeIn("fast");
																									$('#step_by_step ul li#step2').removeClass('disabled');
																									});
			}else{
				$('#services_fieldset .error').css('display', 'block');
			}
			return false;
		});
	}
	
	$('#step_by_step, #estimate_form .hidden').removeClass('hidden');
	if($('#estimate_form').length){
		/* step by step */
		$('.no_js').removeClass('no_js');
		$('#step_by_step ul li:first').removeClass('disabled');
	}
	
});

function article_switch(use, current){
  clearInterval(autotime);
	$('#articles .article_'+current).css('display', "none");
	$('#articles .article_'+use).css('display', 'block');
	
	$('#article_images img.article_'+current).css('display', "none");
	$('#article_images img.article_'+use).css('display', 'block');
	
	$('#article_switcher li').removeClass('active');
	$("#article_switcher li."+use).addClass('active');
	autotime = setInterval(_auto, 8000);
}

