﻿// set global variable to hold the current article and total articles in the RSS feed
var currentArticle = 0;
var totalArticles = 0;
var articleWidth = 700;

function pageLoad() {
    $(function () {
		// Fetch latest articles from Business Access
       BACMSServices.GetRSS('0', RSSSuccess, RSSFail, null);
	   // setup social networking icons
	   socialnetworking();
	   setupSlideshow();
	   clientSurvey();
    });
}
function RSSSuccess(result, context) {
    $("#RSSArticles").html(result);
    setupAnimation();
    playArticles = setInterval("changeArticle(1)", 7000);
	tagExternalLinks();
}
function RSSFail(reason) {
    $("#RSSArticles").html('<p>Latest Articles could not be loaded.<p>');
}
function setupSlideshow() {
	totalSlides = $("#slides img").size();
	slideHeight = $("#slideshow").height();
	slideIndex = Math.floor(Math.random()*totalSlides);
	
	changeSlide = function() {
		var slidePosition = slideIndex * slideHeight;
		$("#slides").hide().css({top: -slidePosition}).fadeIn(1500, function() {
			$("#slideshow").css("background-image","url(" + $("#slides img").eq(slideIndex).attr("src") + ")");
		});
	};
	
	beginSlideShow = function () {
		$("#slideshow").css("background-image","url(" + $("#slides img").eq(slideIndex).attr("src") + ")");
		playSlides = setInterval(function() {
			slideIndex = (slideIndex<totalSlides-1?slideIndex+=1:0);
			changeSlide();
		}, 11000);
	};
	changeSlide();
	beginSlideShow();
}
function setupAnimation() {
    // get the number of recent articles to put in the feed.
    totalArticles = $("#RSSArticles div").size();
    articleWidth = $(".rssArticle").width();
    var articlesWidth = totalArticles * articleWidth
    $("#RSSArticles").css({ 'width': articlesWidth });
    scrollAndFade = function (leftPosition) {
        $("#RSSArticles").css({ left: -leftPosition }).fadeIn('slow');
    };
    changeArticle = function (direction) {
        currentArticle += direction;
        if (currentArticle < 0) { currentArticle = totalArticles - 1 }
        if (currentArticle >= totalArticles) { currentArticle = 0 }
        var leftPosition = currentArticle * articleWidth;
        $("#RSSArticles").hide();
        scrollAndFade(leftPosition);
    };
    $("#rssNext").click(function () {
        clearInterval(playArticles);
        changeArticle(1);
        playArticles = setInterval("changeArticle(1)", 7000);
    });
    $("#rssPrevious").click(function () {
        clearInterval(playArticles);
        changeArticle(-1);
        playArticles = setInterval("changeArticle(1)", 7000);
    });
    $("#rssPlayPause").toggle(function () {
        $("#rssPlayPause").addClass("rssPlayPause");
        clearInterval(playArticles);
    }, function () {
        $("#rssPlayPause").removeClass("rssPlayPause");
        playArticles = setInterval("changeArticle(1)", 7000);
    });
}
function googleTranslateElementInit() {
		new google.translate.TranslateElement({
		pageLanguage: 'en'
	}, 'google_translate_element');
}
function clientSurvey() {
	// Setup Modal dialog box for survey prompt
    $("#surveyModal").dialog({
		close: function(event, ui) {$("#cse-search-form").show();},
    	modal: true,
        autoOpen: false,
        buttons: {
        	"No Thanks": function() {
            	$( this ).dialog( "close" );
             }
        }
    });	
	//Check if survey is requested in Query String
    //var survey = getQuerystring("survey", 0);
	  var survey = 0;
	  $("#surveyURL").attr("href","https://www.surveymonkey.com/s/WSGeneral");
	  //if (!isNaN(survey)) {
	  //  switch (parseInt(survey))
	  //  {
	  //      case 1: $("#surveyURL").attr("href","https://www.surveymonkey.com/s/WSEverett");
	  //              $("#WSLocation").text(" Everett ")
	  //              break;
	  //      case 2: $("#surveyURL").attr("href","https://www.surveymonkey.com/s/WSLynnwood");
	  //              $("#WSLocation").text(" Lynnwood ")
	  //              break;
	  //      case 3: $("#surveyURL").attr("href","https://www.surveymonkey.com/s/WSMonroe");
	  //              $("#WSLocation").text(" Monroe ")
	  //              break;
	  //      case 4: $("#surveyURL").attr("href","https://www.surveymonkey.com/s/WSYouthCenter");
	  //              $("#WSLocation").text(" Youth Center ")
	  //              break;
	  //  }
	  //}
	  var wsSurveyCookie = getCookie("wsSurvey");
	  if (wsSurveyCookie < 0) {
		  setCookie("wsSurvey",survey, 15);
		  // setup the modal box
		  $("#surveyModal").dialog('open');
	  } else {
			$("#cse-search-form").show();  
	  }
}
function getQuerystring(key, default_)
{
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
  return -1;
}

function setCookie(c_name,value,exminutes)
{
var exdate=new Date();
exdate.setMinutes(exdate.getMinutes() + exminutes);
var c_value=escape(value) + ((exminutes==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function tagExternalLinks() {
	$('a').each(function(index, element) {
        var url = $(this).attr("href");
		if (url.indexOf('http://', 0) >= 0 ) {
			$(this).attr('onclick', '_gaq.push([\'_trackEvent\', \'Outbound Links\', \'Visited\', \'' + url + '\']);');
		}
    });	
}
