var $j = jQuery.noConflict(); 

$j(document).ready(function(){

	// jump nav
	$j('a.jn').click(function(){
		var target = $j(this).attr('href');
		$j('html,body').animate({ scrollTop: $j(target).offset().top }, { duration: '1000', queue: false });				
		return false;
	});
	
	// Slide outs
	$j('.slideout').hide().append('<a href="#" class="close">Close</a>');
	$j('.slidetigger').click(function(){				
		var target = $j(this).attr('href');
		if (target == '#gallery' || target == '#videos') {
			$j('.intro').fadeOut('slow', function() {
				$j(target).slideDown('slow', function() {
					var targetTop = $j(target).offset().top -12;
					$j('html,body').animate({ scrollTop: targetTop }, { duration: '1000', queue: false });
				});
			});
		} else {
			$j(target).slideToggle();
		};
		return false;
	});
	
	// Slide outs - close
	$j('.close').click(function(){
		var target = '#'+$j(this).parent().attr('id');
		if (target == '#gallery' || target == '#videos') {
			$j(target).slideUp('slow', function() {
				$j('html,body').animate({ scrollTop: $j('#content').offset().top }, { duration: '1000', queue: false });
				$j('.intro').fadeIn('slow');
			});
		} else {
			$j(target).slideToggle();
		};
		return false;
	});
	
	
	// Gift Message
	$j('.giftbag p.opt, .giftaid').hide();
	$j('.product-gift-voucher .giftbag p.opt').show();
	
	$j('.giftbag input[type=checkbox]').click(function(){	

		var checked = $j(this).attr('checked');
		if (checked == false) {
			$j('.giftbag p.opt').slideUp('slow', function(){ 
				$j('.giftbag p.opt textarea').val('');
			});
		} else {
			$j('.giftbag p.opt').slideDown('slow');
		}
		
	});
	
	// TAMBA / Giftaid
	$j('.help_giftaid, .giftaid').hover(function(){	
		$j('.giftaid').slideToggle('slow');
		return false;
	});
	
	// GIFT MATCH - Show on check
	$j('.gift-aid').hide();
	$j('.match input[type=checkbox]').click(function(){
		var checked = $j(this).attr('checked');
		if (checked == false) {
			$j('.gift-aid').hide();
		} else {
			$j('.gift-aid').show();
		}
	});
	
	// Equal Height Boxes
	
	
	// Gallery
	$j('.thumbs img').click(function () {
		var gallerysrc = $j(this).attr('src');
		stop();
		$j('#gallery #main-image').fadeOut('fast', function() {
			$j('#gallery #main-image').attr('src', gallerysrc);
			$j('#gallery #main-image').fadeIn('slow');
		});
	});
	
	// Video gallery
	$j('#videos div.wrapper').css('display','block');	
	$j('#video2').hide();
	$j('#videos p.links a').click(function() {
		var videolink = "#" + $j(this).attr("class");
		$j('iframe').stop().fadeOut(500, function () { 
		 	$j(videolink).fadeIn(1000);		  
		});
		return false;
	});
	
	// Gift message on cart page / truncated items
	$j('.truncated span.truncated_full_value').hide();
	$j('.truncated a.dots').click(function(){
		$j('.truncated span.truncated_full_value').show();
		$j('.truncated span.value').hide();
		return false;
	});
	
	// Equal Heights
	equalHeights = function (group) {
		$j(group).each(function(i){
				var currentTallest = 0;
				// Get Tallest
				$j(this).children().children('div.inner').each(function(x){
					if( $j(this).height() > currentTallest) {
						currentTallest = $j(this).height();
					}
				});
				// Set Tallest
				$j(this).children().children('div.inner').each(function(x){
					$j(this).css('height',currentTallest);
				});
		});
	}
	equalHeights('.ehb');
	
	// Sage code
        if($('sagecode')&&$('billing:firstname')&&$('billing:lastname')&&$('billing:telephone'))
        {
            $('sagecode').up('.field-row').hide();
            generateSageCode();
            Event.observe($('billing:firstname'), 'change', function(e)     {
                generateSageCode();
            }, false);
            Event.observe($('billing:lastname'), 'change', function(e)     {
                generateSageCode();
            }, false);
            Event.observe($('billing:telephone'), 'change', function(e)     {
                generateSageCode();
            }, false);
        }
});

function generateSageCode()
{
    if($('sagecode')&&$('billing:firstname')&&$('billing:lastname')&&$('billing:telephone'))
    {
        if($('sagecode').getValue()==""&&$('billing:firstname').getValue()!=""&&$('billing:lastname').getValue()!=""&&$('billing:telephone').getValue()!="")
        {
            firstname_part = $('billing:firstname').getValue().substr(0,1);
            lastname_part = $('billing:lastname').getValue().substr(0,4);
            while(lastname_part.length<4) {
                lastname_part = '_' + lastname_part;
            }
            if($('billing:telephone').getValue().length<3)
                telephone_part = $('billing:telephone').getValue();
            telephone_part = $('billing:telephone').getValue().substr($('billing:telephone').getValue().length-3,3);
            
            while(telephone_part.length<3) {
                telephone_part = '_' + telephone_part;
            }
            
            $('sagecode').setValue(firstname_part + lastname_part + telephone_part);
            // alert('sage code ' + firstname_part + lastname_part + telephone_part)
        }
    }
}

remove_price = /\s{1}\+?\-?£[0-9]+.{1}[0-9]{2}$/;

function updateForm(config)
{
        if($('mediagallery')&&$('attribute80')&&$('largeproduct')) {
            $('mediagallery').select('.procuctthumbs img').each(function (s) {
                Event.observe(s, 'click', function(e)     {
                    changeColour(s.getAttribute('alt'));
                    src = s.getAttribute('src');
                    $('largeproduct').fade({ duration: 0.35, afterFinish: function() {
                        $('largeproduct').writeAttribute('src', src);
                        $('largeproduct').appear({duration: 0.75});
                    }});
                }, false);
            });
        }

        function changeColour(colour)
        {
            var options = $$('#attribute80 option');
            for(var i=0;i<options.length;i++) {  
                if(options[i].text.replace(remove_price, '')==colour) $('attribute80').setValue(options[i].value);
            }
            config.configureElement($('attribute80'));
        }
}

function updateImage()
{
        if($('mediagallery')&&$('attribute80')&&$('largeproduct')) {
            activeOption = $('attribute80').getValue();
            options = $$('#attribute80 option');
            for(var i=0;i<options.length;i++) {
                if(options[i].value==activeOption) colour = options[i].text.replace(remove_price, '');
            }
        }

        $('mediagallery').select('.procuctthumbs img').each(function (s) {
            if(s.getAttribute('alt')==colour&&$('largeproduct').getAttribute('src')!=s.getAttribute('src')) {
                src = s.getAttribute('src');
                $('largeproduct').fade({ duration: 0.35, afterFinish: function() {
                    $('largeproduct').writeAttribute('src', src);
                    $('largeproduct').appear({duration: 0.75});
                }});
            }
        });
}
