/***************************************************************************
*
* Copyright (C) Telegraph Media Group Ltd.
* All Rights Reserved. No use, copying or distribution of this work may be
* made. This notice must be included on all copies, modifications and
* derivatives of this work.
****************************************************************************
* Author: L Dyson  Date: 12/05/2008
*
* Description:
* Common javascript functions
*
****************************************************************************
* $Id: common.js,v 1.11 2008/09/05 13:23:02 spencer Exp $
***************************************************************************/

document.domain="telegraph.co.uk";

/* JQuery plugin to retrieve the outer HTML of any given element */
jQuery.fn.outerHTML = function() {
	return $('<div>').append( this.eq(0).clone() ).html();
};

/* New JQuery plugin to pull back comments from any given HTML element */
/* Author: L Dyson Date:13/07/2008 */
jQuery.fn.comments = function(i) {
	if (this.html() != null && this.html() != '') {
		var searchString = this.html().split("<!--");
		var commentArray = new Array();
	
		for (j in searchString) {
			if(searchString[j].indexOf("-->") != -1) {
				commentArray.push(searchString[j].split("-->")[0].replace(/^\s+/,'').replace(/\s+$/,''));
			}
		}
		if (isNaN(Number(i)) || i == null) {
			return commentArray;
		} else {
			return commentArray[i];
		}
	} else {
		return null;
	}
};

/* ON PAGE LOAD... */
$(function() {
	/***********************************************************************
	*                          Breaking News Ticker                        *
	***********************************************************************/
	if ($("#tmglBreakingNews").length > 0) {
		$("#tmglBreakingNews > ul > li:not(:first)").hide();
		$("#tmglBreakingNews > ul > li:first").addClass("shown");
	
		setInterval(rotateBreakingNews,5000);
	}

	/***********************************************************************
	*                      Share this article function                     *
	***********************************************************************/
	// Fail-safe code - hide the share dropper if the user has JavaScript enabled
	$(".share > ul").removeClass("show").hide();

	// If the user clicks the share dropper; show/hide it accordingly
	$(".share a:first").click(function(){
		$(".share").toggleClass('sharey');
		if ($(".share > ul:hidden").get().length>0) {
			$(".share > ul:hidden").show();
		} else {
			$(".share > ul:visible").hide();
		}
		return false;
	});

	/***********************************************************************
	*                          Commercial Carousel                         *
	***********************************************************************/
	if($("#comCarousel").length > 0) {
		var comCarousel = $("#comCarousel").get();
		
		for (i in comCarousel) {
			ccObj[i] = new comCarouselObj();
			ccObj[i].carousel = comCarousel[i];
			
			$(ccObj[i].carousel).find("#comCarTools").removeClass("hide");
			
			$(ccObj[i].carousel).find(".offer[class*='hide']").removeClass("hide").hide();
			$(ccObj[i].carousel).find(".offer[class*='show']").removeClass("show").show();
			
			var partners = $(ccObj[i].carousel).find(".partner[class*='hide'").removeClass("hide").hide();
			var partners = $(ccObj[i].carousel).find(".partner[class*='show'").removeClass("show").show();
			
			$(ccObj[i].carousel).find("#carouselBtns > .btn").bind("click",{count:i},function(e){				
				comCarSelectPartner(e.data.count,$(ccObj[e.data.count].carousel).find("#carouselBtns > .btn").index(this));
				if (ccObj[e.data.count].timeoutId!=null){
					clearInterval(ccObj[e.data.count].timeoutId);
					ccObj[e.data.count].timeoutId=null;
				}
			});
			ccObj[i].timeoutId = setInterval("comCarNextPartner("+i+")",5000);
		}
	}



/***********************************************************************
*					 most Popular      (HAREESH)   *
***********************************************************************/

	 $TabActive = $("#mostpop > .tabs > ul > li:first"); 

	    $('#mostpop > .tabs > ul > li').click(function() { 
	     
		  if ($(this) != $TabActive) {  
			 $TabActive.removeClass('current');  
			  $(this).addClass('current');  
			 $TabActive = $(this);  
			   
			contentIdChildDiv = "#div-" + $TabActive.attr("id");  
			$("div.view-content").hide();  
			 $(contentIdChildDiv).show();  
			 }  
	   });
	   
	     $(".lists div:not(div:first)").hide();  
		$("#mostpop > .tabs > ul > li:first").addClass("current");  


});


/***********************************************************************
*         Standard functions required for Breaking News Ticker         *
***********************************************************************/
function rotateBreakingNews() {
	if ($("#tmglBreakingNews > ul > li:first[class*='shown']").length > 0) {
		$("#tmglBreakingNews > ul > li:first[class*='shown']").removeClass("shown").hide();
		$("#tmglBreakingNews > ul > li:not(:first)[class!='shown']").addClass("shown").show();
	} else {
		$("#tmglBreakingNews > ul > li:first[class!='shown']").addClass("shown").show();
		$("#tmglBreakingNews > ul > li:not(:first)[class*='shown']").removeClass("shown").hide();
	}
}

/***********************************************************************
*          Standard functions required for Commercial Carousel         *
***********************************************************************/
var ccObj = [];
function comCarouselObj() {
	this.carousel=null;
	this.timeoutId=null;
}

function comCarNextPartner(id) {
	var activePartner = $(ccObj[id].carousel).find("#carouselBtns > .btn").index($(ccObj[id].carousel).find("#carouselBtns > .btn[class*=active]").get(0));
	if ($(ccObj[id].carousel).find("#carouselBtns > .btn").get().length > activePartner+1) {
		comCarSelectPartner(id,activePartner+1);
	} else {
		comCarSelectPartner(id,0);
	}
}

function comCarSelectPartner(id,btn) {
	$(ccObj[id].carousel).find("#carouselBtns > .btn[class*='active']").removeClass("active");
	$(ccObj[id].carousel).find("#carouselBtns > .btn:eq("+btn+")").addClass("active");

	$(ccObj[id].carousel).find(".partner").hide();
	$(ccObj[id].carousel).find("#partner-"+$(ccObj[id].carousel).find("#carouselBtns > .btn[class*='active'] > img").attr("id")).show();
}


/* OLD WINDOW OPEN FOR MIGRATED CONTENT */
function newWindow(url, name, features) {
	var newWin = window.open(url,name,features);
	newWin.moveTo(50,50);
	newWin.focus();
}
