﻿$(document).ready( setupShowLinks );
var theaterCount;
function setupShowLinks() {
    theaterCount = 5;
	$( '#now-showing ul li div' ).css( 'display', 'none' );
	$( '#now-showing a.show' ).click( clickShow );
	$( '#now-showing a.open' ).click( clickShow );
	$( '#now-showing a.go' ).click( loadContent );
	var object = findOpenTab();
	if (object) { openTab(object); }
}
function thZipKeyDown( e, obj,  zipcode ) {
	if (e.keyCode && e.keyCode == 13 && zipcode.length == 5) {
	    var goButton = $(obj).parents('h4').find('.go');
	    if (goButton) {
	        var alreadyOpen = $( goButton ).parents( 'h4' ).find( 'a' ).hasClass( 'open' );
	        if ( !alreadyOpen ) {
		        $( '#now-showing ul li div' ).hide( 'fast' );
		        $( '#now-showing ul li a' ).removeClass( 'open' );
		        $( goButton ).parents( 'h4' ).find( 'a' ).addClass( 'open' );
	        }
	        theaterCount = 5;
	        openTab(goButton);
	    }
	 }
}
function moreTheaters( event ) {
	event.preventDefault();
	theaterCount += 5;
	var object = findOpenTab();
	if (object) { openTab(object); }
}

function dateClicked( event ) {
	event.preventDefault();
	theaterCount = 5;
	var clickedDate = $( this ).attr('id');
	var object = findOpenTab();
	var dateString = findString($( object).attr( 'href' ), 'date_clicked=');
    if (dateString.length > 0) $( object).attr( 'href' , $( object).attr( 'href' ).replace(dateString, 'date_clicked=' + clickedDate));
	if (object) { openTab(object); }
}
function clickShow( event ) {
	event.preventDefault();
	var alreadyOpen = $( this ).hasClass( 'open' );
	$( '#now-showing ul li div' ).hide( 'fast' );
	$( '#now-showing ul li a' ).removeClass( 'open' );
	if ( alreadyOpen ) { $( this ).addClass( 'show' ); return; }
	$( this ).addClass( 'open' );
	var contentArea = $(this).parents( 'li' ).find( 'div' );
	contentArea.show( 'slow' );
}
function loadContent( event ) {
	event.preventDefault();
	var alreadyOpen = $( this ).parents( 'h4' ).find( 'a' ).hasClass( 'open' );
	if ( !alreadyOpen ) {
		$( '#now-showing ul li div' ).hide( 'fast' );
		$( '#now-showing ul li a' ).removeClass( 'open' );
		$( this ).parents( 'h4' ).find( 'a' ).addClass( 'open' );
	}
	theaterCount = 5;
	openTab(this);
}
function fadeLoading() {
	$( '#now-showing div.loading' ).fadeOut( 'normal' ).remove();
	contentLoadingDone();
}
function contentLoadingDone() {
    var object = findOpenTab();
    if (object) {
        var contentArea = $( object).parents( 'li' ).find( 'div' );
        
        if ($(contentArea).text() == 'Invalid Zip.') {
            return;
        }
        
        var statusMsg  = $(contentArea).find( '#status_msg' );
        if (statusMsg && $(statusMsg).text().length > 0) {
            var availableMessage = $( object).parents( 'li' ).find( 'span.availableMessage' );
            $(availableMessage).text($(statusMsg).text());               
            $(statusMsg).text('')
            if ( $( contentArea).find('ul.dates' ) == null || $( contentArea).find('ul.dates' ).text().length == 0) {
                $(contentArea).css('display', 'none');
            }
        }
        $( '.see-more-theaters').find('a').click( moreTheaters );
        $( '#on-tv-content ul.dates li a' ).click( dateClicked );
        $( '#in-theaters-content ul.dates li a' ).click( dateClicked );
        var zipObj = $('#now-showing ul li h4').find( 'input' );
        if (zipObj) {
            var zipCode = '';
            for (i=0; i < zipObj.length; i++ ){
                if (zipCode.length != 5) zipCode = $ (zipObj[i]).val();
                if (zipCode.length != 5) {
                    zipCode = getZipCode();
                }
	            if (zipCode.length == 5) $ (zipObj[i]).val(zipCode);
            }
            // if valid zip, set the session cookie for zip/headend
            var headendid = "", device = "", timezone = ""; 
            headendid = getHeadEndId(); timezone = getTimeZone();device = getDevice();
            setCookie("__TEMP_PHDZ", timezone + "|" + headendid + "|" + device + "|" + zipCode);
        }
    }
}
function findOpenTab() {
    var openObjects = $('#now-showing ul li h4').find( 'a' );
	for (i=0; i < openObjects.length; i++ )
    {
  	    var isOpen = $(openObjects[i]).hasClass( 'open' )
	    if (isOpen) {
	        return $(openObjects[i]).parents('h4').find('.go');
	    }
    }
    return null;
}
function openTab(object) {
    var zipInput = $(object).parents('h4').find('input');
    var zip = getZipCode();
    if (zipInput != null && zipInput.length > 0) {
        var zip = $(object).parents('h4').find('input').val();
        if (zip.length != 5) { zip = getZipCode(); if (zip.length == 5) $(object).parents('h4').find('input').val(zip); }
    }
    var contentArea = $( object).parents( 'li' ).find( 'div' );
    $( object).parents( 'h4' ).after( '<div class="loading"><img src="/assets/global/reelzLoading.gif" /></div>' ).show( 'slow' );
    contentArea.html( '... loading ...' );
    var timezone = getTimeZone();
    var timezoneString = findString($(object).attr('href'), 'timezone=');
    if (timezoneString.length > 0) $(object).attr('href', $(object).attr('href').replace(timezoneString, 'timezone=' + timezone));
    
    var zipcodeString = findString($( object).attr( 'href' ), 'zipcode=');
    if (zipcodeString.length > 0) $( object).attr( 'href' , $( object).attr( 'href' ).replace(zipcodeString, 'zipcode=' + zip));
    var theatercountString = findString($( object).attr( 'href' ), 'theatercount=');
    if (theatercountString.length > 0) $( object).attr( 'href' , $( object).attr( 'href' ).replace(theatercountString, 'theatercount=' + theaterCount));
    var clientDateString = findString($( object).attr( 'href' ), 'client_date=');
    if (clientDateString.length > 0) 
        {  var currentDate= new Date(); var currentMonth = currentDate.getMonth()+1; $( object).attr( 'href' , $( object).attr( 'href' ).replace(clientDateString, 'client_date=' + currentMonth + '/' + currentDate.getDate() + '/' + currentDate.getFullYear())); }
    contentArea.load($( object).attr( 'href' ), fadeLoading ).show( 'slow');   
}
function findString(source, lookupString) {
    var found = '';
    var lookupStringIndex = source.indexOf(lookupString);
	if (lookupStringIndex >= 0)
	{
	    var lookupStartString = source.substring(lookupStringIndex);
	    var lookupEndIndex = lookupStartString.indexOf('&');
	    if (lookupEndIndex >= 0)
	        found = lookupStartString.substring(0, lookupEndIndex);
        else 
            found = lookupStartString;
     }
    return found;
}
function getZipCode(){
    var zipcode = "";
    var cookie = getCookie("__TEMP_PHDZ");
    if (cookie == null) { 
        cookie = getCookie("__REELZ_PHDZ"); 
    }
    if (cookie) {
         var values = cookie.split('|');
         if (values != null && values.length >=4)
            zipcode = values[3];
    }
    return zipcode;
}
function getTimeZone() {
    var timezone = "";
    var cookie = getCookie("__REELZ_TIMEZONE");
    if (cookie != null) {
        timezone = cookie;
    }
    return timezone;
}
function getHeadEndId() {
    var headendid = "";
    var cookie = getCookie("__TEMP_PHDZ");
    if (cookie == null) {
        cookie = getCookie("__REELZ_PHDZ");
    }
    if (cookie != null) {
         var values = cookie.split('|');
         if (values != null && values.length >=2)
            headendid = values[1];
    }
    return headendid;
}
function getDevice()
{
    var device = "";
    var cookie = getCookie("__TEMP_PHDZ");
    if (cookie == null) {
        cookie = getCookie("__REELZ_PHDZ");
    }
    if (cookie != null) {
         var values = cookie.split('|');
         if (values != null && values.length >=3)
            device = values[2];
    }
    return device;
}
function clearZipInput(element) {
    if (element.value.toLowerCase() == "zip") element.value = "";
}