$(document).ready(function () {
    $('body').addClass('has_js');

    // Initiate jQuery UI tabs for event details
    $('.events.details #content-secondary #tabs').tabs();

    // Setup highlighted event for events listing
    $('#upcoming-events-container .event.featured').css('min-height', $('ul#upcoming-events').height() - 40);
    eventHighlight($('ul#upcoming-events li:first-child'));
    $('ul#upcoming-events li').click(function () {
        eventHighlight($(this));
        return false;
    });

    $('#news-header, #contact-header, #related-header, #clients-header').click(function () {
        $(this).toggleClass('closed');
        $(this).parent().find('div[id*="-container"]').slideToggle('slow');
    });

    // $('#news-latest li, #news-headlines li, #news-in-depth li, #previous-events li, #upcoming-events li').hover(function() {
    // 	$(this).toggleClass('hover');
    // });
    //$('#news-latest li, #previous-events li, #upcoming-events li').hover(function() {
    //    $(this).toggleClass('hover');
    //});
    // $('#news-latest li, #news-headlines li, #news-in-depth li, #previous-events li').click(function() {
    // 	window.location = ($(this).find("a").attr("href"));
    // });

    $('#news-latest li, #news-in-depth li, #previous-events li, #upcoming-events li').hover(function () {
        $(this).toggleClass('hover');
    });
    $('#news-latest li, #news-in-depth li, #previous-events li').click(function () {
        if ($(this).find("a").attr("target") == '_blank') {
            window.open($(this).find("a").attr("href"));
            return false;
        } else {
            window.location = ($(this).find("a").attr("href"));
        }
    });

    // Setup news ticker
    if ($('#news-bar').length) {
        var options = {
            newsList: "#ticker-stories",
            startDelay: 10,
            placeHolder1: " _",
            placeHolder2: " _",
            controls: true,
            ownControls: true,
            resumeOffHover: true
        }
        $().newsTicker(options);
    }

    // Setup news slider
    newssliderinit();

    contactUs();

    ///////////////////
    // ZENITH CUSTOM //
    ///////////////////

    // Setup highlighted client for client listing
    $('#upcoming-events-container .event.featured').css('min-height', $('ul#upcoming-events').height() - 40);
    //clientHighlight($('div.clients-featured div:first-child'));
    //$('div.clients-featured div').click(function () {
    //    clientHighlight($(this));
    //    return false;
    //});
});

// Highlight event on events listing
function eventHighlight(obj) {
	$('ul#upcoming-events li.active').removeClass('active');
	obj.addClass('active');
	$('#upcoming-events-container .event.featured').html(obj.html());
	//$('#upcoming-events-container .event.featured h3 a').contents().unwrap();
}

// News slider from http://www.queness.com/post/443/jquery-image-gallerynews-slider-with-caption-tutorial
function newssliderinit() {
	//Speed of the slideshow   (millisecond delay)
    var speed = 15000;
     
    //You have to specify width and height in #slider CSS properties
    //After that, the following script will set the width and height accordingly
    $('#mask-gallery, #gallery li').width($('#slider').width());   
    $('#gallery').width($('#slider').width() * $('#gallery li').length);
    $('#mask-gallery, #gallery li, #mask-excerpt, #excerpt li').height($('#slider').height());
     
    //Assign a timer, so it will run periodically
    var run = setInterval('newsslider(0)', speed); 
     
    $('#gallery li:first, #excerpt li:first, #slider-headings li:first').addClass('selected');
 
    //Pause the slidershow with clearInterval
    $('#btn-pause').click(function () {
        clearInterval(run);
        return false;
    });
 
    //Continue the slideshow with setInterval
    $('#btn-play').click(function () {
        run = setInterval('newsslider(0)', speed);
        return false;
    });
     
    //Next Slide by calling the function
    $('#btn-next').click(function () {
        newsslider(0);
		clearInterval(run);
		run = setInterval('newsslider(0)', speed);
        return false;
    });
 
    //Previous slide by passing prev=1
    $('#btn-prev').click(function () {
        newsslider(1);
		clearInterval(run);
		run = setInterval('newsslider(0)', speed);
        return false;
    });
     
    //Mouse over, pause it, on mouse out, resume the slider show
    $('#slider').hover(     
        function() {
            clearInterval(run);
        },
        function() {
            run = setInterval('newsslider(0)', speed); 
        }
    );	
}

function newsslider(prev) {
 
    //Get the current selected item (with selected class), if none was found, get the first item
    var current_image = $('#gallery li.selected').length ? $('#gallery li.selected') : $('#gallery li:first');
    var current_excerpt = $('#excerpt li.selected').length ? $('#excerpt li.selected') : $('#excerpt li:first');
	var current_title = $('#slider-headings li.selected').length ? $('#slider-headings li.selected') : $('#slider-headings li:first');
 
    //if prev is set to 1 (previous item)
    if (prev) {
         
        //Get previous sibling
        var next_image = (current_image.prev().length) ? current_image.prev() : $('#gallery li:last');
        var next_excerpt = (current_excerpt.prev().length) ? current_excerpt.prev() : $('#excerpt li:last');
		var next_title = (current_title.prev().length) ? current_title.prev() : $('#slider-headings li:last');
     
    //if prev is set to 0 (next item)
    } else {
         
        //Get next sibling
        var next_image = (current_image.next().length) ? current_image.next() : $('#gallery li:first');
        var next_excerpt = (current_excerpt.next().length) ? current_excerpt.next() : $('#excerpt li:first');
		var next_title = (current_title.next().length) ? current_title.next() : $('#slider-headings li:first');
    }
 
    //clear the selected class
    $('#excerpt li, #gallery li, #slider-headings li').removeClass('selected');
     
    //reassign the selected class to current items
    next_image.addClass('selected');
    next_excerpt.addClass('selected');
	next_title.addClass('selected');
 
    //Scroll the items
    $('#mask-gallery').scrollTo(next_image, 800);
    $('#mask-excerpt').scrollTo(next_excerpt, 800);
     
}

function contactUs() {
	
	$("#nav-secondary li.contact a").live("click", function(){
		
		$.scrollTo($("#contact-social"), 600);
		
		return false;
		
	});

}

///////////////////
// ZENITH CUSTOM //
///////////////////


// Highlight client on client listing
function clientHighlight(obj) {
    $('div.clients-featured div.active').removeClass('active');
    obj.addClass('active');
    $('div.clients-detail').html(obj.html());
    $('clients-detail a').contents().unwrap();
}
