// initialize the jquery code

var doSlide = function(container) {
    if (activeTab != "") {
        if (activeTab == container) {
            $(activeTab).slideUp();
            activeTab = "";
        } else {
            $(activeTab).slideUp(function() {
                $(container).slideDown();
                activeTab = container;
            });
        }
    } else {
        $(container).slideDown();
        activeTab = container;
    }
}

// bookmark script
function CreateBookmarkLink() {
	title = "Anglian Pumping Services"; 
	url = "http://www.anglianpumping.co.uk/";
	
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if ( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); 
	} else if (window.opera && window.print) { // Opera Hotlist
		return true; 
	}
}

$(function() {
	// product info tabs
	$(".specs ul").tabs({ fxSlide: true, fxFade: true, fxSpeed: 'fast', unselected: true, unselect: true });
	
	// save for later tab
	$(".saveLater ul").tabs({ fxSlide: true, fxFade: true, fxSpeed: 'fast', unselected: true, unselect: true });
	
	// send basket to a colleague
	$(".sendBasket ul").tabs({ fxSlide: true, fxFade: true, fxSpeed: 'fast', unselected: true, unselect: true });

	// send to a colleague
	$(".sendColleague ul").tabs({ fxSlide: true, fxFade: true, fxSpeed: 'fast', unselected: true, unselect: true });

	// text resize
	$('#accessibility').ability({ 
                    textsizer: true, 
                    textsizeclasses: ['m', 'l', 'xl'], 
                    switcher: false,
                    styledir: "css/", 
                    savecookie: false, 
                    defaultcss: 'style.css' 
                }); 
});


$(document).ready(function() {
    
    for(x in galeries) {
        $('#'+x).jcarousel({
            start: galeries[x]
        })
    }    
       
    $('.subc').hide();
    
    if (activeTab != "") {
        $(activeTab).slideDown();
    }
    
    $('#tabs > ul > li').bind('click', function(){
        var container = $(this).find('a').attr('href');
        if ((container.charAt(0) == "#") && (container.length > 1)) {
            $('#tabs > ul > li').removeClass('active');
            $(this).toggleClass('active');
            doSlide(container);
            return false;
        }
    });
    
    $('.img_container').hoverIntent({
    	sensitivity: 7,
    	interval: 300,
    	timeout: 0,
    	over: function () {
            $(this).parent().parent().parent().parent().parent().find('div.subc_desc').html($(this).find('a').attr('title'));
            $(this).parent().parent().parent().parent().parent().find('div.subc_desc').animate({ opacity: 100 }, 500 );
        },
    	out: function() {
            $(this).parent().parent().parent().parent().parent().find('div.subc_desc').animate({ opacity: 0 }, 300 );
        }
    });
    
    // fancy animated scrolling
	$('.scroll').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target	|| $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top - 15;
				$('html,body').animate({scrollTop: targetOffset}, 1000, function () {
                    $target.parent().hasClass('hfade') ? $target.parent().highlightFade({speed:1000}) : null;
                    //$('.hfade').highlightFade({speed:1000});
                });
				return false;
			}
		}
	});

	$('#hfade').each(function() {
		//var $target = $(this.hash);
		var $target = $(this);
		if ($target.length) {
			var targetOffset = $target.offset().top - 15;
			$('html,body').animate({scrollTop: targetOffset}, 1000, function () {
				$target.highlightFade({speed:1000})
			});
		}
	});
    
    function prev_image($obj) {
        var img_index = $obj.data('selected_image');
        if (img_index > 0) {
            var new_index = img_index-1;
            var img_src = $obj.find('div.product_div > ul > li:eq('+new_index+')').find('img').attr('src');
            $obj.find('img.prod_img').attr({src: img_src});
            $obj.data('selected_image', img_index-1);            
        }
    }

    function next_image($obj) {
        var img_index = $obj.data('selected_image');
        var img_count = $obj.find('div.product_div > ul > li').length;
        if (img_index < img_count-1) {
            var new_index = img_index+1;
            var img_src = $obj.find('div.product_div > ul > li:eq('+new_index+')').find('img').attr('src');
            $obj.find('img.prod_img').attr({src: img_src});
            $obj.data('selected_image', img_index+1);
        }
    }
    
    function open_image($obj) {
        var img_index = $obj.data('selected_image');
        var img_src = $obj.find('div.product_div > ul > li:eq('+img_index+')').find('a').attr('href');
        $.facebox('<img src="'+img_src+'"/>');
    }
    
    $('div.product').each(function (i) {
        var start_image = $(this).find('div.product_div > ul > li:eq(0)').find('img').attr('src');
        $(this).find('img.prod_img').attr({ src: start_image});
        $(this).data('selected_image' , 0);
        
        $(this).find('div > a.prev_link').bind('click', function() {
            prev_image($(this).parent().parent());
        });
        
        $(this).find('div > a.next_link').bind('click', function() {
            next_image($(this).parent().parent());
        });
        
        $(this).find('img.prod_img').bind('click', function() {
            open_image($(this).parent().parent());
        });
    });
    
});

// contact form
$(document).ready(function() {
	$("#contact").validate({
		rules : { 
			dummyfield: {
				required: true
			}
		},
		messages: {
			dummyfield: {
				required: "Enter phone number or email address."
            }
		}
	});
	
	
	$('input#tel').keypress( function() {
		temp = $("input#tel").val();
		$("#dummyfield").val(temp);
		return $("#dummyfield").val(temp);
	});
	
	$('input#email').keypress( function() {
		temp = $("input#email").val();
		$("#dummyfield").val(temp);
		return $("#dummyfield").val(temp);
	});
	
});
