﻿var newsitems;
var news_index = -1;
var clicked = false;

$(document).ready(function () {
	// add events for hover nav
    $("#mainNav > li").each(function() {
        $(this).mouseover(function() {$(this).addClass("sfhover");});
        $(this).mouseout(function() {$(this).removeClass("sfhover");});

        /*var div = $(this).find("div")      
        var col1 = $(this).find(".col1");
        var col2 = $(this).find(".col2");
        var col3 = $(this).find(".callout-col");
        var height = col1.height();
        alert(height);
        if (col2.height() > height) height = col2.height();        alert(height);
        if (col3.height() > height) height = col3.height();        alert(height);
        
		div.css("height", height);*/        
    });
	
	// add event to close buttn
	$("#mainNav .close-icon").each(function() {
		$(this).click(function() {
			var li = $(this).parents("li").removeClass("sfhover");			
		});
	});
	
	// add flyout to flyout left
	$("#nav li.flyleft").each(function() {
        var div = $(this).find("div");
        var marginLeft = div.width() + 20 - 4 - $(this).width() + 18;
        
        div.css('margin-left', '-' + marginLeft + 'px');
    });
    
    // add hover effect to rollup
    $("div.rollup").hover(
        function()  {
            $(this).addClass("rollup-hover");
        },     
        function() {
            $(this).removeClass("rollup-hover");            
        }
    );
   
	// animate latest news
    newsitems =  $('#ln-container').find('.ln-newslist > li > a');
	animateLatestNews();
 
 
 	// add view more link to web parts
	$('.ms-WPHeaderTd').each(function() {
	    var title = $(this).attr('title');
	    var titleIndex = title.indexOf(' - ');

	    if (titleIndex != -1) {
            $(this).attr('title', title.substring(0, titleIndex));
	        title = title.substring(titleIndex + 3);
	        if ($(this).find('a').length) {
	        	var link = $(this).find('a');
	            var url = link.attr('href');
				$(this).find('h3').html(link.html());
	            $(this).parent().append('<td class="view-more"><a href="' + url + '">' + title + '</a></td>');
	        }
	    }        
	}); 
});


 function ZoneToTabs(id) {
 	id = id.substring(0, id.lastIndexOf('_'));

    // grab the current webpart
    $wp = $(id);
    // finds the parent container for the current webpart
    $parent = $wp.parents("td.s4-wpcell-plain").parent();
    // get webpart id for uniqueness
    $id = $wp.attr("id");

    $titles = new Array();
    $contents = new Array();

    // loop through each other webpart in the zone
    $parent.siblings().each(function () {
        // grab the webpart title
        $titles.push($(this).find(".ms-WPTitle").text().trim());
        // grab the webpart contents
        $contents.push($(this).find(".ms-WPBody").html());
        // hide the webpart
        $(this).hide();
    });

    // create tab html
    $wp.html(BuildTabOutput($id, $titles, $contents));
    // convert to tabs
    $("#tabs-" + $id).tabs();
}

function BuildTabOutput($uid, $titles, $content) {
	$output = '<div id="tabs-' + $uid + '">';
	$output += '<ul>';
	for ($i = 0; $i < $titles.length; $i++) {
		$output += '<li><a href="#tab-' + $uid + '-' + $i + '">' + $titles[$i] + '</a></li>';
	}
	$output += '</ul>';
	for ($i = 0; $i < $content.length; $i++) {
		$output += '<div id="tab-' + $uid + '-' + $i + '"><p>' + $content[$i] + '</p></div>';
	}
	$output += '</div>';
	
	return $output;
}

function animateLatestNews() {	
	if ($('#ln-content').length) {
		if (clicked) {
			clicked = false;
		}
		else {
			displayLatestNewsItem(1, false);		
		}
		setTimeout("animateLatestNews()", 7000);	
	}
}
function displayLatestNewsItem(dir, userClicked) {
	if (userClicked)
		clicked = true;
	news_index += dir;
	if (news_index >= newsitems.length) news_index = 0;
	if (news_index < 0) news_index = newsitems.length - 1;

	if (dir > 0) {
	    $('#ln-content').hide('slide', { direction: "left" }, 500);
	}
	else {
	    $('#ln-content').hide('slide', { direction: "right" }, 500);
	}
	
	$('#ln-title').html(getLnTitle()).attr('href', getLnUrl());
	$('#ln-date').html(getLnDate());
	$('#ln-category').html(getLnCategory());

	if (dir > 0) {
	    $('#ln-content').show('slide', { direction: "right" }, 500);
	}
	else {
	    $('#ln-content').show('slide', { direction: "left" }, 500);
	}	
}
function getLnTitle() {	return newsitems[news_index].innerHTML;	}
function getLnDate() { return $('#ln-container').find('.ln-newslist > li').eq(news_index).find('.ln-itemdate').html(); }
function getLnUrl() { return newsitems[news_index].href; }
function getLnCategory() { return $('#ln-container').find('.ln-newslist > li').eq(news_index).find('.ln-itemcategory').html(); }

