$(document).ready(function() {
	
	$("#tandc").live('click', function() {
		
		$('#terms-holder').slideToggle('slow');
		$('html,body').animate({scrollTop: $('#terms-holder').offset().top},'fast');

		return false;
	});
	
	/* Handle Popups for compeitions */
	$( "#popups" ).dialog({
		modal:true, 
		width:750, 
		height:306, 
		draggable:false, 
		resizable:false,
		buttons: {
				
				"No thanks": function() {
					
					var self = $( this );
					var params = new Object();
					var banner_id = $('.popuplink').attr('id');
					var banner_id_arr = banner_id.split("-");
					params.banner_id = banner_id_arr[1];
					
					$.ajax({
						type: "POST",
						url: '/default/customer/competition/',
						data: params,
						success: function(response) {
							
							self.dialog( "close" );
						}
					});
					
					//
					
				},
				"Show me more": function() {
					
					
					var href = $( "#popups a:first").attr('href');
					document.location.href = href;
					return false;
				}
			},
		open: function(event, ui) {
			 
			  // Get the dialog 
				var dialog = $(event.target).parents(".ui-dialog.ui-widget"); 

				// Get the buttons 
				var buttons = dialog.find(".ui-dialog-buttonpane").find("button"); 


			 
				 var cancelButton = buttons[0]; 
				 var moreButton = buttons[1]; 
				 
				  //$(cancelButton).addClass("cancel"); 
                  $(moreButton).addClass("more"); 

				 

		}
		
		});
	
	$('#signup-newsletter').live('click', function() {
		var params = new Object();
		params.email = $('#newsletter-email').val();
		
		if(params.email == 'Email Address') {
			params.email = '';
		}
		
		$.ajax({
			type: "POST",
			url: '/default/customer/newsletter/',
			data: params,
			success: function(response) {
				
				if (response == 'SUCCESS') {
					$('#newsletter-email').val('');
					$('#newsletter-msg').html('<p class="nm-msg nm-success">Success!</p>');
				} else {
					if (response == 'EXISTS') {
						$('#newsletter-msg').html('<p class="nm-msg nm-warning">Already Registered</p>');
					}
					
					if (response == 'INVALID') {
						$('#newsletter-msg').html('<p class="nm-msg nm-warning">Invalid Email</p>');
					}
					
				}
			}
		});
	});
	
	$( "#tabs" ).tabs();
	
	$('.fm-minibasket-summary').css('cursor', 'pointer');
	$('.fm-minibasket-summary').live('click', function() {
		window.location.href = '/basket/';
	});
	
	
	$('#keyword').focus(function() {
		if($(this).val() == 'Search') {
			$(this).val('');
		}
		
		$(this).addClass('active');
	});
	
	$('#keyword').blur(function() {
		if($(this).val() == '') {
			$(this).val('Search');
			$(this).removeClass('active');
		}
		
		
	});
	
	
	$('.minisearch-btn').click(function(){
		var btn = $(this);
		var key = $('#keyword').val();
		
		if(key == 'Search') {
			return false;
		}
		return true;
	});
	
	
	$('#newsletter-email').focus(function() {
		if($(this).val() == 'Email Address') {
			$(this).val('');
		}
		
		$(this).addClass('active');
	});
	
	$('#newsletter-email').blur(function() {
		if($(this).val() == '') {
			$(this).val('Email Address');
			$(this).removeClass('active');
		}
		
		
	});
	
	
	$('.click-row').live('mouseover mouseout click', function(event) {
	  if (event.type == 'mouseover') {
		$(this).addClass('active');
		
		
               $(this).find('.offer')
                .animate({top:"-6px"}, 200).animate({top:"0px"}, 200);;
            
				$(this).find('.sale')
                .animate({top:"-6px"}, 200).animate({top:"0px"}, 200);
				
				$(this).find('.new')
                .animate({top:"-6px"}, 200).animate({top:"0px"}, 200);
		
	  } else {
		$(this).removeClass('active');
		if(event.type == 'click') {
			var href = $(this).find('a:first').attr('href');
			
			window.location.href = href;
			return false;
		}  
	  }
	});
	
	startBanners();
	

	
	
	function megaHoverOver(){
		
		$(this).addClass('active');
		
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
		$(this).find(".sub-brands").stop().fadeTo('fast', 1).show();
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".sub-brands").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			$(this).find(".sub-brands").css({'width' : rowWidth});
		}
	}
	
	function megaHoverOut(){
		$(this).removeClass('active');
		$(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide();
		});
		$(this).find(".sub-brands").stop().fadeTo('fast', 0, function() {
		  $(this).hide();
		});
		
		
	}
 
 
	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 200, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
 
	$("ul.fm-nav-tree li .sub").css({'opacity':'0'});
	$("ul.fm-nav-tree li").hoverIntent(config);
 
 	
	$('#fm-minibasket').fmBasket();

});


function startBanners() {
	
	$('.fm-banners').cycle({ 
		fx:     'fade', 
		speed:  'fast', 
		timeout: 6000, 
		pager:  '.banner-nav-inner',
		next:   '#next2', 
		prev:   '#prev2' 
	
	});
	
	$('.fm-widget-cycle').cycle({ 
		fx:     'fade', 
		speed:  'fast', 
		timeout: 12000
	
	});
	
	


}
