/*
	Campaign For Tobacco-Free Kids scripts (utilizing jQuery 1.2.1)
	Developed by Noah Lazar, Saforian
*/


// Absolute path for the root of the site
var path = '/';


// Timeline markers
var steps = new Array(0, 39, 79, 120, 132, 166, 206, 240);  // Pixel steps
var currentstep = 0;


// Get filename for subnavigation highlight
var slashsplice = location.href.split('/');
var filename = slashsplice[slashsplice.length-1];


/* --- Initialize page --- */
$(document).ready(function(){

	// Stripe table rows
	$("table.table-data tr:nth-child(even)").css("backgroundColor","#dde2f3");


	/* - Tools navigation flyout - */

	// Add flyouts
	$("#nav-tools").hover(function(){
		$(this).addClass("highlight");

		if ($.browser.msie && $.browser.version < 7) {
			// Just show in IE6
			$("ul",this).show();
		} else {
			// Slide down
			$("ul",this).slideDown(200);
		}

	},function(){
		$(this).removeClass("highlight");

		if ($.browser.msie && $.browser.version < 7) {
			// Just hide in IE6
			$("ul",this).hide();
		} else {
			// Slide up
			$("ul",this).slideUp(70);
		}

	});

	// Add column breaks
	$("#nav-tools>ul>li:even").css("clear","both");



	/* - Timeline slider - */

	if ($("body").is("#home")) {

		// Make events horizontal
		$("#timeview #events").css({position:"absolute",zIndex:"1",top:"0",left:"0",width:"9999px"});

		// Show next/last arrows
		$("#timeback, #timenext, #slider img").css({display:"block"});


		// Go forewords
		$("#timenext").click(function(){

			// Check for end of timeline
			if (currentstep < steps.length-1) {

				currentstep++;
				$("#events").animate({left:'-=244px'}, "slow");
				$("#slider img").animate({left:steps[currentstep]+'px'}, "slow");

			} else {

				// Reset to beginning
				currentstep = 0;
				$("#events").animate({left:'0'}, "normal");
				$("#slider img").animate({left:steps[0]+'px'}, "normal");
			}

			return false;
		});

		// Go backwards
		$("#timeback").click(function(){

			// Check for beginning of timeline
			if (currentstep > 0) {

				currentstep--;
				$("#events").animate({left:'+=244px'}, "normal");
				$("#slider img").animate({left:steps[currentstep]+'px'}, "normal");

			} else {

				// Reset to end
				currentstep = steps.length-1;
				$("#events").animate({left:'-'+(244*(steps.length-1))+'px'}, "normal");
				$("#slider img").animate({left:steps[steps.length-1]+'px'}, "normal");

			}

			return false;
		});

		// Homepage: Create quick links for each step on timeline bar
		$("#events>div").each(function(i){
	
			// Get heading text and link
			var heading = $("h6",this).text();
			var url = $("p.more>a",this).attr("href");
	
			// Create step link
			var newlink = document.createElement("a");
	
			newlink.appendChild(document.createTextNode(heading));
			newlink.href = url;
			newlink.title = heading;
			newlink.style.left = steps[i]+'px';
	
			$(newlink).click(function(){
				currentstep = i;
				$("#events").animate({left:'-'+(244*i)+'px'}, "normal");
				$("#slider img").animate({left:steps[i]+'px'}, "normal");
				return false;
			});
	
			// Add to page
			$("#slider>div").append(newlink);
	
		});

	}

	// Timeline pages: Create quick links for each step on timeline bar
	$("body:not(#home):has(#slider)").find("#subnav a").each(function(i){
		var subnavlink = this;

		// Get heading text and link
		var heading = $(this).text();
		var url = $(this).attr("href");

		// Create step link
		var newlink = document.createElement("a");

		newlink.appendChild(document.createTextNode(heading));
		newlink.href = url;
		newlink.title = heading;
		newlink.style.left = steps[i]+'px';

		// Display marker on timeline
		if (newlink.href.indexOf(filename) > -1) {
			$("#slider img").css({left:steps[i]+'px', display:"block"});
		}

		// Hover effects
		$(newlink).hover(function(){
			subnavlink.style.backgroundColor = "#f2f2f2";
			this.style.backgroundColor = "#0f5aad";
		},function(){
			subnavlink.style.backgroundColor = "transparent";
			this.style.backgroundColor = "transparent";
		});

		$(this).hover(function(){
			newlink.style.backgroundColor = "#0f5aad";
		},function(){
			newlink.style.backgroundColor = "transparent";
		});

		// Add link to page
		$("#slider>div").append(newlink);
	});



	// Insert document icons to links
	$("a[href$=pdf]").not(":has(img)").append('<img class="icon" src="'+path+'images/icon_pdf.gif" width="14" height="14" alt=" (PDF)">');
	$("a[href$=doc]").not(":has(img)").append('<img class="icon" src="'+path+'images/icon_word.gif" width="14" height="14" alt=" (Word Document)">');
	$("a[href$=xls]").not(":has(img)").append('<img class="icon" src="'+path+'images/icon_excel.gif" width="14" height="14" alt=" (Spreadsheet)">');
	$("a[href$=ppt]").not(":has(img)").append('<img class="icon" src="'+path+'images/icon_powerpoint.gif" width="14" height="14" alt=" (Presentation)">');
	$("a[href$=rtf]").not(":has(img)").append('<img class="icon" src="'+path+'images/icon_rtf.gif" width="14" height="14" alt=" (Rich Text File)">');



	// Highlight the current section's navigation link (Dreamweaver sets relative link to just index.html)
	$("#navigation a[href='index.html']").slice(0,1).addClass("highlight");

	// Highlight subnavigation link -- parse filename from URL
	$("#subnav a[href='" + filename + "']").slice(0,1).addClass("highlight");


	// sIFR headlines
	if (typeof sIFR == "function") {
		sIFR.replaceElement(named({sSelector:"#titletrue>h1", sFlashSrc:path+"common/newsgothic.swf", sColor:"#777777", sLinkColor:"#777777", sBgColor:"#ffffff", nPaddingTop:4, sHoverColor:"#777777", sFlashVars:"offsetTop=1"}));
		sIFR.replaceElement(named({sSelector:"#titlefalse>h1", sFlashSrc:path+"common/newsgothic.swf", sColor:"#ffffff", sLinkColor:"#ffffff", sBgColor:"#3272b9", sHoverColor:"#ffffff", sCase:"upper", sWmode:"transparent", sFlashVars:"textalign=right&offsetTop=7&offsetLeft=0"}));
	}

});


