﻿
var FSH = {
consumerContext: ',"ConsumerContext":{"DeviceContext":{"DeviceID":null,"DeviceChannelType":"INTERNAL","DeviceOSVersion":null,"DeviceLocale":null,"DeviceLanguage":null},"SecurityToken":{"TenantID":null,"TenantName":null,"UserID":null,"TokenString":"AMPSecurityTokenString"}}',
    serviceUrl:function() {return 'http://services.ionosplatform.com/services/FlightSearchesHistory/FlightSearchHistory.svc/jsonp/[METHOD]?servicerequest={"ServicePayload":{"AirportsToExclude":["NULL"]}' + FSH.consumerContext + '}&method=?'},
    flightSearch: {
        url: function() {
            return FSH.serviceUrl().replace("[METHOD]", 'GetFlightSearches')
        }
    },
    arrival: {
        url: function() {
            return FSH.serviceUrl().replace("[METHOD]", 'getMostSearchedArrivals')
        },
        column: "col3"
    },
    departure: {
        url: function() {
            return FSH.serviceUrl().replace("[METHOD]", 'GetMostSearchedDepartures')
        },
        column: "col2"
    },
    totalSearches: {
        url: function() {
            return FSH.serviceUrl().replace("[METHOD]", 'GetTotalFlightSearches')
        }
    },
    dataDashboard: {
        count: 1,
        totalItems: 0,
        slideSeries: 0
    }
};
jQuery.fn.extend({
    mostSearchedArrDepPanel: function(DepArr) {
        var column;
        var url;
        if (DepArr == "Arr") {
            column = FSH.arrival.column;
            url = FSH.arrival.url();
        } else {
            column = FSH.departure.column;
            url = FSH.departure.url();
        }
        jQuery.getJSON(url + "&callback=?", function(data) {
            var dr = data.d.ServicePayload.ResultPayload;
            var noOfRows = dr.length;
            var rowNumber = 1;
            var ui = $(document).find('.dashData .' + column + ' ul');
            var img = "<img src=\"img/img_icon_dashData_[UD].gif\" alt=\"[UD]\" width=\"12\" height=\"12\" />";
            var imgUp = img.replace("[UD]", "up");
            var imgDown = img.replace("[UD]", "down");
            var htmlRow0 = "<li class=\"top\">[NO]. [CITY] ([AC]) [IMAGE]</li>";
            var htmlRowRest = "<li>[NO]. [CITY] ([AC]) [IMAGE]</li>";
            ui.empty();
            for (rowCounter = noOfRows - 1; rowCounter >= 0; rowCounter--) {
                if (dr[rowCounter].airportInformation.CityName != null && dr[rowCounter].airportInformation.AirportCode != null) {
                    if (rowCounter == 0)
                        row = htmlRow0.replace("[CITY]", dr[rowCounter].airportInformation.CityName);
                    else row = htmlRowRest.replace("[CITY]", dr[rowCounter].airportInformation.CityName);
                    row = row.replace("[AC]", dr[rowCounter].airportInformation.AirportCode.toUpperCase());
                    row = row.replace("[NO]", dr[rowCounter].SearchRank);
                    if (dr[rowCounter].FlagPrevDayCompare == 0)
                        row = row.replace("[IMAGE]", imgDown)
                    else row = row.replace("[IMAGE]", imgUp)
                    ui.prepend(row);
                    rowNumber++;
                }
            }
        });
    },
    totalSearchPanel: function() {
        var tfsURL = FSH.totalSearches.url();
        jQuery.getJSON(tfsURL + "&callback=?", function(data) {
            var dr = data.d.ServicePayload.ResultPayload;
            var parentDiv = $(document).find('.dashData .col4 ul li div div ');
            var accessibilitySection = $(document).find('.dashData .col4 ul li .accessibilityData p');
            var charLength;
            if (dr != null) {
                var totalSearches = dr[0].TotalSearches + "";
                //to convert it to string.
                charLength = totalSearches.length;
                var rowNumber = charLength;
                var rowHtmlRest = "<span>[DATA]</span>";
                var rowHtmlFirst = "<span class=\"last\">[DATA]</span>";
                var rowHtmllast = "<span class=\"first\">[DATA]</span>";
                parentDiv.empty();
                parentDiv.prepend(rowHtmlFirst.replace("[DATA]", totalSearches.charAt(0)));
                for (j = 1; j < charLength - 1; j++) {
                    parentDiv.prepend(rowHtmlRest.replace("[DATA]", totalSearches.charAt(j)));
                }
                if (charLength > 1)
                    parentDiv.prepend(rowHtmllast.replace("[DATA]", totalSearches.charAt(charLength - 1)));
                accessibilitySection = accessibilitySection.text() + totalSearches;
            }
        });
    },

    /*   DATA DASHBOARD rk
    ================================================== */


    dataDashboard: function() {

        jQuery.getJSON(FSH.flightSearch.url() + "&callback=?", function(data) {
            var rowCounter = 0;
            var dr = data.d.ServicePayload.ResultPayload;
            var noOfRows = dr.length;
            var rowNumber = noOfRows;
            var ui = $(document).find('.dashData .col1 ul');
            ui.empty();

            $(".unique").css("display", "block");
            $(".dashData").css("display", "block");
            $(".minMaxButton").css("display", "block");

            for (rowCounter = 0; rowCounter < noOfRows - 5; rowCounter++) {
                var row = "<li class=\"data" + rowNumber + "[CSS]\"><div class=\"content\"><strong>" + dr[rowCounter].DepartureCity + "</strong> to <strong>" + dr[rowCounter].ArrivalCity + "</strong></div></li>";
                row = row.replace("[CSS]", "");
                ui.append(row);
                rowNumber--

            }
            for (rowCounter = rowCounter; rowCounter < noOfRows; rowCounter++) {
                var row = "<li class=\"data" + rowNumber + "[CSS]\"><div class=\"content\"><strong>" + dr[rowCounter].DepartureCity + "</strong> to <strong>" + dr[rowCounter].ArrivalCity + "</strong></div></li>";
                if (rowNumber == 5) {
                    row = row.replace("[CSS]", " show top");
                }
                else
                    row = row.replace("[CSS]", " show");
                ui.append(row);
                rowNumber--
            }



            var target = $(document);
            FSH.dataDashboard.totalItems = target.find('.dashData .col1 ul li').length;
            if (jQuery.support.leadingWhitespace) {
                var listHeight = target.find('.dashData .col1 ul').height();
            } else {
                var listHeight = target.find('.dashData .col1 ul').height() + 5;
            }
            target.find('.dashData .col1 ul').css('height', listHeight);
            var countOfRotation = -1;
            var startMinute = (new Date()).getMinutes();
            function rotateDataSet() {
                countOfRotation++;
                var hideDataItem = FSH.dataDashboard.count;
                var showDataItem = FSH.dataDashboard.count + 5;
                if (target.children('.wrapper').hasClass('minTrue')) {
                    target.find('.data' + showDataItem).slideDown(400, function() {
                        target.find('.dashData .col1 ul .data' + showDataItem + ' .content').fadeIn(400);
                        target.find('.dashData .col1 ul .data' + hideDataItem).removeClass('show');
                    }).addClass('show top').siblings('li').removeClass('top').removeAttr('style');
                } else {
                    target.find('.dashData .col1 ul .data' + showDataItem).slideDown(500, function() {
                        target.find('.dashData .col1 ul .data' + showDataItem + ' .content').fadeIn(1500);
                        target.find('.dashData .col1 ul .data' + hideDataItem).removeClass('show');
                    }).addClass('show top').siblings('li').removeClass('top').removeAttr('style');
                }
                FSH.dataDashboard.count += 1;
                if (showDataItem >= FSH.dataDashboard.totalItems) {
                    var endMinute = (new Date()).getMinutes();
                    clearInterval(slideshowrotate);
                    $('#dataDashboard').dataDashboard();
                    FSH.dataDashboard.count = 1;
                }
            }
            var slideshowrotate = setInterval(rotateDataSet, 3000);
            target.find('.minMaxButton a').bind('click', function(e) {
                e.preventDefault();
                var targetButton = $(this);
                var col1list = targetButton.parent().siblings('.dashData').find('.col1 ul');
                col1list.removeAttr('style');
                if (targetButton.closest('.wrapper').hasClass('minTrue')) {
                    targetButton.html('Show Less <img src=\"img/img_show_less.gif\" alt=\"show less\" width=\"7\" height=\"7\" \/>').closest('.wrapper').removeClass('minTrue');
                } else {
                    targetButton.html('Show More <img src=\"img/img_show_more.gif\" alt=\"show more\" width=\"7\" height=\"7\" \/>').closest('.wrapper').addClass('minTrue');
                }
                if (jQuery.support.leadingWhitespace) {
                    var listHeight = col1list.height();
                } else {
                    var listHeight = col1list.height() + 5;
                }
                col1list.css('height', listHeight);
            })

        });
        }


});
//end of extend

