
Shadowbox.init({
    overlayOpacity: "0.8"
});

$(function() {
    $('.scroll-pane').jScrollPane();
});

function isValidEmail($email){
    return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
}

function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
};

//preload images
function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
    });
}

preload([
    'images/drop_bg_top.png',
    'images/drop_bg.png',
    'images/lava_home2.png',
    'images/lava3.png',
    'images/lava_home.png',
    'images/lava2.png'
]);

$(document).ready(function() {
    
    
    //head slideshow
    $('#head_slide').cycle({ 
        fx: 'fade',
        speed: 2000,
   	timeout: 6000
    });
    
    //correct navigation bg
    /*$('#nav').find('a').each(function() {
	
	if ($(this).next() == 'ul') {
	    $(this).parent().addClass('drop');
	}
	
    });*/
    
    //top nav
    $('#nav li').hover(function() {
	
	if ($('ul',this).length) {
	    //alert('dropdown');
	    $(this).addClass('over_drop');
	    $(this).children(':first').addClass('curve_drop');
	    $('ul', this).slideDown('fast');
	}
	
	$(this).addClass('over');
	$(this).children(':first').addClass('curve');
	//$('ul', this).slideDown('fast');
	
	$('#nav li ul li').removeClass('over');
	$('#nav li ul li a').removeClass('curve');
	
    },function() {
	
	$(this).removeClass('over');
	$(this).removeClass('over_drop');
	$(this).children(':first').removeClass('curve');
	$(this).children(':first').removeClass('curve_drop');
	$('ul', this).slideUp('fast');
	    
	/*var menu = $(this);
	
	setTimeout(function(){
	    
	    $('ul', menu).fadeOut('fast');
	    
	}, 300);*/
	
    });
    
    
    
    

    var brochOver = $('<img src="/images/corp_over.png" class="sidebar_over" />').insertAfter('#broch_link img').hide();
    var prodOver = $('<img src="/images/prod_over.png" class="sidebar_over" />').insertAfter('#prod_link img').hide();
    var nbsOver = $('<img src="/images/nbs_over.png" class="sidebar_over" />').insertAfter('#nbs_link img').hide();
    var prodBrochOver = $('<img src="/images/prod_broch_over.png" class="sidebar_over" />').insertAfter('#prod_broch_link img').hide();
    


    $('#broch_link').hover(
        function(){
            brochOver.stop(true,true).fadeIn('fast');
        },
        function(){
            $('.sidebar_over').stop(true,true).fadeOut('fast');
        }
    );

    $('#prod_link').hover(
        function(){
            prodOver.stop(true, true).fadeIn('fast');
        },
        function(){
            $('.sidebar_over').stop(true,true).fadeOut('fast');
        }
    );

    $('#nbs_link').hover(
        function(){
            nbsOver.stop(true,true).fadeIn('fast');
        },
        function(){
            $('.sidebar_over').stop(true,true).fadeOut('fast');
        }
    );
    
    $('#prod_broch_link').hover(
        function(){
            prodBrochOver.stop(true,true).fadeIn('fast');
        },
        function(){
            $('.sidebar_over').stop(true,true).fadeOut('fast');
        }
    );
    
    //products links
    var measureOver = $('<img src="/images/measure-over.png" class="prod_link_over" />').insertAfter('#measure_link img').hide();
    var wholesaleOver = $('<img src="/images/whole-over.png" class="whole_link_over" />').insertAfter('#whole_link img').hide();
    
    $('#measure_link').hover(
	function() {
	    measureOver.stop(true, true).fadeIn('fast');
	},
	function() {
	    $('.prod_link_over').stop(true, true).fadeOut('fast');
	}
    );
    
    $('#whole_link').hover(
	function() {
	    wholesaleOver.stop(true, true).fadeIn('fast');
	},
	function() {
	    //alert('off');
	    $('img.whole_link_over').stop(true, true).fadeOut('fast');
	}
    );
    
    
    
    $('#home_content #box, #home_content #sidebar').hide().delay(1000).fadeIn(3000);
    
    //search box
    $('input.search_text').click(function() {
	if ($(this).val() == 'Search') {
	    $(this).val('');
	}
    });
    
    //nbs hover
    $('#nbs_link').click(function() {
	
	if ($('#nbs_output').css('display') == 'none') {
	    $('#nbs_output').fadeTo(800, 0.8);
	}else{
	    $('#nbs_output').fadeOut(800);
	}
	
	return false;
	
	/*function(){
            $('#nbs_output').fadeIn('slow');
	    return false;
        },
        function(){
            $('#nbs_output').fadeOut('slow');
	    return false;
        }*/
	
    });
    
    $('#nbs_close').click(function() {
	$('#nbs_output').fadeOut(800);
	return false;
    });
    
    /*gallery*/
    
    /*$('#gallery li a').hover(function() {
	var caption = $(this).attr('title');
	
	$('#caption p').hide().remove();
	$('#caption').append('<p>' + caption + '</p>');
	$('#caption p').hide().fadeIn('fast');
    });*/
    
    
    //product viewer
    $('#exp_link').click(function(e) {
	e.preventDefault();
    });
    
    //set height of #view_nav ul
    
    var setHeight = $('#view_nav ul.cats').outerHeight() + 40;
    $('#view_nav ul.cats').css('height', setHeight);
    
    $('#view_nav li').hover(function() {
	
	$('ul.cats', this).stop('true', 'true').slideDown('fast');
	
    },function() {
	
	$('ul.cats', this).stop('true', 'true').slideUp('fast');
	
    });
    
    $('ul.cats li').hover(
	
	function() {
	
	    $('ul', this).stop('true', 'true').fadeIn('fast');
	    
	    if ($('li', this).length < 6) {
		$(this).css('position', 'relative');
	    }
	
	}, function() {
	
	    $('ul', this).stop('true', 'true').fadeOut('fast');
	}
    );
    
    $('ul.prods li').hover(function() {
	$('img', this).stop('true', 'true').fadeIn('fast');
    }, function() {
	$('img', this).stop('true', 'true').fadeOut('fast');
    });
    
    /*$('#products_menu').hover(function() {
	$('#mega').stop('true', 'true').slideDown('fast');
    }, function() {
	$('#mega').stop('true', 'true').slideUp('fast');
    });*/
    
    $('a.prod_more').click(function() {
        
        if ($(this).next().hasClass('vis')) {
            $(this).next().slideUp('fast').removeClass('vis');
        }else{
            $(this).next().slideDown('fast').addClass('vis');
        }
        
        return false;
        
    });
    
    
    //slider
    
    $('#prod_slide').cycle({ 
	fx: 'fade',
        speed: 2000,
   	timeout: 6000,
	pause: 1,
	next: '#next_prod', 
	prev: '#prev_prod' 
    });
    
    $('#prev_prod').css('opacity', 0.5);
    $('#next_prod').css('opacity', 0.5);
    
    $('#prev_prod').hover(function() {
	$(this).fadeTo('fast', 1);
    },function() {
	$(this).fadeTo('fast', 0.5);
    });
    
    $('#next_prod').hover(function() {
	$(this).fadeTo('fast', 1);
    },function() {
	$(this).fadeTo('fast', 0.5);
    });
    
    /**  brochure download **/
    $('a.broch_download').click(function(e) {
	
	e.preventDefault();
	
	var file_id = $(this).attr('rel');
	var $file_type = $(this).attr('title');
	
	var maskHeight = $('#container').outerHeight();
        
        $('<div id="shadow"></div>').insertBefore('#modal');
	$('#shadow').css({'width':'100%','height': maskHeight}).fadeIn('fast');
	
	$('<input type="hidden" name="file_id" value="' + file_id + '" />').appendTo('#download_form');
	$('<input type="hidden" name="type" value="' + $file_type + '" />').appendTo('#download_form');
	
	var topMargin = ($(window).height() - 194) / 2;
	
	$('#modal').css('top', topMargin);
	
	$('#modal').fadeIn('slow', function() {
            
            if ($.browser.msie) {
                this.style.removeAttribute('filter');
            }
            
        });
		
    });
    
    $('#shadow').live('click', function() {
	$('#shadow').fadeOut('fast');
	$('#modal').fadeOut('normal');
    });
    
    $('#mod_close').live('click', function() {
	$('#shadow').fadeOut('fast');
	$('#modal').fadeOut('normal');
    });
    
    $('#download_form').live('submit', function() {
	
	//e.preventDefault();
	
	var formErrors = new Array();
	
	$('input.error').removeClass('error');
	$('p.error_message').hide();
	
	if ($('#name').val().length < 2) {
	    formErrors[0] = "Please enter a name.";
	    $('<p class="error_message">Please enter a name.</p>').insertAfter($('#name'));
	    $('#name').addClass('error');
	}
	
	if ($('#company').val().length < 2) {
	    formErrors[1] = "Please enter your company.";
	    $('<p class="error_message">Please enter your company.</p>').insertAfter($('#company'));
	    $('#company').addClass('error');
	}
	
	if ($('#phone').val().length < 6) {
	    formErrors[2] = "Please enter your phone number.";
	    $('<p class="error_message">Please enter your phone number.</p>').insertAfter($('#phone'));
	    $('#phone').addClass('error');
	}
	
	if (!isValidEmailAddress($('#email').val())) {
	    formErrors[3] = "Please enter a valid email address.";
	    $('<p class="error_message">Please enter a valid email address.</p>').insertAfter($('#email'));
	    $('#email').addClass('error');
	}
	
	if (formErrors.length == 0) {
	    $('#shadow').hide();
	    $('#modal').fadeOut('normal');
	    return true;
	}else{
	    return false;
	}
	
	
	
    });
    
    
    //show category name on hover
    $('#brochure_list li').hover(function(){
            $('p', this).fadeTo('normal', 0.8);
        },
        function(){
            $('p', this).fadeOut('fast');
        }
    );
    
    //help icon
    $('img.info').hover(
        function() {
            var helpContent = $(this).attr('alt');
            $('<p class="info_output">'+ helpContent +'</p>').insertAfter($(this)).hide().fadeTo('fast', 0.8);
        },
        function() {
            $('p.info_output').fadeOut('fast');
        }
    );
    
    //delivery address set up
    //delivery details function
    function copy_field(field, copy) {
        
        var fieldName = field;
        var copyLoc = copy;
        
        fieldName.val(copyLoc.val());
        fieldName.attr('disabled', true).addClass('disabled_input');
        
    }
    
    function uncopy_field(field) {
        
        $(field).val('').attr('disabled', false).removeClass('disabled_input');
        
    } 
    
    //fill in delivery details
    $('#send_to_billing').click(function() {
        
        if ($(this).is(':checked')) {
            
            copy_field($('#del_address1'), $('#address1'));
            copy_field($('#del_address2'), $('#address2'));
            copy_field($('#del_city'), $('#city'));
            copy_field($('#del_county'), $('#county')); 
            copy_field($('#del_postcode'), $('#postcode')); 
            
        }else{
            
	    uncopy_field($('#del_address1'));
            uncopy_field($('#del_address2'));
            uncopy_field($('#del_city'));
            uncopy_field($('#del_county'));
            uncopy_field($('#del_postcode'));
            
        }
        
    });
    
    //datepicker
    //$(function() {
	//$('#required').datepicker();
    //});
    
    $( "#required" ).datepicker({dateFormat: 'dd/mm/yy'});
    
    /*$( "#required" ).datepicker();
	$( "#format" ).change(function() {
	    $( "#required" ).datepicker({dateFormat: 'dd/mm/yy'});
    });*/
    
    /*$.datepicker.setDefaults( $.datepicker.regional[ "" ] );
    $( "#required" ).datepicker( $.datepicker.regional[ "fr" ] );
    $( "#locale" ).change(function() {
	$( "#required" ).datepicker( "option",
	$.datepicker.regional[ $( this ).val() ] );
    });*/

    
});
