﻿$.easing.easeOutQuart = function (x, t, b, c, d) { return -c * ((t=t/d-1)*t*t*t - 1) + b;};
$.easing.easeout= function(t, b, c, d) { return -c * t * t / (d * d) + 2 * c * t / d + b;}
$.easing.easein = function(t, b, c, d) { return c * (t /= d) * t + b; };

function loadVideoArticle(assetid,url,titleOverride){
    $("#RelatedStory").hide().html("");
    $("#Title").html("");
    if (assetid!=null && assetid!="")
        $("#RelatedStory").load("/"+region+"/Article/Show/"+assetid,function (responseText, textStatus, XMLHttpRequest){
                if(textStatus!="error") {
                    if (titleOverride==null){
                        var x = $(responseText).get(0);
                        $("#Title").html($(x).html());
                    } else
                    $("#Title").html("<span>"+titleOverride+"</span>");
                    
                    $("#RelatedStory").html(responseText);
                    //$('.corner').corner();
                    ApplyPhotoScroller();
                    $("#RelatedStory").show();
                    loadFile(url);
                }    
        });
    else
        if (url!=null){
            
            loadFile(url);
        }
        
}

function loadFile(url,playstatus,placeHolderId,thumbnailurl){
      var hasFlashInstalled=DetectFlashVer(9, 0, 0);
      if (hasFlashInstalled){
      var s1 = new SWFObject('/content/player.swf','jstest','410','270','8','#000000');
      s1.addParam('allowscriptaccess', 'always');
      s1.addParam('allowfullscreen', 'true');
      s1.addVariable('width','410');
      s1.addVariable('height','270');
      s1.addVariable('overstretch','none');
      s1.addVariable('stretching','none');
      s1.addVariable('fullscreen','true');
      s1.addVariable('bufferlength','3');
      s1.addVariable('skin','/content/dwtvskin.swf');
      s1.addVariable('abouttext','Copyright (C) MetrolandWest Media Group.');
      s1.addVariable('aboutlink','http://metroland.com');
      
      if (thumbnailurl!=null){
        s1.addVariable('image',thumbnailurl);
        }
      if (playstatus!=null)  s1.addVariable('autostart',playstatus); 
        else s1.addVariable('autostart','true');
      s1.addVariable('javascriptid','jstest');
      s1.addVariable('enablejs', 'true');
      //url="rtmp://metroland.flash.internapcdn.net/metroland_vitalstream_com/_definst_/a25ebed346c79ee475dd7042de73.flv";
      //alert(url);
      if (url.indexOf("http") == 0) {
          s1.addVariable('file', url);
      } 
      else {
        var streamer = url.substring(0, url.lastIndexOf("/"));
        var streamname = url.substring(url.lastIndexOf("/")+1, url.length);
        s1.addVariable('file', streamname);
        s1.addVariable('streamer', streamer);
      }
      s1.write((placeHolderId==null)?'playercontext':placeHolderId);
      
      }else
      {
          document.getElementById('playercontext').innerHTML="<a target='_blank' href='http://www.adobe.com/go/getflash' style='color:white;display:block;text-align:center;line-height:250px;font-size:22px;font-family:arial;'>Click Here To Get The Latest Flash Player</a>";
		  if(window.confirm("Would you like to navigate to the Adobe website to get the latest flash player required by this website?")){
		    window.open("http://www.adobe.com/go/getflash");
		  }
          return;
      }
}

function IsIE6(){
    return (navigator.userAgent.indexOf("IE 6.0")>0);
}


$.metadata.setType("attr", "data");
$(document).ready(function() {

	$("#offers li")
        .mouseover(function() { $(this).addClass("hover"); })
        .mouseout(function() { $(this).removeClass("hover"); })
        .click(function() {
        	var anchor = $(this).find("a")[0];
        	window.location.href = anchor.href;
        });
	$("#jobs li")
        .mouseover(function() { $(this).addClass("hover"); })
        .mouseout(function() { $(this).removeClass("hover"); })
        .click(function() {
        	var anchor = $(this).find("a")[0];
        	window.location.href = anchor.href;
        });
	$(".tabs a").click(function() {
		if (!IsIE6())
			$(this).parents("ul.tabs").find("a").removeClass("selected");
		else {
			var anchors = $(this).parents("ul.tabs").find("a");
			for (i = 0; i < anchors.length; i++) {
				var anchor = anchors[i];
				anchor.className = anchor.className.replace(" selected", "");
				anchor.className = anchor.className.replace(".selected", "");
				anchor.className = anchor.className.replace("selected", "");
			}
		}

		if ($(this).parents("div.rightCol").find("form").length > 0) {
			$(this).parents("div.rightCol").find("form").get(0).action = "/" + region + "/Search/" + $(this).attr("class").replace("Tab", "");
		}
		else {
			// Archive Tab is clicked
			var link = $(this).attr("src");
			$("#ArchiveHolder img.loader").animate({ opacity: 1.0 });
			$("#Archive")
                    .fadeOut("fast", function() {
                    	$("#Archive").load(link, function() {
                    		// Update the Archive link on the top of Archive Section
                    		$("#ArchiveHolder a.archivelnk")[0].href = link.replace("List", "Archive");

                    		$("#ArchiveHolder img.loader").animate({ opacity: 0.0 });
                    		var myheight = $('#Archive').height() + 80;
                    		if (myheight < 350) myheight = 350;
                    		$("#ArchiveHolder").animate({ height: myheight + "px" }, 500);
                    		$("#Archive").fadeIn("fast", function() {

                    		});
                    	});

                    });

		}
		if (!IsIE6())
			$(this).addClass("selected");
		else {
			var anchor = this;
			anchor.className += "selected";
		}
	});
	if (window.location.href.toLowerCase().indexOf("/search/article") > 0)
		$(".rightCol .tabs a.ArticleTab").trigger("click");
	if (window.location.href.toLowerCase().indexOf("/search/video") > 0)
		$(".rightCol .tabs a.VideoTab").trigger("click");
	if (window.location.href.toLowerCase().indexOf("/search/specialoffer") > 0)
		$(".rightCol .tabs a.SpecialOfferTab").trigger("click");
	if (window.location.href.toLowerCase().indexOf("/search/jobs") > 0)
		$(".rightCol .tabs a.JobsTab").trigger("click");

	$("form.searchbox input#q").focus(function() { if ($(this).get(0).value == "Quick Search") $(this).get(0).value = ""; });
	$("form.searchbox input#q").blur(function() { if ($(this).get(0).value == "") $(this).get(0).value = "Quick Search"; });
	$("form.searchbox").submit(function(obj) { if ($(this).find("input#q").get(0).value == "Quick Search") { alert("Please enter a term to search!"); return (false); } });
	$(".weekdays a").click(function() {
		$(".ajaxPlaceHolder").animate({ opacity: 0.2 }, 50);
		$(this).parents("ul.weekdays").find("a").removeClass("selected");
		imgObj = $(this).find("img").get(0);
		day = imgObj.alt.substring(0, imgObj.alt.indexOf("/")).replace(" ", "");
		$(this).addClass("selected");
		$("#VideoItemsTitle").html((imgObj.alt.substring(imgObj.alt.indexOf("/") + 1, imgObj.alt.length) + " Featured Videos").toUpperCase());
		$("#ArticleItemsTitle").html((imgObj.alt.substring(imgObj.alt.indexOf("/") + 1, imgObj.alt.length) + " Featured Articles").toUpperCase());
		$("#FeaturedArticles").attr("href", "/" + region + "/Article/List/" + day);
		$("#FeaturedVideos").attr("href", "/" + region + "/Video/Scroller/" + day);
		$("#videorss").attr("href", "/" + region.toLowerCase() + "/rss/" + day.toLowerCase() + "/videos");
		$("#videorss").attr("title", day + ": " + region + " Videos RSS Feed");
		$("#articlerss").attr("href", "/" + region.toLowerCase() + "/rss/" + day.toLowerCase() + "/articles");
		$("#articlerss").attr("title", day + ": " + region + " Articles RSS Feed");
		$("#DayContainer .loader").animate({ opacity: 1.0 }, 50);
		reloadAjax();
		$("#DayContainer .loader").animate({ opacity: 0.0 }, 50);

	});
	//reloadAjax();
	$(".leftCol .tabs a.VideoTab").trigger("click");
	$("#regions").click(function() {
		if ($(this).height() <= 30)
			$(this).animate({ height: (regionCount + 1) * 20 }, 200); else
			$(this).animate({ height: 20 }, 200);
	});
	ApplyPhotoScroller();
	if (IsIE6()) {
		var anchors = $(".tabs a");
		for (i = 0; i < anchors.length; i++) {
			var anchor = anchors[i];
			anchor.className = anchor.className.replace(" selected", "selected");
		}
	}

	tb_init('a.thickbox, area.thickbox, input.thickbox');

	// homepage Code ************************************************************************************
	if ($(".homepageHolder").length > 0) {
		// Open and Close categories
		$("#" + currentday + " .collapsearea").addClass("active");
		$("#" + currentday + " .collapsearea").show();
		$("#" + currentday + " .categorytext").addClass("open");
		$("#" + currentday + " .categoryhead").addClass("collapsed");

		$(".categorylink").click(function() {
			$(this).find(".categorylink").trigger('click');
		});

		$(".categorytext").click(function() {
			var currentlink = $(this);
			var string = $(this).attr("title");
			var temp = $("#" + string);
			$(".collapsed").removeClass("collapsed");

			if ($(this).hasClass("open")) {
				$(".categorytext").removeClass("open");
				$(temp).removeClass("open").slideUp(300);
			}
			else {
				$(".categorytext").removeClass("open");
				$(".collapsearea").slideUp("slow");
				$(currentlink).addClass("open");
				$(this).parent().addClass("collapsed");
				//console.log($(this).parent().attr("class"));
				setTimeout(function() { $.scrollTo('#' + string, 300, { easing: 'linear', offset: -150 }); }, 1000);
				$(temp).addClass("active").slideDown("slow");
			}
		});

		$('#homeContestBox').cycle({
			fx: 'fade',
			speed: 'fast',
			timeout: 0,
			random: 1,
			continuous: 0,
			next: '.homeNav #contestNext',
			prev: '.homeNav #contestPrev'
		});
	}


	$(".homeThumbnailsScroll")
	.click(function() {
		var anchor = $(this).find("a")[0];
		window.location.href = anchor.href;
	});

});
function ApplyPhotoScroller(){
        $('#Photos .scrollable').serialScroll({
	    items:'li',
	    prev:'#Photos a.prev',
	    next:'#Photos a.next',
	    axis:'x',
	    onBefore:function( e, elem, $pane, $items, pos ){
			$('#Photos a.prev').add($('#Photos a.next')).show();
			if( pos == 0 )
				$('#Photos a.prev').hide();
			else if( pos == $items.length-1 )
				$('#Photos a.next').hide();
		},
	    step:3,
	    offset:0, 
	    start:0, 
	    duration:1500,
	    force:false,
	    stop:false,
	    lock:false,
	    cycle:true, //don't pull back once you reach the end
	    easing:'easeOutQuart', //use this easing equation for a funny effect
	    jump: false //click on the images to scroll to them
    });
}
function reloadAjax(func) {
    
$.each( $(".ajaxPlaceHolder"),
    function(i, obj) {
        $(obj).load($(obj).attr("href"),function (responseText, textStatus, XMLHttpRequest){
            if(textStatus!="error") {
                $(obj).html(responseText).animate({ opacity: 1.0 }, 150);
                if (func!=null) func;
            }
            
        })
            
    });
}