//Team Sky base functions

//execute any functions inside basescript tags
var basescript = document.getElementById("basescript");
if( basescript ){eval( basescript.innerHTML )};


base = {
		flashversion:function(){
		
			//check for flash versions
			var isInstalled = false;
			var version = null;
			if (window.ActiveXObject) {
				var control = null;
					try {
						control = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
					
					} catch (e) {
						version=0
					
		
				}
		
				if (control) {
					isInstalled = true;
					version = control.GetVariable('jQueryversion').substring(4);
					version = version.split(',');
					version = parseFloat(version[0] + '.' + version[1]);
				
				}

			
		} 
	
	else {
		if(navigator.plugins['Shockwave Flash']){
		    version = navigator.plugins['Shockwave Flash'].description;
			version = version.split('.')[0];
			version = parseInt(version.split('Flash ')[1]);
		}
		
		else{version=0}
		
		}

		window.flashversion = version;
		
		
		
		
		
		},
		nav:function(){
	
		jQuery('.nav-enable').each(function(){
			
			
			var thisnav = jQuery(this);
			
			//default options
			var Nanim  = false;
			var Nspeed = 300;
			var Noffspeed = 100;
			var Nheight = 200;
			var Nlowlight = false;
			
			
			if(jQuery(this).attr('options')){
				
				var options = jQuery(this).attr('options');
				options = options.split("&");
				
				jQuery(options).each(function(){
					
					var opts = this.split('=');
						
					if(opts[0]=='animate'){Nanim=opts[1]}
					if(opts[0]=='offspeed'){Noffspeed=parseInt(opts[1])}
					if(opts[0]=='speed'){Nspeed=parseInt(opts[1])}
					if(opts[0]=='height'){Nheight=parseInt(opts[1])}
					if(opts[0]=='lowlight'){Nlowlight=opts[1]}
						
				});
			}
			
			var bodywidth = jQuery(this).width();
		
			jQuery(this).find('.base-nav-item').each(function(){
			
				/*var offsetleft = jQuery(this).position().left;
			
				var width = jQuery(this).find('.base-nav-drop').width();
				
				var overlap = (offsetleft+width)-bodywidth;
		
				if(overlap>0){jQuery(this).find('.base-nav-drop').css({marginLeft:(overlap*-1)})}
				*/
				
				if(Nanim=='slide'){
					
					var h = jQuery(this).find('.base-nav-drop').height();
					jQuery(this).find('.base-nav-drop').attr('realheight',h).css({height:0,overflow:'hidden'});
				
				}
				
				
			})
			
			
			
			if(Nanim=='slide'){
				
				jQuery(this).find('.base-nav-drop').css({height:0,overflow:'hidden'})
				
				
				//mouseover
				jQuery(this).find(".base-nav-item").hover(function(){
					
					if(Nlowlight!=false){
						jQuery(thisnav).find('.base-nav-item').children('a').addClass('off');
						clearTimeout(window.settempnav);
					}
					
					if(jQuery(this).hasClass('on')){jQuery(this).addClass('orig')}
					
					jQuery(this).addClass('on').children('a').removeClass('off');
					
					Nheight = jQuery(this).find('.base-nav-drop').attr('realheight');
					jQuery(this).find('.base-nav-drop').stop().animate({height:Nheight},{queue:false, duration:Nspeed})
					
					
				},function(){
				
						window.tempnav = jQuery(thisnav);
						window.settempnav = setTimeout("window.tempnav.find('.base-nav-item a').removeClass('off')",200);
					
					
					if(!jQuery(this).hasClass('orig')){jQuery(this).removeClass('on');}
					
					jQuery(this).find('.base-nav-drop').stop().animate({height:'0px'},{queue:false, duration:(Noffspeed+1),complete:function(){jQuery(this).hide()}})
				
				
				});
		
				
			}
			
			
			if(Nanim==false || Nanim == 'undefined' || Nanim=='off'){
				
				jQuery(this).find(".base-nav-item").hover(function(){
				
					if(Nlowlight!=false){
						jQuery(thisnav).find('.base-nav-item').children('a').addClass('off');
						clearTimeout(window.settempnav);
					}
					
					if(jQuery(this).hasClass('on')){jQuery(this).addClass('orig')}
					jQuery(this).addClass('on').children('a').removeClass('off');
					jQuery(this).addClass('on');
					jQuery(this).find('.base-nav-drop').show();
				
				
				},function(){
				
					window.tempnav = jQuery(thisnav);
					window.settempnav = setTimeout("window.tempnav.find('.base-nav-item a').removeClass('off')",200);
						
					if(jQuery(this).hasClass('orig')==false){
						jQuery(this).removeClass('on');
					}
					
					else{
						jQuery(this).removeClass('orig');
					}
					
					jQuery(this).find('.base-nav-drop').hide();
				
				
				});
			
			
			}
			
			jQuery(this).children().children('li').children('.base-nav-link').click(function(){
				
				if(jQuery(this).attr('href')=='#'){ return false }
			})
		
		
		})
		
		
	},
	
	racesDrop:function(){
	
		jQuery('.races-breadcrumb-item').hover(function(){
		
			jQuery(this).find('.base-races-dropdown').show()
		
		
		
		},function(){
		
			jQuery(this).find('.base-races-dropdown').hide()
		
		
		})
	
	
	
	
	
	},
	
	desaturate:{
	
		init:function(){
		if(jQuery('.pic')[0]){
			jQuery.getScript(window.basescriptpath+"pixastic.js", function(){
				
				jQuery('.pic').each(function(){
					
					var img = jQuery(this).find('.photo').clone();
					img.removeClass('photo').addClass('photoalt').css({display:'none'});
					jQuery(this).find('a').append(img);
					
					var img = Pixastic.process(jQuery(this).find('img')[0], "desaturate");
					
					jQuery('.pic').css('visibility','visible');
					
					jQuery('.pic').parent().hover(function(){
					
						jQuery(this).find('.photo').hide();
						jQuery(this).find('.photoalt').show();
					
					},function(){
					
						jQuery(this).find('.photoalt').hide();
						jQuery(this).find('.photo').show();
					
					})
					//base.desaturate.write(this)
					
					})
			
				})
			
			}
		
		}

	},
	
	galleryMini:function(p){
		
			jQuery('.base-gallery-enable').each(function(i){
			
				var el = jQuery(this);
				
				//set up options
				var Gtooltip = true;
				var Ggallerylink = true;
				var Gthickbox = false;
				var Gimagepath = '496x259';
				
				
				if(jQuery(this).attr('options')){
					
					var options = jQuery(this).attr('options');
					options = options.split("&");
				
					jQuery(options).each(function(){
						
						var opts = this.split('=');
						if(opts[0]=='tooltip'){Gtooltip=opts[1]}
						if(opts[0]=='gallerylink'){Ggallerylink=opts[1]}
						if(opts[0]=='thickbox'){Gthickbox=opts[1];Gtooltip=false;Ggallerylink=false}
						if(opts[0]=='imagepath'){Gimagepath=opts[1];}
						
					});
				
				}
				
				
				//if we have thumbnails, change the width of caption area.
				if(el.find('.base-gallery-thumbs li').length>0){el.find('.base-text-caption').addClass('withthumbs');}
				
				
				el.find('.base-gallery-thumbs li').click(function(){
					
					el.find('.base-gallery-thumbs li').removeClass('on');
					jQuery(this).addClass('on');
					
					var link = jQuery(this).find('a').attr('href');
					var tool = jQuery(this).find('a').hasClass('tool-tip');
					var title = jQuery(this).find('a').attr('title') || jQuery(this).find('a').attr('oldtitle');
				
					var im = jQuery(this).find('img').attr('src');
					var match = /\/\d{2,3}x\d{2,3}\//gi
					newim = im.replace(match,"/"+Gimagepath+"/");
				
				
					el.find('.base-gallery-pic span').image(newim,function(){
						
						el.find('.base-gallery-pic span').fadeIn(500,function(){
							el.find('.base-text-caption').html(title);
							el.find('.base-gallery-pic').html("<span></span><img src='"+newim+"' />");
							
							if(Ggallerylink==true){el.find('.base-gallery-pic img').wrap("<a href='"+link+"'></a>")}
							
							if(tool==true && Gtooltip==true){
								
								el.find('.base-gallery-pic a').addClass('tool-tip').attr('title',title);
								el.find('.base-gallery-pic a').tooltip({ 
									track: true, 
									delay: 0, 
									showURL: false, 
									fade: 250
						
								});
							
							}
							
							if(Gthickbox=='true'){
								
								el.find('.base-gallery-pic img').wrap("<a href='"+link+"' class='thickbox'></a>");
								el.find('.base-gallery-pic a').click(function(){tb_show(title, this.href);return false})
							
							
							}
							
						})
					
					})

					
					return false;
				
				})
				
			
			})
	
		
	
	},
	
	tooltip:function(p){
		var el = jQuery('.tool-tip');
		//if(p){el=p}
	
		if(jQuery('.tool-tip')[0]){
			jQuery.getScript(window.basescriptpath+"tooltip.js", function(){
				
				jQuery(function(){
					el.tooltip({ 
						track: true, 
						delay: 0, 
						showURL: false, 
						fade: 250
						
					});
				})
			
			})
		}
	},
	
	validation:function(){
		
		if(jQuery('form.validator-enable')[0]){
			
			jQuery.getScript(window.basescriptpath+"validationaide.js", function(){
				
				jQuery("form.validator-enable").prepend('<div id="ClientValidationSummary" style="display:none"></div>');
				jQuery("form.validator-enable").validationAideEnable();
				
				
			});

		
		}

	
	
	
	
	},
	
	stuff:function(){
	
		//article tools hover
		jQuery('.base-article-tools li').hover(function(){
			
			jQuery('.base-article-tools li a').addClass('off');
			jQuery(this).find('a').removeClass('off').parent().find('.base-article-tools-panel').show();
		
		},function(){
		
			jQuery(this).find('.base-article-tools-panel').hide();
			jQuery('.base-article-tools li a').removeClass('off');
		
		
		})
	
	
	
	
	},
	rating:function(){
		
		jQuery('.base-rating-enable').each(function(i){
			
			var rater = jQuery(this);
			var rateid = rater.attr('id');
		
			if(getCookie(rateid)){
				rater.find('.current-rating').width((parseInt(getCookie(rateid))*20)+"%");
				rater.find('h5').html('You rated');
		
			}
			
			else{
			
				rater.find('.base-rating-disable').hide();
				rater.find('li a').click(function(){
				
					var urate = rater.find('li').index(jQuery(this).parent());
					
					rater.find('.current-rating').width((urate*20)+"%");
					rater.find('h5').html('You rated');
					setCookie(rateid,urate);
					rater.find('.base-rating-disable').show();
					return false;
				
				})
			
			
			}
		
		})
	
	
	},
	
	tabs:function(){
	
		jQuery('.tabs-enable').each(function(i){
		
			var thistab = jQuery(this);
			
			
			//default options
			var Tremember  = false;
			var Tresize = false;
			
			
			if(jQuery(this).attr('options')){
				
				var options = jQuery(this).attr('options');
				options = options.split("&");
				
				jQuery(options).each(function(){
					
					var opts = this.split('=');
					if(opts[0]=='remember'){Tremember=opts[1]}
					if(opts[0]=='resize'){Tresize=opts[1]}
						
				});
				
			}
			
			if(Tresize=='true'){
				var tabheight = jQuery(thistab).find('.tab-anchors').height();
				jQuery(thistab).css({overflow:'hidden'})
				
				jQuery(thistab).find('.tab-section').each(function(){
					
					jQuery(this).attr('realheight',jQuery(this).height()).css({height:'100%'})
					
				
				})
			
			
			}
			
			jQuery(this).find('.tab-anchors li').click(function(){
		
				if(jQuery(this).find('a')[0]){
				
					if(jQuery(this).find('a').attr('href').match(/^\#/)==null){
					
						location.href=jQuery(this).find('a').attr('href');
						
					}			
				
					else{
					
						jQuery(thistab).find('.tab-anchors li').removeClass('on');
						jQuery(thistab).find('.tab-section').hide()
							
						setCookie(Tremember,jQuery(this).find('a').attr('href'));
						jQuery(this).addClass('on');
						
						
						if(jQuery(this).find('a').attr('href')){
							
							var h = parseInt(jQuery(jQuery(this).find('a').attr('href')).attr('realheight'));
							jQuery(jQuery(this).find('a').attr('href')).show();
							
							if(Tresize=='true'){
								
								jQuery(thistab).animate({height:h+tabheight+22},300)
							}
							return false;
							
						}

					}
				}
			
			})
			
			
			if(Tremember && getCookie(Tremember)){
				
				var toclick = getCookie(Tremember);
				jQuery(this).find(".tab-anchors li a[href='"+toclick+"']").parents('li').trigger('click');
				
			}
			
			else{
				if(!jQuery(this).find('.tab-anchors li.on')[0]){jQuery(this).find('.tab-anchors li:eq(0)').addClass('on')}
			}
			
		})
	
	},
	
	
	video:function(p){
	
		if(jQuery('#base-video')[0]){
		
			jQuery.getScript(window.basescriptpath+"video.js");
		
		
		}
	
	
	
	
	},
	
	flashText:function(){
		if(window.flashversion>7){
			
			jQuery('.render-flash-text').each(function(){
			
				var text = jQuery(this).text();
				text=text.replace(/<br>/ig,'');
				jQuery(this).text(text);
				var path = "/TeamSky/build/flash/headline.swf";
				
				if(jQuery(this).attr('options')){
				
				var options = jQuery(this).attr('options');
				options = options.split("&");
				
				jQuery(options).each(function(){
					
					var opts = this.split('=');
						
					if(opts[0]=='path'){path=opts[1]}
					
						
				});
				
			}
			
				text = escape(text);
				text = text.replace("%uFFFD",'&#200;');
				
				
				var color=jQuery(this).css('color');
				if(color == '#fff'){color='#ffffff'}
				
				if(color.search('#')!=-1){
					
					color = color.replace('#','');
					
					color = parseInt(color,16);
					
			
				}
				
				else if(color.search('rgb')!=-1){
				
					color = RGBToHex(color);
					color = parseInt(color,16);
				
				}
			
				
			
			
				var width = jQuery(this).width();
			
				var height = jQuery(this).height();
			
			
				var fontsize = jQuery(this).height();
				
				if(jQuery.browser.opera==true){
					fontsize = jQuery(this).css('font-size');
					height = jQuery(this).css('font-size');
				}
				
				var id = "text"+Math.floor(Math.random()*1001);
				
			
				
				//embed the flash
				jQuery(this).html('<object type="application/x-shockwave-flash" data="'+path+'" height="'+height+'" width="'+width+'" id="'+id+'"><param name="movie" value="'+path+'" /><param name="wmode" value="transparent" /><param name="AllowScriptAccess" value="always" /><param name="flashvars" value="textid='+id+'&headlinetext='+text+'&textcolor='+color+'&textsize='+fontsize+'&width='+width+'&height='+height+'" /></object>');
				
				jQuery(this).css({visibility:'visible'});
				
				
				
				
			})
	
		}
		
		else{jQuery('.render-flash-text').css({visibility:'visible','letter-spacing':0})}

	
	},
	
	schedule:function(){
	
		if(jQuery('#slider')[0]){
		
		jQuery.getScript(window.basescriptpath+'jquery-ui.js',function(){
		
		
		jQuery(function(){
		
		
		
			jQuery('#slider').slider({ step: 8.8,animate:true,slide: function(event, ui) { 

				var month = Math.ceil(ui.value/8.8);
				
				jQuery('.base-races-events').children('li').addClass('js-hdn')
				
				jQuery('.base-races-events').children('li:eq('+month+')').removeClass('js-hdn');
				
				

			 },stop:function(event,ui){
			 
				var month = Math.ceil(ui.value/8.8);
				location.hash="month="+(month+1);
				return false;
			 
			 
			 }});
			 
			 
			 if(location.hash.match('month')){
			 
				var month = location.hash.replace('#month=','');
				if(month>12){month=12}
				if(month<1){month=1}
				jQuery('#slider').slider( 'value' , [(month-1)*8.8] );
				
				jQuery('.base-races-events').children('li').addClass('js-hdn')
				jQuery('.base-races-events').children('li:eq('+(month-1)+')').removeClass('js-hdn');
				
				return false;
				
			}
			
			else{
			jQuery('.base-races-events').children('li:eq(0)').removeClass('js-hdn');
			
			}
			
			
			
			})
		
		})
		
		}
	
	
	},
	
	flashImage:function(){
		if(window.flashversion>7){
			jQuery('.pic').each(function(){
				
				var path = window.flashimagepath;
				
				var h = jQuery(this).height();
				var w = jQuery(this).width();
				
				if(h==0){
					h=jQuery(this).find('img').css('height');
					w=jQuery(this).find('img').css('width');
				}
				
			
				var linkel = jQuery(this).children('a');
				var link = linkel.attr('href');
				if(!link){link="";}
			
				var imgpath = jQuery(this).find('img').attr('src');
				var imageid = "image"+Math.floor(Math.random()*1000001);
				var alt = jQuery(this).children('a').attr('flashtitle'); //|| jQuery(this).children('a').children('img').attr('alt');
				if(!alt){alt=""}
		
				jQuery(this).html('<object id="'+imageid+'" style="z-index:1" type="application/x-shockwave-flash" data="'+path+'" height="'+h+'" width="'+w+'"><param name="movie" value="'+path+'" /><param name="wmode" value="transparent" /><param name="AllowScriptAccess" value="always" /><param name="flashvars" value="image='+imgpath+'&link='+link+'&alt='+alt+'&h='+h+'&w='+w+'" /></object>');
			
				
			
				//jQuery(this).parents('.ts-home-story').hover(function(){
					
					//getFlashOb(imageid).highlight(true)
				
				//},function(){
				
					//getFlashOb(imageid).highlight(false)
				
				
				
				//})
				
			
			})
		
		
		
		}
		
		else{
		
			jQuery('.pic').find('img').css({visibility:'visible'});
			
		}
	},
        productMenu : function(){
             jQuery(".p7TBMtext li a, .prod_cat_box a").click(function(){
                       var asco = jQuery(this).attr("href");
                       asco = asco.replace(/\//,"");
                       jQuery.cookie('linkhreF', asco);
             });
             jQuery(".p7TBMtext li a").each(function(){
             jQuery(this).removeClass('p7TBMdown');
             var getslash = jQuery(this).attr("href");
             getslash = getslash.replace(/\//,"");
                      if(getslash ==  jQuery.cookie('linkhreF')){
                           jQuery(this).addClass('p7TBMdown');
                      }
             });
        }
}
/*
jQuery('.footer li img').hover(function(){
	var src=jQuery(this).attr('src');
	jQuery(this).attr('oldsrc',src)
	var src2 = src.replace('.gif','-color.gif');
	jQuery(this).attr('src',src2);
},function(){
	var oldsrc = jQuery(this).attr('oldsrc');
	jQuery(this).attr('src',oldsrc);
})




function resolution(){

	var w = parseInt(screen.width)/parseInt(screen.height);
	var r;
	
	if(w>=1.6){
		if(screen.width<=1280){
		
			r = '1280x800';
		}
		
		else {
		
			r = '1440x900'
			
		}
		
		if(screen.width>1440){
	
			r = '1920x1080';
	
		}
		
	}
	
	else{
		
		if(screen.width<=1024){
		
			r = '1024x768';
		
		}
		
		else{
		
			r = '1280x1024';
		}
		
	
	}
	
	

	return r;
}

jQuery('.wallpaper .'+resolution()).addClass('active');

jQuery('.wallpaper span > a').click(function(){

	return false;

})
jQuery('.wallpaper-download').hover(function(){
	
	jQuery(this).find('.wallpaper-download-link').addClass('wallpaper-download-link-on');
	jQuery(this).find('ul').show();

},function(){
	jQuery(this).find('.wallpaper-download-link').removeClass('wallpaper-download-link-on');
	jQuery(this).find('ul').hide();

})



jQuery(".video-promo").hover(function(){
		jQuery(this).find('div').animate({top:126},{queue:false})
	
	},function(){
		jQuery(this).find('div').animate({top:162},{queue:false})
	
	})
	
*/


function RGBToHex(c){
	
	c = c.replace('rgb(','');
	c = c.replace(')','');
	
	c = c.split(',');

   var Red = parseInt(c[0]);
   var Green = parseInt(c[1]);
   var Blue = parseInt(c[2]);
   
 
   

   var a = GiveHex(Math.floor(Red / 16));
   var b = GiveHex(Red % 16);
   var c = GiveHex(Math.floor(Green / 16));
   var d = GiveHex(Green % 16);
   var e = GiveHex(Math.floor(Blue / 16));
   var f = GiveHex(Blue % 16);

   var z = a + b + c + d + e + f;

   return z;
}

	function GiveHex(Dec)
{
   if(Dec == 10)
      Value = "A";
   else
   if(Dec == 11)
      Value = "B";
   else
   if(Dec == 12)
      Value = "C";
   else
   if(Dec == 13)
      Value = "D";
   else
   if(Dec == 14)
      Value = "E";
   else
   if(Dec == 15)
      Value = "F";
   else
      Value = "" + Dec;
   return Value;
}


adjustheight = function(p){

	jQuery('#'+p[0]).css({height:p[1]+"px"});
	


}

base.nav();
//base.desaturate.init();
base.galleryMini();
base.tooltip();
base.stuff();
base.validation();
base.video();
base.tabs();
base.flashversion();
base.flashText();
base.flashImage();
base.schedule();
base.racesDrop();
base.flashImage();
base.productMenu();


function markActiveLink() {
   jQuery(".nav-new a").each(function() {
      var currentURL = window.location.toString().split("/");
      addslash = "/"+currentURL[currentURL.length-1];
      if(jQuery(this).attr("href") == addslash){
          jQuery(this).parent().addClass("on");
          jQuery(this).addClass("active");
          jQuery(this).parents(".base-nav-item").addClass("on");
     }
    });
    
/*
   if(jQuery(".nav-new ul li a").hasClass("active") == false) {
      jQuery(".nav-new ul li a").parent().addClass("active");
    }
*/
 }
markActiveLink();

