﻿var $jq = jQuery.noConflict();

function activate_tab(e) {
	var $target = $jq(e.target);
	$box = $target.parents(".box-tab");
	// Display the proper div
	$box.find(".content > div").hide();
	$jq($target.attr('href')).show();
	// No tabs are active except the clicked one
	$box.find(".header ul li").removeClass("active");
	$target.parents(".header li").addClass("active");
	
	// Bind the event listeners
	$box.find(".header ul li a").bind("click", activate_tab);
	$target.unbind('click');
	$target.bind('click',prevent_default);
	
	prevent_default(e);
}

function prevent_default(e) {
    // This prevents screen jumps from accessing # url's
    e.preventDefault();
    return false;
}

function box_init(i) {
	// Get the target
	$target = $jq(this);
		
	// Show only the first tab
	$target.find(".content > div").hide();
	$target.find(".content > div:first").show();

	// Bind the event listener to all but the first tab.
	// inactive_tab listener is just to prevent the default click action
	$target.find(".header ul li a").bind("click", activate_tab);
	$target.find(".header ul li:first a").unbind("click");
	$target.find(".header ul li:first a").bind("click", prevent_default);

	// Make the first tab display as active
	$target.find(".header ul li:first").addClass("active");

	// Tab display width
	var tabCount = $target.find(".header ul li").size();
	var width = 100 / tabCount;
	$target.find(".header ul li").css("width", width + "%");
}

$jq(document).ready(function() {
    // Initialize tabs
    $jq(".box-tab").each(box_init);

    // slideshow
    $jq("#slideshow-pause").bind("click", pauseShow);
    $jq("#slides").cycle({
        fx: 'cover',
        speed: 0,
        timeout: 13000,
        next: '#slideshow-forward',
        prev: '#slideshow-backward',
        fit: 1,
        sync: 'false',
        pager: '#slideshow .slideshow-control-inner .links'
    });
    var width = $jq(".slideshow-container").innerWidth() - $jq(".slideshow-control").innerWidth();
    $jq("#slides .slide p").css({ 'width': width });

    $jq("a.flowplayer").append("<img class=\"play\" src=\"http://flowplayer.org/img/player/btn/play.png\" />");

    // initialize any flash video on the page
    flowplayer("a.video", "/media/flowplayer.commercial-3.1.0.swf", {
        key: '#$1cdba038c9891475a26',
        clip: {
            autoBuffering: true,
            autoPlay: false,
            bufferLength: 5
        }
    });

    flowplayer("a.videoAuto", "/media/flowplayer.commercial-3.1.0.swf", {
        key: '#$1cdba038c9891475a26',
        clip: {
            autoBuffering: true,
            autoPlay: true,
            bufferLength: 5
        }
    });

    // Initialize the email modal box
    var m = $jq('#email_modal').dialog({
        autoOpen: false,
        modal: true,
        width: 300,
        height: 380,
        title: 'Email a Friend',
        bgiframe: false,
        position: 'middle'
    });
    // Move the modal box inside the form so it posts to the asp page.
    $jq('#email_modal').parent().parent().appendTo($jq("form:first"));

    $jq('.emailShare').click(function(e) {
        if ($jq(this).attr('href') && $jq(this).attr('href') != "#") {
            $jq("#" + email_url).attr('value', $jq(this).attr('href'));
        }
        $jq('#email_modal').dialog('open');
        prevent_default(e);
    });
});

function pauseShow(e) {
    e.preventDefault();
    if ($jq('#slideshow-pause').hasClass('playing')) {
        $jq('#slides').cycle('pause');
    } else {
        $jq('#slides').cycle('resume');
    }
    $jq('#slideshow-pause img').toggle();
    $jq('#slideshow-pause').toggleClass('playing');
}

function textClear(id) {
    var el = $jq("#" + id)
    if (el.data("cleared")) {
        return;
    }

    el.val("").data("cleared", true);
}
