// Core Javascript
//youtube playlist for trailers
var yt_url = "http://gdata.youtube.com/feeds/api/playlists/B262781B299E6946?alt=json-in-script&max-results=4&callback=?";

$(function() {
    //random bg
    var num_bgs=26;
    var nbg=Math.floor(Math.random()*num_bgs)+1;
    var bgfile="yellow_bg_" + nbg + ".jpg";
    $("body").css("background", "#000 url(images/bgs/" + bgfile + ") no-repeat 50% 0");

	// Home page 
	$("#featured").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true);
	$("#featured").hover(function() { $("#featured").tabs("rotate",0,true); },	function() { $("#featured").tabs("rotate",5000,true); });
	
	$('.ThreeColButn li a[href="javascript:void(0) "]').css("margin-left", "0")

	
// YOUTUBE?
    //if ($('#youtube'))
//        doHomeYoutube();

	//thumb scroller
	if ($('#thumbscroll'))
	    $('#thumbscroll').hoverscroll();

	// FAQ's - Artists - cast
	$('h2.title + div').hide();
	
	$("#nav-main li li.current a").parent().parent().parent().addClass('current');
	
	// Need to figure out for Social
	//var pathname = window.location.pathname.split("/")[window.location.pathname.split("/").length - 1];
	//alert(pathname);
	


	$('h2.title').click(function() {
		$(this).toggleClass("open");
		var $nextDiv = $(this).next();
		var $visibleSiblings = $nextDiv.siblings('h2.title + div:visible');
		if ($visibleSiblings.length ) {
			$visibleSiblings.slideUp('fast', function() {
				$nextDiv.slideToggle('fast');
			});
		} else {
			$nextDiv.slideToggle('fast');
		}
	});
	
	$('#frm-join input[title], #frm-search input[title], #searchFrm input[title]').each(function() {
			if($(this).val() === '') {
				$(this).val($(this).attr('title'));	
			}
			
			$(this).focus(function() {
				if($(this).val() == $(this).attr('title')) {
					$(this).val('');	
				}
			});
			$(this).blur(function() {
				if($(this).val() === '') {
					$(this).val($(this).attr('title'));	
				}
			});
		});
		
		$('#search-tabs').tabs({
				load: function(event, ui) {
						$('a', ui.panel).click(function() {
								$(ui.panel).load(this.href);
								return false;
						});
				}
		});


	// Allow modal popups
	$('#modalWin').jqm(); 
	
	// Pager Sub Nav Add Pipe Character
	$(".pager .subViews li:not(:last)").append("| ");


	// Puff boxes

	// hide all but first
	$('div.expando>div.content').each(function() { $(this).css('display', 'none'); });
	//$('div.expando h2').addClass('open');


	// add click to rest
	$('.expando h2 a').click(function()	{
		if ($(this).parent().hasClass('open'))		{
			$(this).parent().removeClass('open');
			$(this).parent().parent().find('.content').css('display', 'none');
		}else{
			$(this).parent().addClass('open');
			$(this).parent().parent().find('.content').css('display', 'block');

		}
	});
	

});


function doHomeYoutube() {

    jQuery.getJSON(yt_url,
        function(data) {

            $.each(data.feed.entry, function(i, item) {
                var title = item['title']['$t'];
                var video = item['id']['$t'];
                var flash = item['media$group']['media$content'][0].url;
                
                //this would link to vid on youtuibe, we use the flash
                //video = video.replace('http://gdata.youtube.com/feeds/api/videos/', 'http://www.youtube.com/watch?v=');
                var thumb = item['media$group']['media$thumbnail'][0].url;

                if (i == 0) {
                    //first item
                    $('#youtube').html('<div id="playPos"><div class="overlay"><a title="Click to view ' + title + '" href="javascript:showPopupVideoYT(\'' + flash + '\')" id="productionVideoMain"><img src="images/play_button.png" alt="Play button" /></a></div><img alt="' + title + ' thumbnail" src="' + thumb + '" id="prodImg" width="218" /></div><div id="galleryThumbs">');
                }
                else {
                    //remaining items
                    $('#youtube').append('<a href="javascript:changeMainProductionImage(\'' + flash + '\', \'' + thumb + '\')" title="' + title + '" ><img alt="' + title + ' thumbnail" src="' + thumb + '" width="72" /></a>');                       
                }

            });
            
            $('#youtube').append('</div>');

        });

}


function changeMainProductionImage(video, image)	{
	$('#playPos a').attr('href', 'javascript:showPopupVideoYT(\'' + video + '\')');
	$('#prodImg').attr('src', image);
}


// Creates the modal overlay for video clips
function showPopupVideo(vid)	{
    showPopupVideoPrefix(vid, '/content/');
}

function showPopupVideoYT(vid) {
    showPopupVideoPrefix(vid, '');
}

function showPopupVideoPrefix(vid, pre) {
    // Stick the current content into the lightbox
    $.get('/content/mediaassets/tpl/Para - Flash movie embed.tpl', function(data) {

        $('#modalWin').html(data)

        var flashvars = {};
        var params = {};
        var attributes = {};
        swfobject.embedSWF(pre + vid, "myAlternativeContent", "320", "240", "8.0.0", false, flashvars, params, attributes);

    });

    $('#modalWin').jqmShow();
}

// Creates the modal overlay for video clips
function showEmbedVideo(vid, embedWhere, title, description)	{
	// Stick the current content into the lightbox
	$.getJSON('/content/mediaassets/tpl/Para - Flash movie embed details.tpl',	function(data){	
		//alert(data);
		data = data.replace('%%vid%%', vid);
		data = data.replace('%%vid2%%', vid);
		data = data.replace('%%title%%', title);
		data = data.replace('%%desc%%', description);
		
		$('#' + embedWhere).html(data)
	});
}
