$().ready(function(){
	
	/* hover function use class hover */
	$(function() {
		$('.rollover').hover(function() {
			var currentImg = $(this).attr('src');
			$(this).attr('src', $(this).attr('hover'));
			$(this).attr('hover', currentImg);
		}, function() {
			var currentImg = $(this).attr('src');
			$(this).attr('src', $(this).attr('hover'));
			$(this).attr('hover', currentImg);
		});
	});

	/* pulldown */
	function resetHover(){
		$('#contact').find('img').attr('src','/images/contact-us.png'); 
		$('#contact').find('img').attr('hover','/images/contact-us_over.png');
		$('#question').find('img').attr('src','/images/ask-us-a-question.png'); 
		$('#question').find('img').attr('hover','/images/ask-us-a-question_over.png');
		$('#question').find('img').removeClass('active-rollover');
		$('#contact').find('img').removeClass('active-rollover'); 
	}

	$('#question').click(function(e){
		e.preventDefault();
		$('.pulldown-contact').slideUp(function(){
			
			$('.pulldown-question').slideToggle(function(){
				if($(".pulldown-question").is(":visible")){
					resetHover();
					$('#question').find('img').addClass('active-rollover');
					$('#question').find('img').attr('src','/images/ask-us-a-question_active.png'); 
					$('#question').find('img').attr('hover','/images/ask-us-a-question_active.png');
				}else{
					resetHover();
				}
			});
		});
		
	});

	$('#contact').click(function(e){
		e.preventDefault();
		$('.pulldown-question').slideUp(function(){			

			$('.pulldown-contact').slideToggle(function(){
				if($(".pulldown-contact").is(":visible")){
					resetHover();
					$('#contact').find('img').addClass('active-rollover');
					$('#contact').find('img').attr('src','/images/contact-us_active.png'); 
					$('#contact').find('img').attr('hover','/images/contact-us_active.png');	
					$('#googlemap').attr('src',$('#googlemap').attr('src'));
				}else{
					resetHover();
				}
			});
		});
		
	});

	/* Nav submenus */
	$('.nav ul li').hover(function(){
		if($(this).find('.navHolder').length){
			$(this).find('.navHolder').show();
			$(this).find('a:first').addClass('stayHover');
			$(this).find('.navHolder').find('div:nth-child(4n)').css('border','0');
			$(this).find('.navHolder').find('div:last').css('border','0');

			var overall_width = 0;
			var i = 0;
			$(this).find('.navHolder').find('div').each(function(index, elem) {
				i++;
				if(i < 5){
					var $elem = $(elem);
					overall_width += $elem.outerWidth() + parseInt($elem.css('margin-left'), 10) + parseInt($elem.css('margin-right'), 10);
				}
			});
			$(this).find('.navHolder').width(overall_width);
		}
		
	},function(){
		if($(this).find('.navHolder').length){
			$(this).find('.navHolder').hide();
			$(this).find('a:first').removeClass('stayHover');
		}
	});

	/* Product image hover */
	$('.imageholder').find('.small').hover(function(){
			$(this).css('cursor', 'pointer');
			$('.imageholder').find('.large').attr('src',$(this).attr('rel'));
	});

	/* fancybox class */
	$('.fancybox').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	300, 
		'speedOut'		:	200, 
		'overlayShow'	:	true
	});

	/* colour choice/sizing*/
	if ($('#colour-choice').length > 0){
		$('#size-holder').html($('#'+$('#colour-choice :selected').attr('rel')).html());
	}

	$('#colour-choice').change(function(){
		$('#size-holder').html($('#'+$('#colour-choice :selected').attr('rel')).html());
	});

	$('#colour-choice2').change(function(){
		$('.totalprice').html('&pound;'+$('#colour-choice2 :selected').attr('rel')+' <span>Incl VAT</span>');
		$('.hiddenprice').val($('#colour-choice2 :selected').attr('rel'));
	});

	$('#size-holder').click(function(){
		if($('#size-holder').find('select :selected').attr('rel') > 0 ){
			$('.totalprice').html('&pound;'+$('#size-holder').find('select :selected').attr('rel')+' <span>Incl VAT</span>');
			$('.hiddenprice').val($('#size-holder').find('select :selected').attr('rel'));
		}

	});

	/* homepage cycle*/
	if ($('.itemHold').length > 0){
		$('.slides').cycle({ 
			fx:    'fade', 
			sync:   1, 
			delay: 4000,
			pause:   1,
			pager: '.slidesNav'
		});	
		$('.cy1').cycle({ 
			fx:    'scrollLeft', 
			sync:   1, 
			pause:   1,
			next: '.cy1next',
			prev: '.cy1prev'
		});	

		$('.cy2').cycle({ 
			fx:    'scrollLeft', 
			sync:   1, 
			pause:   1,
			next: '.cy2next',
			prev: '.cy2prev'
		});
		$('.cy1').cycle('pause');
		$('.cy2').cycle('pause');
	}

	/* apply styling to refine checkboxes */

	$('#refineForm').find('input[type=checkbox]').customInput();

	/* ajax shop refine */
	
	$('#refineForm').find('input[type=checkbox]').click(function(e){
		//e.preventDefault();
		$('.shop-content').fadeOut(function(){
			$('.shop-content').html('..Loading');
			$.post("/ajax-search.php", $('#refineForm').serialize() , function(data) {
				$('.shop-content').html(data);
				$('.shop-content').fadeIn('slow');
			});
			
		});
	});

	$('#refineForm').find('h3').click(function(){
		$(this).find('div.refine').toggle();
	});

	// URL ENCODE DECODE
		$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
	  while(x<c.length){var m=r.exec(c.substr(x));
		if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
		}else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
		o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
	URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
	  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
	  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
	});


	$('#comm').keyup(function(){
		$('#commlink').attr('href','http://twitter.com/home?status=' + $.URLEncode($('#comm').val()));
	});

	// Placeholder
	$('input[placeholder]').each(function(){
	    $inputs = $(this);
	    $inputs.data('placeholder',$inputs.val());
	     
	    $inputs.focus(function () {
	        var $input = $(this);
	        if ($input.val() === $input.data('placeholder')) {
	            $input.val();  	        }
	    }).blur(function () {
	        var $input = $(this);
			if ($input.val() === '') {
	            $input.val($input.data('placeholder'));
	        }
	    });
	});

	
		

});
