jQuery(window).load( function(){
	new SlidePanel("SlidingInfoLiner", "slideItem", 245, 385, 400);
	new SlideNews("NewsHolderLiner", "newsItem", "LeftToggle", "RightToggle", 3, false);
	//new Personalise("HeroImageHolder", "PersonaliseButton");

    //DFH 2010-06-18 Fix a bug where you have to click twice on any link before it loads, if it is located within a sliding panel.
	$('div.slideItem A').click(function(ev) {
	    ev.stopPropagation();
	});
});

function SlidePanel(containerID, panelClass, defaultWidth, activeWidth, duration) {

	this.init = function(){		
		var that = this;
		this.containerID = containerID;
		this.container = jQuery("#"+containerID);
		this.panels = this.container.children("div." + panelClass);
		this.panelsLength = this.panels.length;
		this.defaultWidth = defaultWidth;
		this.activeWidth = activeWidth
		this.inactiveWidth = ((this.panelsLength * this.defaultWidth) - (this.activeWidth)) / (this.panelsLength - 1);
		this.maxContainerWidth = this.panelsLength * this.activeWidth;
		this.duration = duration;
		this.holdAnimation = false;
		this.heldIndex = false;
		this.setWidth();			
		this.setEventHandlers();
	}
	
	this.setWidth = function(){
		this.container.css("width", this.maxContainerWidth+"px")
	}

	this.animateIt = function(panel, width, override) {

	    if (!this.holdAnimation || override) {
	        panel.animate(
				{
				    width: width + "px"
				},
				{
				    queue: false,
				    duration: this.duration,
				    complete: function() {
				        //$('div#MyGolfLogin').css('position', 'static').css('left','').css('top','');
				    }
				}
			);
	    }
	}
	
	this.defaultState = function(){
	    var that = this;

//	    var pos = $('div#MyGolfLogin').position();
//	    $('div#MyGolfLogin').css('position', 'absolute').css({ top: pos.top, left: pos.left });

		that.panels.each(function(i){
			var panel = jQuery(this);
			that.animateIt(panel, that.defaultWidth);
		});				
	}
	
	this.resetState = function(){
		this.heldIndex = false;		
		this.holdAnimation = false;			
	}
	
	this.animateToActive = function(index, override){
	    var that = this;
    	
//    	var pos = $('div#MyGolfLogin').position();
//	    $('div#MyGolfLogin').css('position', 'absolute').css({ top: pos.top, left: pos.left });

		that.panels.each(function(indexToCompare){
			var panel = jQuery(this);
			if(indexToCompare == index){	
				that.animateIt(panel, that.activeWidth, override);
			}	else {
				that.animateIt(panel, that.inactiveWidth, override);
			}			
		});				
	}

	this.setEventHandlers = function() {
	    var that = this;
	    jQuery(document).click(function(event) {
	        var target = jQuery(event.target);
	        var parents = target.parents();
	        if (!parents.filter("#" + that.containerID).length) {
	            that.resetState();
	            that.defaultState();
	        }
	    });
	    that.container.bind("mouseleave", function() {
	        that.defaultState();
	    });
	    that.panels.each(function(index) {
	        jQuery(this).click(function(event) {
	            if (index !== that.heldIndex) {
	                event.preventDefault();
	                that.holdAnimation = true;
	                that.heldIndex = index;
	                that.animateToActive(index, true);
	            } else {
	                that.resetState();
	            }
	        });
	        jQuery(this).hover(
				function() {
				    that.animateToActive(index);
				},
				function() {
				}
			);
	    });
	}
	
	this.init();
}
function Personalise(containerID, buttonID) {

	this.init = function(){		
		var that = this;
		this.button = jQuery("#"+buttonID);
		this.close = jQuery("#BttnClose");
		this.thumbholder = jQuery("#PersonaliseThumbHolder");
		this.thumbs = jQuery("div.thumb").children('a');
		this.hero = jQuery('.heroImage');
		this.containerID = containerID;
		this.container = jQuery("#"+containerID);
		this.cookie = 'heroImage';
		this.containerState = false;
		
		this.thumbsArray = new Array();
		this.herosArray = new Array();
		
		this.thumbs.each(function(i){
			that.thumbsArray[i] = jQuery(this);
			that.herosArray[i] = jQuery(this).attr('rel');	
		});
		
		this.setEventHandlers();
	}
	
	this.animateToActive = function(){
		var that = this;
		
		this.button.fadeOut(function(){
			that.close.fadeIn();	
		});
		
		this.thumbholder.css({'top':'-74px','opacity':0,'display':'block'});
    this.thumbholder.animate({ 
      top: "1",
			opacity: 1
    }, 600 );
    
		
		this.containerState = true;
	}
	
	this.animateToInactive = function(){
		var that = this;
		this.close.fadeOut(function(){
			that.button.fadeIn();	
		});
		
    this.thumbholder.animate({ 
      top: "-74px",
			opacity: 0
    }, 600 );
		
		this.containerState = false;
	}
	
	this.togglePersonalise = function(){
		if(this.containerState){
			this.animateToInactive();
		} else {
			this.animateToActive();			
		}
	}
	
	this.changeImage = function(index, URL){
		var that = this;
		that.hero.css('height','292px').addClass('loading');
		that.hero.children('img').fadeOut(1000,function(){
		that.hero.image(URL, that);
		jQuery.cookie(that.cookie, URL, {expires: 365 });
		});	
	}
	
	this.setEventHandlers = function() {
		var that = this;
		that.button.click(function(){
			that.togglePersonalise();
		});
		that.close.click(function(event){
			event.preventDefault();
			that.togglePersonalise();
		});
		
		this.thumbs.each(function(i){
			jQuery(this).click(function(event){
				event.preventDefault();
				var url = jQuery(this).attr('rel');
				that.changeImage(i, url);
			});
		});
	}
	
	this.init();
}

jQuery.fn.image = function(src, _this){
	return this.each(function(){
		var i = new Image();
		i.src = src;
		jQuery(i).css('display','none');
		jQuery(this).html(i);
		if (!jQuery.browser.msie) 
			i.onload = fadeImage(jQuery(this), _this);
		else
			i.load = fadeImage(jQuery(this), _this);
					
		jQuery(this).removeClass('loading');
	});
}

function fadeImage(el,_this){
	el.children('img').fadeIn(
		1000,
		function(){
			_this.togglePersonalise();
		}
	);
}


/*
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
*/


//hovers on the pro shop products
$(document).ready(function() {
    $('div#TheProShop div.productthumb div.thumb').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });
    $('div#TheProShop').hover(function() {
        $('#img_pro_shop_now_open').fadeOut();
    }, function() {
        $('#img_pro_shop_now_open').fadeIn();
    });
});