﻿function RenderPageContent(text)
{
    this.response.json = JSON.decode(text, this.options.secure); 
    var obj = eval(text);
    obj.Result.each(function(content)
    {
        var _page;
        if(content.Name == 'Home')
        {
            _page = $('home_details');
        }
        else if(content.Name == 'About Us')
        {
            _page = $('about_details');
        }
        else
        {
            _page = $('contact_details');
        }
        _page.set('html',decodeURI(content.Content));
    });
}
function InsertRelatedLink()
{
    var typeSelected = $('link_type').getSelected();
    var title = $('name');
    var linkUrl = $('url');
    
    PowercatVision.PageContentService.InsertRelatedLink(typeSelected.get('value'),ConvertSymbols(decodeURI(title.value)),ConvertSymbols(decodeURI(linkUrl.value)));
    
    typeSelected.value = '0';
    title.value = '';
    linkUrl.value = '';
}

function InsertNewsItem()
{
    var title = $('title');
    var synopsis = $('synopsis');
    var content = $('content');
    
    PowercatVision.PageContentService.InsertNewsItem(title.value, synopsis.value, content.value);
    
    title.value = '';
    synopsis.value = '';
    content.value = '';
}
function InsertGameShowing()
{
    var gameName = $('game_name');
    var gameDate = $('game_date');
    var gameStartTime = $('start_time');
    var active = $('game_active');
    
    var convertedDate = decodeURI(gameDate.value);
    alert(convertedDate);
    alert(active.get('checked'));
    
    PowercatVision.PageContentService.InsertGameShowing(gameName.value, convertedDate, gameStartTime.value, active.get('checked'));
    
    ResetGameControls();
}
function ResetGameControls()
{
    var gameName = $('game_name');
    var gameDate = $('game_date');
    var gameStartTime = $('start_time');
    var active = $('game_active');
    
    gameName.value = '';
    gameDate.value = '';
    gameStartTime.value = '';
    active.removeProperty('checked');
}
function ShowLocations()
{
    PowercatVision.ListingsService.GetListingNames(RenderGameShowingLocations);
}
function RenderGameShowingLocations(text)
{
    //alert(text);
    this.response.json = JSON.decode(text, this.options.secure);
    var obj = eval(text);
    
    if(obj.Result != null & obj.Result.length > 0)
    {
        locations = obj.Result;
        //alert(locations);
        var locationShowingsContainer = $('location_showing_list');
        if(locationShowingsContainer != null)
        {
         locationShowingsContainer.destroy();
        }
        locationShowingsContainer = new Element('ul',{'style':'height:465px;overflow:scroll;list-style:none;','id':'location_showing_list'});
        
        var locationShowingsFieldset = new Element('fieldset');
        var locationShowingsFieldsetLegend = new Element('legend',{'html':'KSU Friendly Locations'});
        var locationShowingsHeader = new Element('ul',{'style':'list-style:none;'});
        var locationShowingHeaderItem = new Element('li');
        var locationShowingHeaderSepItem = new Element('li');
        
        var locationHeader = new Element('label',{'html':'Location'});
        var statusHeaders = new Element('label',{'style':'float:right;width:60%;'});
        var showingHeader = new Element('label',{'html':'Showing','style':'width:24%;'});
        var soundHeader = new Element('label',{'html':'With Sound','style':'width:24%;'});
        var askHeader = new Element('label',{'html':'Must Ask For It','style':'width:24%;'});
        var watchPartyHeader = new Element('label',{'html':'Watch Party','style':'width:24%;'});
        
        var headerSep = new Element('hr');
        
        showingHeader.inject(statusHeaders);
        soundHeader.inject(statusHeaders);
        askHeader.inject(statusHeaders);
        watchPartyHeader.inject(statusHeaders);
        
        //headerTbSep.inject(locationShowingHeaderTbSepItem);
        
        locationHeader.inject(locationShowingHeaderItem);
        statusHeaders.inject(locationShowingHeaderItem);
        
        headerSep.inject(locationShowingHeaderSepItem);
        
        //locationShowingHeaderTbItem.inject(locationShowingsHeader);
        //locationShowingHeaderTbSepItem.inject(locationShowingsHeader);
        locationShowingHeaderItem.inject(locationShowingsHeader);
        locationShowingHeaderSepItem.inject(locationShowingsHeader);
        locationShowingsHeader.inject(locationShowingsFieldset);
        
        locations.each(function(location)
        {
            var locationShowingItem = new Element('li',{'name':'location_item','id': location.ID});
            var locationShowingSepItem = new Element('li');
            var locationShowingSepHr = new Element('hr');
            locationShowingSepHr.inject(locationShowingSepItem);
            
            var locationName = new Element('label',{'html':location.Name});
            var statusImgs = new Element('label',{'style':'float:right;width:60%;'});
            var showing = new Element('input',{'type': 'checkbox','id':'showing' + location.ID});
            var showingCon = new Element('label',{'style': 'width:24%;text-align:center;'});
            showing.inject(showingCon);
            var sound = new Element('input',{'type': 'checkbox','id':'sound' + location.ID});
            var soundCon = new Element('label',{'style': 'width:24%;text-align:center;'});
            sound.inject(soundCon);
            var askForShowing = new Element('input',{'type': 'checkbox','id':'ask' + location.ID});
            var askForShowingCon = new Element('label',{'style': 'width:24%;text-align:center;'});
            askForShowing.inject(askForShowingCon);
            var watchParty = new Element('input',{'type': 'checkbox','id':'watchparty' + location.ID});
            var watchPartyCon = new Element('label',{'style': 'width:24%;text-align:center;'});
            watchParty.inject(watchPartyCon);
            
            showingCon.inject(statusImgs);
            soundCon.inject(statusImgs);
            askForShowingCon.inject(statusImgs);
            watchPartyCon.inject(statusImgs);
            
            locationName.inject(locationShowingItem);
            statusImgs.inject(locationShowingItem);
            
            locationShowingItem.inject(locationShowingsContainer);
            locationShowingSepItem.inject(locationShowingsContainer);
        });
        locationShowingsContainer.inject(locationShowingsFieldset);
        locationShowingsFieldsetLegend.inject(locationShowingsFieldset);
        locationShowingsFieldset.inject($('location_showing_details'));
        //locationShowingsHolder.inject($('location_showing_details'));
        var sumbitStatus = new Element('input',{'type':'button','value':'Submit','style':'width:125px;'});
        sumbitStatus.addEvent('click',function()
        {
            var locationItems = locationShowingsContainer.getElements('li[name=location_item]');
            //alert(locationItems.length);
            locationItems.each(function(locationItem)
            {
                var insertLocation = "false";
                var gameShowingId = $('game_showings').getSelected().get('value');
                var locationId = locationItem.get('id');
                var showing = locationItem.getElementById('showing' + locationId);
                var sound = locationItem.getElementById('sound' + locationId);
                var ask = locationItem.getElementById('ask' + locationId);
                var watchparty = locationItem.getElementById('watchparty' + locationId);
                if(showing.get('checked') == true)
                {
                    insertLocation = "true";
                }
                if(sound.get('checked') == true)
                {
                    insertLocation = "true";
                }
                if(ask.get('checked') == true)
                {
                    insertLocation = "true";
                }
                if(watchparty.get('checked') == true)
                {
                    //alert('Watching');
                    insertLocation = "true";
                }
                
                               
                if(insertLocation == "true")
                {
                    //alert('InsertLocationShowing');
                    PowercatVision.PageContentService.InsertLocationShowing(gameShowingId, locationId, showing.get('checked'), sound.get('checked'), ask.get('checked'), watchparty.get('checked'));
                }
            });
        });
        
        sumbitStatus.inject($('location_showing_details'));
    }
}
function GetActiveWeeklyChannelGuide(id)
{
    
    //alert('getting there');
    //alert(id);
    PowercatVision.PageContentService.GetActiveWeeklyChannelGuideByType(id, function(text)
    {
        this.response.json = JSON.decode(text, this.options.secure);
        var obj = eval(text);
        //alert(obj.Result);
        if(obj.Result != null)
        {
            //alert(obj.Result.Name);
            var channelGuideLegend = $('channel_guide_legend');
            //alert(channelGuideLegend);
            channelGuideLegend.set('html',channelGuideLegend.get('html') + ' for ' + ConvertSymbols(decodeURI(obj.Result.Name)));
        }
    });
    
}
function GetActiveWeeklyChannelGuideChannels(id)
{
    //alert(id);
    var _uacct = "UA-6427972-1";
    var pageTracker = _gat._getTracker(_uacct);
    HideTopElements();
    var _newsItemDetailContainer = $('news_item_details');
    var listings = $('listing_details');
    if(listings != null)
    {
        listings.setStyle('display','none');
    }
    _newsItemDetailContainer.empty();
    _newsItemDetailContainer.setStyle('display','block');
    var channelGuideHolder = $('channel_guide_holder');
    //alert(channelGuideHolder);
    if (channelGuideHolder != null)
    {
        channelGuideHolder.destroy();
    }
    channelGuideHolder = new Element('div',{'id':'channel_guide_holder','style':'height:500px;overflow:scroll;','class':'content'});
    var channelGuideContainer = new Element('ul',{'style':'list-style:none;','id':'channel_guide_list'});
    var channelGuideFieldset = new Element('fieldset');
    var channelGuideFieldsetLegend = new Element('legend',{'html':'KSU Channel Guide','id':'channel_guide_legend'});
    
    channelGuideFieldset.inject(channelGuideHolder);
    channelGuideFieldsetLegend.inject(channelGuideFieldset);
    channelGuideContainer.inject(channelGuideFieldset);
    channelGuideHolder.inject(_newsItemDetailContainer);
    GetActiveWeeklyChannelGuide(id);
    PowercatVision.PageContentService.GetActiveChannelGuideCities(
    function (text)
    {
        this.response.json = JSON.decode(text, this.options.secure);
        var obj = eval(text);
        
        obj.Result.each(function(city)
        {
           RenderChannels(id, city.CityID);
           var channelGuideCityItem = new Element('li',{'style':'width:100%;background-color:#8354BF;font-weight:bold;','id':city.CityID + '_channels'});
           var labelCity = new Element('label',{'html':city.Name,'style':'width:100%;text-align:center;'});
           labelCity.inject(channelGuideCityItem);
           
           channelGuideCityItem.inject(channelGuideContainer);
           
        });
    });
    pageTracker._trackPageview('TheOutlook/WeeklyChannelGuide/' + id);
}
function RenderChannels(typeId, cityId)
{
    //alert(cityId);
    PowercatVision.PageContentService.GetActiveWeeklyChannelGuideChannelsByCityId(cityId, typeId, function(text)
       {
            this.response.json = JSON.decode(text, this.options.secure);
            var obj = eval(text);
            var cityChannelsContainer = $('channel_guide_list');
            var currentCityItem = $(cityId + '_channels');
            obj.Result.each(function(channel)
            {
                var channelItem = new Element('li',{'style':'width:100%;'});
                var labelChannelName = new Element('label',{'html':channel.ChannelName,'style':'width:40%;float:left;'});
                labelChannelName.inject(channelItem);
                
                var labelChannel = new Element('label',{'html':channel.Channel,'style':'width:40%;float:right;'});
                labelChannel.inject(channelItem);
                
                channelItem.inject(currentCityItem, 'after');
            });
       });
}