jQuery(document).ready(function(){ 
	$('#carousel').jcarousel({
		scroll : 2
	});
	
	// dropdown menu
	$('#mainmenu ul.sf-menu').superfish({
		autoArrows  : false,
		dropShadows : false,
		animation   : {opacity:'show', height:'show'},
		speed       : 'fast',
		onInit      : doStufftoMenu
	}); 
	
	
	function doStufftoMenu(){
		// add bottom rounded corner to last li
		$('#mainmenu li li:last-child').addClass("last-child"),
		$('#mainmenu li li:last-child').append("<div class='wrap'><!----><\/div>");		
		// add span to first level menu item links
		$('#mainmenu ul:first-child > li a').wrap("<span><\/span>");		
		// make last first level menu item drop-down align to the right
		$('.sf-menu li:last-child ul').css({ left: 'auto', right: '0', background: 'url(images/menu-level2_bg_flip.gif)' });
		$('.sf-menu li:last-child ul li.last-child .wrap').css({ background: 'url(images/menu-level2_bg_flip.gif) left bottom no-repeat' });
	}
	
	// button colours
	var n = Math.ceil( $(".default_but").length / 4 );		
	if(n>=1){
		for (i=0; i < n; i++){
			itt = (i*4);
			$(".default_but:eq("+(0+itt)+")").addClass("pink_but");
			$(".default_but:eq("+(1+itt)+")").addClass("blue_but");
			$(".default_but:eq("+(2+itt)+")").addClass("yellow_but");
			$(".default_but:eq("+(3+itt)+")").addClass("navy_but");
		}
	}
	
	// hide readmore text and show readmore button
	$('.list_expand .hidden').hide();
	$('.list_expand .default_but').show();
	
	// slide toggle on readmore click
	/*$('.list_expand .default_but span').addId('linktext');*/
	$('.list_expand .default_but').toggle(function(){
		$(this).prev('.hidden').slideDown();
		$('span', this).html("Close");
	},function(){
		$(this).prev('.hidden').slideUp();
		$('span', this).html("Read more");
	});	
	
	
	// explore slider
	
	/* add fake slider container graphic 
	$('div.sliderContainer').css({ background:'url(images/slider/slider_bg.gif) center 298px no-repeat' })
	
	$('div.sliderGallery').each(function () {
		var ul = $('ul', this);
		var productWidth = ul.innerWidth() - $(this).outerWidth();
		
		var slider = $('.slider', this).slider({
		  handle: '.ui-slider-handle',
		  min: 0, 
		  max: productWidth,
		  value: 50,
		  slide: function (event, ui) {
			ul.css('left', '-' + ui.value + 'px');
		  }	
		});		
  	});
	*/
	
	// accordion
	$("#branches").accordion({
		header: 'h2',
		autoHeight: false
	});			
	
	// ******************************************************************************* //
	// TOOLTIPS CODE MUST APPEAR BEFORE FANCYBOX CODE, ELSE CONFLICTS!!! (no clue why)
	// ******************************************************************************* //
	
	// tooltips	
	
	jQuery("#management_team a.tooltip").tooltip({ 
			track: true, 
			delay: 0, 
			showURL: false, 
			fixPNG: true, 
			showBody: " | ", 
			extraClass: "tooltip_pretty" 
	});
	
	jQuery(".similar_brands li a").tooltip({ 
			track: true, 
			delay: 0, 
			showURL: false, 
			fixPNG: true, 
			extraClass: "tooltip_brands" 
	});	
	
	
	// fancybox
	$(".ad_listings a.thumbLink").fancybox({
		'padding': 0,
		'frameWidth': 470,
		'frameHeight': 407,
		'overlayOpacity': 0.8,
		'zoomOpacity': false,
		'showTitle': false, 
		'hideOnContentClick': false,
		'callbackOnClose': function() { 
                $("#fancy_content").empty(); 
                } 
	});
	

	$(document).pngFix();
	
	
});

$(window).load(function () {
	
	// explore slider	
	if ( $('.sliderGallery').length > 0 ) {	
	
	/* add fake slider container graphic */
	$('div.sliderContainer').css({ background:'url(images/slider/slider_bg.gif) center 298px no-repeat' })
		
				
		var pic_real_width;
		var pic_real_height;
		
		$('.sliderGallery img').each(function () {
			$(this).removeAttr("width")
			   .removeAttr("height");
			
			pic_real_width = this.width;
			pic_real_height = this.height;
			
			
			$(this).attr({ 
					width: pic_real_width,
					height: pic_real_height
			});
			
			
		});		
		
			
		$('div.sliderGallery').each(function () {
			var ul = $('ul', this);
			var productWidth = ul.innerWidth() - $(this).outerWidth();
			
			//alert(productWidth);
			
			var slider = $('.slider', this).slider({
			  handle: '.ui-slider-handle',
			  min: 0, 
			  max: productWidth,
			  value: 50,
			  slide: function (event, ui) {
				ul.css('left', '-' + ui.value + 'px');
			  }	
			});		
		});		
										   
										   
	}
});
