//*************************************************************************************************************
//*************************************************************************************************************
//this function should resolve issues related to browsing on IPAD, IPhone, etc.
function FixRibbonAndWorkspaceDimensions()
{ULSxSy:;
    g_frl=true;
    var elmRibbon=GetCachedElement("s4-ribbonrow");
    var elmWorkspace=GetCachedElement("s4-workspace");
    var elmTitleArea=GetCachedElement("s4-titlerow");
    var elmBodyTable=GetCachedElement("s4-bodyContainer");
    if(!elmRibbon ||
       !elmWorkspace ||
       !elmBodyTable)
    {
        return;
    }
    if(!g_setWidthInited)
    {
        var setWidth=true;
        if(elmWorkspace.className.indexOf("s4-nosetwidth") > -1)
            setWidth=false;
        g_setWidth=setWidth;
        g_setWidthInited=true;
    }
    else
    {
        var setWidth=g_setWidth;
    }
    var baseRibbonHeight=RibbonIsMinimized() ? 44 : 135;
    var ribbonHeight=baseRibbonHeight+g_wpadderHeight;
    if (GetCurrentEltStyle(elmRibbon, "visibility")=="hidden")
    {
        ribbonHeight=0;
    }
    if(elmRibbon.children.length > 0 && document.getElementsByTagName("html")[0].className.indexOf('ms-dialog-nr') == -1){
	    elmWorkspace.style.paddingTop = ribbonHeight + 'px';
	}
}
//*************************************************************************************************************
//*************************************************************************************************************

$(document).ready(function() {
	sqtLoadFontSizeFromCookie();
	$('#pfLink').attr('href', "http://www.printfriendly.com/print/new?url=" + escape(window.location));
	BuildTabs();
	BuildDeptTabs();
	HotTopics();
	SetFooterSiteMap();
	AddFBLike();
	//GenBunchaFunLingo();
});

function GenBunchaFunLingo()
{
	var arrayItems = randomFromTo(0, 14);
	
	var lingoWords = new Array();
	lingoWords[0] = 'Bunchalicious';
	lingoWords[1] = 'Bunchafried';
	lingoWords[2] = 'Bunchascore';
	lingoWords[3] = 'Buncharight';
	lingoWords[4] = 'Bunchasounds';
	lingoWords[5] = 'Bunchasick';
	lingoWords[6] = 'Bunchagrowl';
	lingoWords[7] = 'Buncha-Buncha';
	lingoWords[8] = 'Bunchafun';
	lingoWords[9] = 'Bunchacrush';
	lingoWords[10] = 'Bunchapretzels';
	lingoWords[11] = 'Bunchatight';
	lingoWords[12] = 'Bunchalingo';
	lingoWords[13] = 'Bunchalater';
	lingoWords[14] = 'Bunchalame';
	
	var lingodefs = new Array();
	lingodefs[0] = 'Ice cream is delicious. Fried ice cream is bunchalicious';
	lingodefs[1] = 'Overdoing it with the fried food';
	lingodefs[2] = 'Winning a prize or getting a great deal on something';
	lingodefs[3] = 'Affirmation of the highest degree, like \"right on.\"';
	lingodefs[4] = 'Good music, a concert';
	lingodefs[5] = 'Teens: Wickedly cool; Parents: The feeling after a thrill ride';
	lingodefs[6] = 'The noise a stomach makes when it needs another funnelcake';
	lingodefs[7] = 'Admiration or praise of someone or something awesome';
	lingodefs[8] = 'It\'s what happens at the Cabarrus County Fair';
	lingodefs[9] = 'Tight crowd at a concert or line at a popular midway ride';
	lingodefs[10] = 'A bunch of pretzels';
	lingodefs[11] = 'A close bond between friends';
	lingodefs[12] = 'The official language of the 2011 Cabarrus County Fair';
	lingodefs[13] = 'Boy: \"Can I see you later?\" Girl: \"Yeah...bunchalater.\"';
	lingodefs[14] = 'Not good; sad; pitiful; e.g. not going to the fair';
	
	try {
		document.getElementById('lingoword').innerHTML = lingoWords[arrayItems];
	}
	catch (err){
	
	}
	
	try {
		document.getElementById('lingodef').innerHTML = lingodefs[arrayItems];
	}
	catch (err){
	
	}	
}

function randomFromTo(from, to)
{
    return Math.floor(Math.random() * (to - from + 1) + from);
}


function tweetLingo()
{
	window.open('http://twitter.com/home?status=Cabarrus County Fair Lingo - ' + document.getElementById('lingoword').innerHTML + ' - ' + document.getElementById('lingodef').innerHTML);
	return false;
}

function listDialog(listURL, lWidth, lHeight, lTitle)
{
	var options = SP.UI.$create_DialogOptions();
    options.url = listURL;
    options.width = lWidth;
    options.height = lHeight;
    options.title = lTitle;
    SP.UI.ModalDialog.showModalDialog(options);
}

function AddFBLike()
{

	var pathname = window.location;
	pathname = encodeURIComponent(pathname);
		
	var ur = 'http://www.facebook.com/plugins/like.php?href=';
	ur = ur + pathname;
	ur = ur + '&amp;layout=button_count&amp;show_faces=true&amp;width=200&amp;action=recommend&amp;colorscheme=dark&amp;height=21';

	//Set iframe url
	$('#fbiframe').attr('src', ur);
}

function BuildTabs() {
	var tabsHTML = "";
	var tabContainersHTML = "";
	$('#tabs').each(function() {
		//$('#content-left-zone .s4-wpcell-plain,#content-left-zone .s4-wpcell').each(function(index) {
		$('#content-left-zone .s4-wpcell-plain').each(function(index) {
			$(this).hide();
			var title = $(this).find('.ms-WPTitle').text();
			var id = index;
			var body = $(this).find('.ms-wpContentDivSpace').html();
			tabsHTML  += '<li class="tab" id="tab_'+id+'"><div>'+'<div class="tab-center">'+title+'</div>'+'</div></li>';
			tabContainersHTML += ''+'<div class="tab-container" style="display:none;" id="tab-container_'+id+'">'+body+'</div>';
		});
	});
	
	$('#tabs .tab-tabs').html('<ul>'+tabsHTML+'</ul><div class="clear"></div>');
	$('#tabs .tab-content-middle-center').html(tabContainersHTML);
	
	// show first tab
	$('#tabs .tab-container:first').show();
		$('#tabs .tab:first').addClass('active');
		
	// add click to show tab
	$('#tabs .tab').mouseover(function() {
		$('#tabs .tab-container').hide();
		$('#tabs .tab').removeClass('active');
		
		var id = $(this).attr('id').replace('tab_','');
		$(this).addClass('active');
		$('#tab-container_'+id).show();
	});

	
}

function BuildDeptTabs() {
	var tabsHTML = "";
	var tabContainersHTML = "";
	var ZoneFound = false;
	
	//Need to hide webpart on published page, but not in edit mode.
	$('.ms-SPZoneTitle').each(function(index) {
		var TopZoneTitle = $(this).text();
			
		//alert(TopZoneTitle);
			
		if (TopZoneTitle == 'TopZone')
		{
			ZoneFound = true;
		}
			
	});
	
	$('#Depttabs').each(function() {
		$('#content-top-zone .s4-wpcell').each(function(index) {
			if (ZoneFound == false)
			{
				//Only hide webpart if we are not in edit mode (TopZone container found)
				$(this).hide();
			}
			
			var title = $(this).find('.ms-WPTitle').text();
			var id = index;
			var body = $(this).find('.ms-wpContentDivSpace').html();
			tabsHTML  += '<li class="Depttab" id="Depttab_'+id+'"><div>'+'<div class="Depttab-center">'+title+'</div>'+'</div></li>';
			tabContainersHTML += ''+'<div class="Depttab-container" style="display:none;" id="Depttab-container_'+id+'">'+body+'</div>';
		});
		
		$('#content-top-zone .s4-wpcell-plain').each(function(index) {
			$(this).hide();
			
			var title = $(this).find('.ms-WPTitle').text();
			var id = index;
			var body = $(this).find('.ms-WPBody').html();
			tabsHTML  += '<li class="Depttab" id="Depttab_'+id+'"><div>'+'<div class="Depttab-center">'+title+'</div>'+'</div></li>';
			tabContainersHTML += ''+'<div class="Depttab-container" style="display:none;" id="Depttab-container_'+id+'">'+body+'</div>';
		});
		
	});	
	
	$('#Depttabs .Depttab-tabs').html('<ul>'+tabsHTML+'</ul><div class="clear"></div>');
	$('#Depttabs .Depttab-content-middle-center').html(tabContainersHTML);
	
	// show first tab
	$('#Depttabs .Depttab-container:first').show();
		$('#Depttabs .Depttab:first').addClass('Deptactive');
		
	// add click to show tab
	$('#Depttabs .Depttab').mouseover(function() {
		$('#Depttabs .Depttab-container').hide();
		$('#Depttabs .Depttab').removeClass('Deptactive');
		
		var id = $(this).attr('id').replace('Depttab_','');
		$(this).addClass('Deptactive');
		$('#Depttab-container_'+id).show();
	});

	
}


function HotTopics(){
	if($('.hot-topic').size() > 0){
		$('.hot-topic:eq(0)').parent('li').parent('ul').wrap('<div id="sqt-hot-topics"/>');
		$('.hot-topic-description').each(function(i){
			if(i > 0){
				$(this).hide();
			}
			else{
				$('.hot-topic-title:eq(' + i + ')').addClass('active');
			}
			$(this).attr('rel', 'ht-' + i);
			$('.hot-topic-title:eq(' + i + ') a').attr('rel', 'ht-' + i);
			var theLink = $('.hot-topic-title:eq(' + i + ') a').attr('href');
			$(this).find('.hot-topic-abstract').append('<div class="hot-topic-more"><a href="' + theLink + '">More...</a></div>');
			
			var theDesc = $(this).detach();
			$('#sqt-hot-topics').append(theDesc);
		});
		$('.hot-topic-title a').click(function(e){
			e.preventDefault();
			$('.hot-topic-description').hide();
			$('.hot-topic-description[rel="' + $(this).attr('rel') + '"]').show();
			$('.hot-topic-title').removeClass('active');
			$(this).parent().addClass('active');
		});
	}
}

function emailFriend() {
	var email = "mailto:?subject=Cabarrus County:  " + document.title;
	email += "&body=I thought you might be interested in this article on CabarrusCounty.us: " + document.title;
	email += ". View the article by clicking here:  " + location.href; 
	location.href = email;
}


// banner

// Define banner constants
var bannerWidth = 960;
var autoScrollTime = 10000;
var scrollTimer = setTimeout('void(0);');
var itemCount = 0;

// Init banner data/HTML
$(document).ready(function(){
	if($('.bannerData').size() > 0){
		$('.bannerData').each(function(i){
			var bannerDiv = $('<div class="featuredBanner"/>');
			if(i > 0){
				$(bannerDiv).hide();
			}
			//var bannerLink = $('<a/>');
			//$(bannerLink).attr('href', $(this).text().split(';;')[1]);
			var bannerImage = $('<img/>');
			$(bannerImage).attr('src', $(this).text().split(';;')[0]);
			$(bannerImage).attr('alt', $(this).text().split(';;')[2]);
			//$(bannerImage).appendTo(bannerLink);
			//$(bannerLink).appendTo(bannerDiv);
			$(bannerImage).appendTo(bannerDiv);
			$(bannerDiv).appendTo('.homeFeatured');
		});
		scrollTimer = setTimeout("nextBanner();", autoScrollTime);
	}
});

// Next banner image
function nextBanner(){
	var nextBanner = $('.featuredBanner:visible').next('.featuredBanner');
	if($(nextBanner).size() == 0){
		nextBanner = $('.featuredBanner:eq(0)');
	}
	$('.featuredBanner:visible').fadeOut(1000);
	$(nextBanner).fadeIn(1000, function(){
		scrollTimer = setTimeout("nextBanner();", autoScrollTime);
	});
}

// Go to banner image
function goToBanner(i){
	if(allowScroll){
		clearTimeout(scrollTimer);
		var currentWidth = $('.homeFeaturedInner').width();
		var newInnerX = (i * bannerWidth) * -1;
		if(newInnerX <= currentWidth * -1){
			newInnerX = 0;
		}
		$('.homeFeaturedNavigation li').removeClass('selected');
		$('.homeFeaturedNavigation li:eq(' + i + ')').addClass('selected');
		allowScroll = false;
		$('.homeFeaturedInner').animate({ left: newInnerX + 'px' }, 1000, function(){
			allowScroll = true;
		});
		scrollTimer = setTimeout("nextBanner();", autoScrollTime);
	}
}


function SetFooterSiteMap() {

	var navHtml = "";
	$('#mainNav ul.root ul.static li.static').each(function() {
		navHtml += '<div class="site-map-subsite">' + $(this).html() + '</div>';
	});
	
	$('#footer-site-map').html(navHtml+'<div class="clear"></div>');

}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}

function sqtResize(theSize){
	switch(theSize){
		case 's':
			createCookie('sqtfontsize', theSize, 365);
			$('#masterColumnLayout,#masterColumnLayout .ms-WPBody').css('font-size', '11px');
			$('.font-resize-link').removeClass('active');
			$('#font-resize-s').addClass('active');
			break;
		case 'l':
			createCookie('sqtfontsize', theSize, 365);
			$('#masterColumnLayout,#masterColumnLayout .ms-WPBody').css('font-size', '17px');
			$('.font-resize-link').removeClass('active');
			$('#font-resize-l').addClass('active');
			break;
		default:
			createCookie('sqtfontsize', 'm', 365);
			$('#masterColumnLayout,#masterColumnLayout .ms-WPBody').css('font-size', '13px');
			$('.font-resize-link').removeClass('active');
			$('#font-resize-m').addClass('active');
	}
}

function sqtLoadFontSizeFromCookie(){
	// add font size tools for javascript-enabled users
	$('#font-size').append('<ul id="font-resize-tools"><li class="label">Font Size:</li><li><a id="font-resize-s" class="font-resize-link" href="javascript:sqtResize(\'s\');">a</a></li><li><a id="font-resize-m" class="font-resize-link" href="javascript:sqtResize(\'m\');">a</a></li><li><a id="font-resize-l" class="font-resize-link" href="javascript:sqtResize(\'l\');">A</a></li></ul>');
	
	// read size cookie
	var theSize = readCookie('sqtfontsize');
	if(theSize != null){
		sqtResize(theSize);
	}else{
		sqtResize('m');
	}
}


var secondsPerMinute = 60;
var minutesPerHour = 60;
											
function convertSecondsToHHMMSS(intSecondsToConvert) {
	var hours = convertHours(intSecondsToConvert);
	var minutes = getRemainingMinutes(intSecondsToConvert);
	minutes = (minutes == 60) ? "00" : minutes;
	var seconds = getRemainingSeconds(intSecondsToConvert);
	return hours; //+":"+minutes;
}
											
function convertHours(intSeconds) {
	var minutes = convertMinutes(intSeconds);
	var hours = Math.floor(minutes/minutesPerHour);
	return hours;
}
											
function convertMinutes(intSeconds) {
	return Math.floor(intSeconds/secondsPerMinute);
}
											
function getRemainingSeconds(intTotalSeconds) {
	return (intTotalSeconds%secondsPerMinute);
}
											
function getRemainingMinutes(intSeconds) {
	var intTotalMinutes = convertMinutes(intSeconds);
	return (intTotalMinutes%minutesPerHour);
}
											
function HMStoSec1(T) { 
	// h:m:s
	var A = T.split(/\D+/) ; return (A[0]*60 + +A[1])*60 + +A[2] 
}

function calcDays(){
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	var startdate = month + "-" + day + "-" + year;
	var date1 = startdate;
	var date2 = '9-7-2012';
	date1 = date1.split("-");
	date2 = date2.split("-");
	var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
	var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
	var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
	return daysApart;
}						
