//Cufon.replace('h1');
Cufon.replace('#promoBox2 a', {fontFamily: 'ITC Lubalin Graph'});
Cufon.replace('.promo2Txt h4', {fontFamily: 'ITC Lubalin Graph'});
Cufon.replace('#footer h5', {fontFamily: 'ITC Lubalin Graph'});
Cufon.replace('#promoBox .leftbox h1', {fontFamily: 'ITC Lubalin Book'});
Cufon.replace('#promoBox .topCaptText p', {fontFamily: 'ITC Lubalin Graph'});
Cufon.replace('#mainmenu > div > ul > li > a', {hover:true, hoverables: {a: true}, fontFamily: 'ITC Lubalin Graph'});
Cufon.replace('#submen ul > li > a', {fontFamily: 'ITC Lubalin Graph'});



$(document).ready(function() {

	$("a").removeAttr("title");
	$("a").removeAttr("alt");
	$('#mainmenu > div > ul > li:first-child').addClass('first-item');
	var htmlApp = '<div id="contactinfo">'+
             '       <div id="connector"></div>'+
             '       <div class="line1 clearfix">'+
             '           <div class="fl" style="display: inline; width: 147px;">'+
             '               <h5>Head Office<br />Timeline Television Ltd</h5>'+
             '               <p>Ealing Studios<br />Ealing Green<br />London<br />W5 5EP</p>'+
             '           </div>'+
             '           <div class="fl" style="display: inline; width: 107px;">'+
             '               <p>t. 08450 944 445<br />f. 08450 944 446<br />e. <a style="display: inline; font-size: 1em; padding: 0; margin: 0;" href="mailto:info@timeline.tv">info@timeline.tv</a></p>'+
             '               <a href="http://www.facebook.com/pages/Timeline-Television/169523219774415" target="_blank" class="faceCont"></a><a target="_blank" href="https://twitter.com/Timeline_TV" class="tweetCont"></a><div class="clear"></div>'+
             '           </div>'+
             '           <div style="float: right; margin:0px; display: inline; overflow: hidden;">'+
             '               <a style="margin:0;padding:0;display:inline;" href="http://maps.google.co.uk/maps?oe=utf-8&client=firefox-a&ie=UTF8&q=Ealing+Studios,+Ealing+Green,+City+of+London,+Greater+London+W5+5EP&fb=1&gl=uk&hq=Ealing+Studios,+Ealing+Green,&hnear=0x48760d8cba54abd3:0xbab29598238e0afd,Ealing+Studios,+Ealing+Green,+City+of+London,+London+W5+5EP&hl=en&view=map&cid=13452979517573171965&z=16&iwloc=A"><img src="http://80.90.198.98/wp-content/themes/timeline/images/contact_map.jpg" style="border: 0;"  /></a>'+
             '           </div>'+
             '       </div>'+
             '       <div class="line2 clearfix">'+
             '           <div class="fl">'+
             '               <h5>Timeline TV Soho</h5>'+
             '               <h6>Timeline Television Ltd</h6>'+
             '               <p>14A Broadwick Street<br />Soho<br />London<br />W1F 8HP<br /><a href="mailto:info@timeline.tv" style="color:#1d9fd8 !important">Contact us</a></p>'+
             '           </div>'+
             '           <div class="fl">'+
             '               <h5>Timeline TV North</h5>'+
             '               <h6>Timeline Television Ltd</h6>'+
             '               <p>The Pie Factory<br />101 Broadway<br />Media City UK<br />Salford<br />M50 2NT<br /><a href="mailto:info@timeline.tv" style="color:#1d9fd8 !important">Contact us</a></p>'+
             '           </div>'+
             '       </div>'+
             '     </div>';

	var $ul = $('#mainmenu > div > ul > li');
	var $el = $ul.eq($ul.length-2);
	//$el.addClass('last-item');
	$('#mainmenu > div > ul > li:last-child').addClass('contact');
	Cufon.refresh();
	$('#submen > ul > li:last-child > a').addClass('lastSub');
	$('#submen > ul > li:last-child').css('background', 'none');
	$('#mainmenu li.contact').append(htmlApp);
	$('#mainmenu li.contact a').live('click', function() {
		if ( ($(this).html() != 'info@timeline.tv') && ($(this).html() != 'Contact us') ) {
			window.open($(this).attr("href"), '_blank');
		} else {
			window.location=$(this).attr("href");
		}
	});
	$("#mainmenu li.contact a").click(function(event) { 
  	event.preventDefault(); 
	}); 

	$('#footer .footerBox:first-child').css('margin-left', '19px');
	
	var pathname = window.location.href;
	
	if ( pathname.indexOf('#comments') != -1 ) {
		$('.commentsWrap').slideDown('slow', function() {
			// Animation complete.
		});
	}

	$('.commentsNbrLnk').click(function() {
		$('.commentsWrap').slideDown('slow', function() {
			// Animation complete.
		});
		return false;
	});

	$('.commentsClose').click(function() {
		$('.commentsWrap').slideUp('slow', function() {
			// Animation complete.
		});
		return false;
	});

	$('#promoBox2 li').hover(function(){
		$('#promoBox2 li').removeClass('active');
		$('#promoBox2 .promo2Container').stop(true, true).fadeOut();
		var idImg = $(this).attr('id').replace("Lnk", "");
		$('#'+idImg).stop(true, true).fadeIn(function () { 
    	$(this).find(".promo2Txt").css("z-index", "9999"); 
    }); 
		$(this).addClass('active')
	},  
  function () { 
    
  } 
); 


	$('#mainmenu ul ul').mouseout(function() {
  	$('#mainmenu ul li a').not('#mainmenu ul li.contact a').css("color","#fff");
		Cufon.refresh('#mainmenu > ul > li > a');
	});
	
	$('#mainmenu > div > ul > li > a').mouseover(function() {
		$(this).not('#mainmenu ul li.contact a').css("color","#1d9fd8");
	});
	
	$('#mainmenu > div > ul > li > a').mouseout(function() {
		$(this).not('#mainmenu ul li.contact a').css("color","#fff");
	});

	var tmpInd = 0;

	$('.strRtProm').click(function(){
		$(".rightbox").stopTime('controlled');
		$(".rightbox .promo1Container:eq("+tmpInd+")").fadeOut("slow", function() {
			 $(".rightbox a.dotProm:eq("+tmpInd+")").css('backgroundPosition', '0px 0px');
       if ( tmpInd < 2 ) {
					tmpInd++;
					$(".rightbox .promo1Container:eq("+tmpInd+")").fadeIn("slow",function () { 
			    	$(this).find("div").css("z-index", "100"); 
    			}); 
					$(".rightbox a.dotProm:eq("+tmpInd+")").css('backgroundPosition', '0px -8px');
				} else {
					tmpInd=0;
					$(".rightbox .promo1Container:eq("+tmpInd+")").fadeIn("slow",function () { 
			    	$(this).find("div").css("z-index", "100"); 
    			}); 
					$(".rightbox a.dotProm:eq("+tmpInd+")").css('backgroundPosition', '0px -8px');
				}

				if ( tmpInd == 0 ) {
					$('.strLtProm').css('backgroundPosition', '0px -11px');
				} else {
					$('.strLtProm').css('backgroundPosition', '0px 0px');
				}
		
				if ( tmpInd == 2 ) {
					$('.strRtProm').css('backgroundPosition', '0px -11px');
				} else {
					$('.strRtProm').css('backgroundPosition', '0px 0px');
				}

    });
	});

	$('.strLtProm').click(function(){
		$(".rightbox").stopTime('controlled');
		$(".rightbox .promo1Container:eq("+tmpInd+")").fadeOut("slow", function() {
			 $(".rightbox a.dotProm:eq("+tmpInd+")").css('backgroundPosition', '0px 0px');
       if ( tmpInd > 0 ) {
					tmpInd--;
					$(".rightbox .promo1Container:eq("+tmpInd+")").fadeIn("slow",function () { 
			    	$(this).find("div").css("z-index", "100"); 
    			}); 
					$(".rightbox a.dotProm:eq("+tmpInd+")").css('backgroundPosition', '0px -8px');
				} else {
					tmpInd=2;
					$(".rightbox .promo1Container:eq("+tmpInd+")").fadeIn("slow",function () { 
			    	$(this).find("div").css("z-index", "100"); 
    			}); 
					$(".rightbox a.dotProm:eq("+tmpInd+")").css('backgroundPosition', '0px -8px');
				}

				if ( tmpInd == 0 ) {
					$('.strLtProm').css('backgroundPosition', '0px -11px');
				} else {
					$('.strLtProm').css('backgroundPosition', '0px 0px');
				}
		
				if ( tmpInd == 2 ) {
					$('.strRtProm').css('backgroundPosition', '0px -11px');
				} else {
					$('.strRtProm').css('backgroundPosition', '0px 0px');
				}
    });

	});

	$(".rightbox").everyTime(5000, 'controlled', function() {
		$(".rightbox .promo1Container:eq("+tmpInd+")").fadeOut("slow", function() {
			 $(".rightbox a.dotProm:eq("+tmpInd+")").css('backgroundPosition', '0px 0px');
       if ( tmpInd < 2 ) {
					tmpInd++;
					$(".rightbox .promo1Container:eq("+tmpInd+")").fadeIn("slow",function () { 
			    	$(this).find("div").css("z-index", "100"); 
    			}); 
					$(".rightbox a.dotProm:eq("+tmpInd+")").css('backgroundPosition', '0px -8px');
				} else {
					tmpInd=0;
					$(".rightbox .promo1Container:eq("+tmpInd+")").fadeIn("slow",function () { 
			    	$(this).find("div").css("z-index", "100"); 
    			}); 
					$(".rightbox a.dotProm:eq("+tmpInd+")").css('backgroundPosition', '0px -8px');
				}

				if ( tmpInd == 0 ) {
					$('.strLtProm').css('backgroundPosition', '0px -11px');
				} else {
					$('.strLtProm').css('backgroundPosition', '0px 0px');
				}
		
				if ( tmpInd == 2 ) {
					$('.strRtProm').css('backgroundPosition', '0px -11px');
				} else {
					$('.strRtProm').css('backgroundPosition', '0px 0px');
				}

    });
	});

	$('#mainmenu ul.children').each(function(index) {
    var height = $(this).height();
		if (height >= 250) {
			childs = $(this).children('li');
			size = childs.length;
			html = '';
			for (var i = 0; i < size; i++) {
				if ( i == 0 ) {
					html += '<ul class="children" style="">';
				}

				html += $(childs[i]).outerHTML();

				if ( i == ((size/2)-1) ) {
					html += '</ul><ul class="children children2" style="">';
				}

				if ( i == (size-1) ) {
					html += '</ul>';
				}

			}
			$(this).replaceWith(html);
		}
  });


	$('a.dotProm').click(function(){
		$(".rightbox").stopTime();
		var indexTmp = $(this).parent().children("a.dotProm").index(this);
		$(".rightbox .promo1Container:eq("+tmpInd+")").fadeOut("slow", function() {
			 $(".rightbox a.dotProm:eq("+tmpInd+")").css('backgroundPosition', '0px 0px');
			 tmpInd = indexTmp;
			 $(".rightbox .promo1Container:eq("+tmpInd+")").fadeIn("slow",function () { 
			    	$(this).find("div").css("z-index", "100"); 
    			}); 
			 $(".rightbox a.dotProm:eq("+tmpInd+")").css('backgroundPosition', '0px -8px');
				
				if ( tmpInd == 0 ) {
					$('.strLtProm').css('backgroundPosition', '0px -11px');
				} else {
					$('.strLtProm').css('backgroundPosition', '0px 0px');
				}
		
				if ( tmpInd == 2 ) {
					$('.strRtProm').css('backgroundPosition', '0px -11px');
				} else {
					$('.strRtProm').css('backgroundPosition', '0px 0px');
				}
    });
	});

	
	$('#carousell')
		.anythingSlider({
		 width          : 224,
		 height         : 230,
		 delay          : 4000,  
		 autoPlay       : true,
		 toggleControls : false,
		 hashTags				: false,
		 buildArrows		: true,
		 buildNavigation: true,
		 startStopped   : false,
		 theme          : 'metallic'
		});
	
	$('.strRtPromCar').click(function() {
		current = $('#carousell').data('AnythingSlider').currentPage;
		if ( current != 4 ) {
		  $('#carousell').data('AnythingSlider').goForward();
			$('#carousell').data('AnythingSlider').startStop(false);
			setTimeout(function(){
	      $('#carousell').data('AnythingSlider').startStop(true);
			},7000); 
		} else {
			$('#carousell').anythingSlider(1);
			$('#carousell').data('AnythingSlider').startStop(false);
			setTimeout(function(){
	      $('#carousell').data('AnythingSlider').startStop(true);
			},7000);
		}
		return false;
	});

	$('.strLtPromCar').click(function() {
		current = $('#carousell').data('AnythingSlider').currentPage;
		if ( current != 1 ) {
		  $('#carousell').data('AnythingSlider').goBack();
			$('#carousell').data('AnythingSlider').startStop(false);
			setTimeout(function(){
	      $('#carousell').data('AnythingSlider').startStop(true);
			},7000); 
			
		} else {
			$('#carousell').anythingSlider(4);
			$('#carousell').data('AnythingSlider').startStop(false);
			setTimeout(function(){
	      $('#carousell').data('AnythingSlider').startStop(true);
			},7000);
		}
		return false;
	});

	if (($('.mainImg').length != 0) || ($('.imgRight').length != 0)) {

		if ($(".mainImg").position()) {
			var capPos = $(".mainImg").position().top + $(".mainImg").height() - $('.caption').outerHeight() - 9;
			$('.caption').css('top', capPos+'px');
			$('.caption').css('left', $(".mainImg").position().left +'px');
		} else {
			var capPos = $(".imgRight").position().top + $(".imgRight").height() - $('.caption').outerHeight() - 9;
			$('.caption').css('top', capPos+'px');
			$('.caption').css('left', $(".imgRight").position().left + 22 +'px');
		}

	}

});

