﻿/* Global variables */

// Constants
var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var years = ["2008","2009"];
var now = new Date(); // Current month
now.setDate(1);

// Production variables
var proid;
var getEntaShoid;
var venueid;
var getEntaVenueid;
var getPromoCatid;

// Slider
var stepSize = 30;                          // How big is the monthly increment?
var initDate = new Date(2008,00,01);        // This is the date the slider graphic starts from

var startPos = now.getMonth() + ((now.getFullYear() - initDate.getFullYear()) * 12);
var endPos = startPos + 3;

//var productionVideo = { video: "grease3_h.flv", image: "/uploads/media/67/2081.jpg" };


/* Init */

function productionsInit() {

    // Production variables
    proid = document.getElementById("getid").value;
    getEntaShoid = document.getElementById("getEntaShoid").value;
    venueid = document.getElementById("getVenueid").value;
    getEntaVenueid = document.getElementById("getEntaVenueid").value;
    getPromoCatid = document.getElementById("getPromoCatid").value;
    
    
    var videoSettings = {
        streamer: "rtmp://83.142.31.91/vod/",
        file: "atg_cms/" + productionVideo.video,
        image: productionVideo.image, 
        skin: "/flash/skin-2.0.swf",
        controlbar: "over",
        backcolor: "666666",
        lightcolor: "0099ff",
        frontcolor: "ffffff",
        displayclick: "none",
        bufferlength: 10,
        stretching: "uniform"
    };


    // Load production carousel if there is more than one image
    if(productionVideo != "") {
        $("#production_mainimg").html('<div id="video"></video>');
        swfobject.embedSWF("/flash/player-4.2.swf", "video", "512", "366", "9.0.15","",videoSettings,{ wmode: "opaque", allowScriptAccess: "always", allowFullScreen: "true" },{ name: "video" });
    } else if(productionImages.length > 1) {
		var prodimages = "";
		for(i = 0; i < productionImages.length; i++) {
			image = prodGalleryDrawItem(productionImages[i]);
			prodimages = prodimages + "<li>" + image + "</li>";
		}
		$("#production_gallery ul").html(prodimages);
		$(".prev").show();
		$(".next").show();
		$("#production_gallery").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			circular: true,
			visible: 1
		});
  
    } else {
        image = prodGalleryDrawItem(productionImages[0]);
        $("#production_gallery").html(image);
    }
    
    // Load some initial performances results
	$("#listholder .loading").show();
	
	//get lesteat date
	var dateObj=new Date();
    y = dateObj.getFullYear();
    m1 = dateObj.getMonth() + 1;
    m2 = dateObj.getMonth() + 4;
    m1 = m1.toString();
    m2 = m2.toString();
    d = dateObj.getDate().toString();
    
    if(m1.length < 2) {
        m1 = "0" + m1;
    }
    if(m2.length < 2) {
        m2 = "0" + m2;
    }
    if(d.length < 2) {
        d = "0" + d;
    }
    var todayDate = new Date();
    date_start = y + "-" + m1 + "-" + d;
    date_end = y + "-" + m2 + "-" + d;
    default_end_date = (y + 2) + "-12-31";
	//$.post("/performance.aspx", {pid: proid, EntaShoid: getEntaShoid, vid: venueid, EntaVenueid: getEntaVenueid, start_date: date_start, end_date: date_end, PromoCatid: getPromoCatid },drawResults,"html");
    $.post("/performance.aspx", { pid: proid, EntaShoid: getEntaShoid, vid: venueid, EntaVenueid: getEntaVenueid, start_date: formatDate(todayDate), end_date: default_end_date, PromoCatid: getPromoCatid }, drawResults, "html");
	$.post("/get_calendar.aspx", {pid: proid, start_date1: 0, end_date1: 1, PromoCatid: getPromoCatid },drawResults_Cal,"html");
    
    // Set the slider background position
    var bgPos = "-" + (startPos * stepSize) + "px 50%";
    $(".date_range").css("background-position",bgPos);
       
    // Performance list slider
    $("#slider").slider({
        handles: [{start:0},{start:3}],
        range: true,
        change: updatePerformances,
        slide: sliderPosition,
        start: handleDown,
        stop: handleUp,
        min: 0,
        max: 16
    });
    
    // Only load the calendar slider when that tab is enabled for the first time
    var calendarActive = false;
    $('#lowexample').bind('tabsshow', function(event, ui) {
        var current_tab = $(ui.panel).attr("id");
        
        if(current_tab == "tab_calendarview" && calendarActive != true) {
        
            // Calendar view slider
            $("#slider2").slider({
                handles: [{start:0},{start:3}],
                range: true,
                change: updateCalendars,
                slide: sliderPosition,
                start: handleDown,
                stop: handleUp,
                min: 0,
                max: 16
            });
            
            calendarActive = true;
    
        }
    });
	
/************************ Friends tooltip  ******************************/

$('body').append('<div id="friends_tooltip"><div class="tooltip_head"><div class="arrow"><div class="arrow_inner"></div></div></div><div class="tooltip_content"></div></div>');
$('.friends_discount')
	.livequery('mouseover',function(){
		var buttonPos = $(this).offset();
		$('#friends_tooltip .tooltip_content').html($(this).siblings(".friends_tooltip").html())
		$('#friends_tooltip')
			.css("left",buttonPos.left)
			.css("top",buttonPos.top)
			.show();
	})
	.livequery('mouseout',function(){
		$('#friends_tooltip .tooltip_content').html('');
		$('#friends_tooltip')
			.hide();
	})
	.livequery('click',function(){
		return false;						
	});

}


/********************* Date range slider ***********************/

function handleUp(e,ui) {
     $(this).find(".slider_tooltip").hide();
}
 
function handleDown(e,ui) {
     $(this).find(".slider_tooltip").show();
}

function sliderPosition(e,ui) {
    var tooltip = $(this).find(".slider_tooltip");
    var sliderOffset = $(ui.handle).offset();
    var prodlistOffset = $(this).offset();
    var currentMonth = (ui.value+startPos+12)%12;
    
    tooltip.css("left",sliderOffset.left - prodlistOffset.left + 10);
    tooltip.html(months[currentMonth]);
}

function updatePerformances(e,ui) {
    var leftRange = ($(this).slider('value',0));
    var rightRange = ($(this).slider('value',1));
    
    // Create a couple of dates
    var leftDate = new Date(now);
    var rightDate = new Date(now);
    
    // Add the slider values
    leftDate.setMonth(leftDate.getMonth() + leftRange);
    rightDate.setMonth(rightDate.getMonth() + rightRange);
    
    //drawResults("From: " + formatDate(leftDate) +" to "+ formatDate(rightDate));
    $("#listholder .loading").show();
    $.post("/performance.aspx", {pid: proid, EntaShoid: getEntaShoid, vid: venueid, EntaVenueid: getEntaVenueid, start_date: formatDate(leftDate), end_date: formatDate(rightDate), PromoCatid: getPromoCatid },drawResults,"html")
}

function updateCalendars(e,ui) {
//    var leftRange = ($(this).slider('value',0)  + startPos) % 12;
//    var rightRange = ($(this).slider('value',1)  + startPos) % 12;

    var leftRange = ($(this).slider('value',0));
    var rightRange = ($(this).slider('value',1));
    
//    // Create a couple of dates
//    var leftDate = new Date(now);
//    var rightDate = new Date(now);
//    
//    // Add the slider values
//    leftDate.setMonth(leftDate.getMonth() + leftRange);
//    rightDate.setMonth(rightDate.getMonth() + rightRange);

    //console.log(leftRange+","+rightRange);
    $.post("/get_calendar.aspx", {pid: proid, start_date1: leftRange, end_date1: rightRange, PromoCatid: getPromoCatid },drawResults_Cal,"html")

}


function drawResults(data) {
    //remove loading
   $("#listholder .loading").hide();
   $("#listholder .data").html(data);
}


function drawResults_Cal(data) {
    //remove loading

   $(".listholder").html(data);
}

function formatDate(dateObj) {
    // YEAR-MONTH-DAY
    y = dateObj.getFullYear();
    m = dateObj.getMonth() + 1;
    m = m.toString();
    d = dateObj.getDate().toString();
    
    if(m.length < 2) {
        m = "0" + m;
    }
    if(d.length < 2) {
        d = "0" + d;
    }
    
    return y + "-" + m + "-" + d;
}

/************************ Production Gallery ******************************/

function prodGalleryInit(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
    });

    carousel.buttonPrev.bind('click', function() {
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {

    }, function() {
        carousel.startAuto();
    });
};

function prodGalleryVisibleIn(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, productionImages.length);
    carousel.add(i, prodGalleryDrawItem(productionImages[idx - 1]));
};

function prodGalleryVisibleOut(carousel, item, i, state, evt)
{
    //carousel.remove(i);
    // Firefox 2 flickr is caused by the element being removed too quickly
    setTimeout(function() { carousel.remove(i); }, 20);
};

function prodGalleryDrawItem(item)
{
    return '<img src="' + item.url + '" width="512" height="366" alt="' + item.title + '" />';
};


/* Init */
$(document).ready(productionsInit);
    
    