// JavaScript Document

/*	Gallery / ad refresh code from Sun */
var currentTab=1;		//Keeps track of the current tab for rotating.
var dlRotate;			//Stores the interval for tab switching so it can be stopped.
var dlRunning = false;	//Keeps track of whether or not the auto scroll is running.
var dlNumTabs;			//Counts the number of tabs once the DOM is ready.
var firstLoad = true;	//Modifies the Toggle function on the first page load.
var dlIsFade = false	//Checks if a fading tranistion is required.
var dlIsTabs = false	//Checks if tab switch is required.
var dlIsCount = true	//Checks if a counter needs to be displayed.
var dlFade = false;	//Keeps track of fading well it is occuring.
var firstPageLoad = true;
var dlTime = 7000;

function externalLinks() {
  if (!document.getElementsByTagName) return;
  var anchors = document.getElementsByTagName("a");
  for (var i=0; i<anchors.length; i++) {
	  var anchor = anchors[i];
	  if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "blank") {
		 anchor.target = "_blank";
	  }
  }
}

function dlCycle(direction) { //Goes to next or previous tab
	refreshAd2("#bigbox","bigbox");
	refreshAd2("#banner","superbanner");
	refreshAd2("#halfpage","halfpage");

	if(direction == "next") {
		dlSwitch(currentTab>=dlNumTabs?1:currentTab+1);
	} else {
		dlSwitch(currentTab==1?dlNumTabs:currentTab-1);
	}
}

function dlSwitch(tabNum) { //Goes to the requested tab

	if(dlIsCount) $(".rotateCount span").html(tabNum);
	function dlTabSwitch() {
	
		if(dlIsTabs) {
			$("#dl .tabs .active").removeClass("active");
			$("#dl .tabs li").eq(tabNum-1).addClass("active");
		}
	}
	
	dlTabSwitch();
	$(".rotateItem").eq(currentTab-1).hide();
	$(".rotateItem").eq(tabNum-1).show();
	currentTab=parseInt(tabNum);
	slideViewSingleAccount("olympics:"+MyOmnitureVariables.articlename,"olympics:"+MyOmnitureVariables.articlename+" (Pic#="+tabNum+")","cnocanoecaprod");
}

function pageInit() {
	//RESET VARIABLES FOR AJAX CALLS
	currentTab=1;		//Keeps track of the current tab for rotating.
	firstLoad = true;	//Modifies the Toggle function on the first page load.
	dlFade = false;		//Keeps track of fading well it is occuring.
	dlIsTabs = false;
		
	//Takes care of the dynamic leads if they exist.
	if($(".rotateBox").length > 0) {

		dlNumTabs = $(".rotateItem").size(); //Checks how many tabs there are
		if(dlNumTabs > 0) { //Checks if there are tabs
			dlIsTabs = true;
		}

		if($(".rotateCount").size() != 0) { //Checks if there is a counter that needs to be updated
			dlIsCount = true;
			$(".rotateCount").html("<span>1</span> of "+dlNumTabs)	
		}
	}
  }

function randomOrd(url,exclude) {
	exclude = typeof(exclude) != "undefined" ? exclude : false; //default value
	if(exclude)
		url = url.replace(/(.*?);ord=(.*?)/,"$1;!exclusion="+exclude+";ord=$2");	
	var rand = Math.floor(Math.random()*8999999999)+1000000000;
	return url.replace(/(.*?)ord=(.*?)(\?|\;)(.*)/,"$1ord="+rand+"$3$4");
}

function refreshAd2(div,type) {
	var whichDiv = $(div);
	var refreshAdTag = whichDiv.children("script").eq(1).attr("src");
	if(refreshAdTag) {
		whichDiv.html('<iframe src="http://www.torontosun.com/html/adrefresh-iframe.html?url='+randomOrd(refreshAdTag,"expandable")+'" frameborder="0" scrolling="no" id="'+div.substring(1)+'_iframe" class="iframe_'+type+'"><\/iframe>');
	} else {
		var adFrame = document.getElementById(div.substring(1)+'_iframe');
		var frameSrc = randomOrd(adFrame.src);
		whichDiv.children("iframe").remove();
		whichDiv.html('<iframe src="'+frameSrc+'" frameborder="0" scrolling="no" allowtransparency="true" id="'+div.substring(1)+'_iframe" class="iframe_'+type+'"><\/iframe>');
	}

}

$(document).ready(function() {
						   
	pageInit();
	
	$('.galleryNav a.prev').click( function() {
		dlCycle('prev');									
	});
	
	$('.galleryNav a.nextF').click( function() {
		dlCycle('next');									
	});
	
});
