/*
* XBox plugin for jQuery
*
* Copyright 2011, TheXCodec
* License MIT-style License.
* http://www.opensource.org/licenses/mit-license.php
*
* Depends:
*   jquery.js
*/
//Bollean swich to detect what to do in the play.aspx static function
var page_type = "normal";

(function($) {
    jQuery.fn.XBox = function(options) {
        //Set the default values
        var settings = {
            opacity: 0.9,
            fadeIn: 500,
            fadeOut: 500
        };
        var options = $.extend(settings, options); //Combine all options
        var setting = options; //Accessor variable for the options

        //if close button is clicked
        $('.close').click(function() {
            $("#mask").fadeOut((setting.fadeOut * 2));
            $('.window').fadeOut((setting.fadeOut / 2), function() { window.scrollTo(0, scroll_position); });
        });

        //if mask is clicked
        $('#mask').click(function() {
            $(this).fadeOut((setting.fadeOut * 2));
            $('.window').fadeOut((setting.fadeOut / 2), function() { window.scrollTo(0, scroll_position); });
        });

        return this.each(function() {
            //Accessor variable for the options
            var setting = options;
            //Get the window height and width
            var winH = $(window).height();
            var winW = $(window).width();

            //Set height and width to mask to fill up the whole screen
            $('#mask').css({ 'width': winW, 'height': 10000 }).fadeTo(setting.fadeIn, setting.opacity);

            //Set the popup window to center
            $(this).css('top', 10);

            if (navigator.appName == "Microsoft Internet Explorer")
                $(this).css('left', ((winW / 2) - ($(this).width() / 2)));
            else
                $(this).css('left', ((winW / 2) - (650 / 2)));

            //Display the item
            $(this).fadeIn(setting.fadeIn);
        });
    };
})(jQuery);



$(function() {
    $("a").click(function() { //Link Override
        if ($(this).attr('name') == "xbox" || $(this).attr('name') == "xbox-cheap" || $(this).attr('name') == "xbox-best") {
            if ($(this).attr('name') == "xbox-cheap")
                page_type = "cheap";
            if ($(this).attr('name') == "xbox-best")
                page_type = "best";

            scroll_position = (window.pageYOffset || document.documentElement.scrollTop)
            $(($(this).attr('href'))).XBox(); //Run XBOX plugin
        }
    });
});


$(function() {
    $("a").click(function() { //Link Override
        if ($(this).attr('name') == "xbox" || $(this).attr('name') == "xbox-cheap" || $(this).attr('name') == "xbox-best") {
            if ($(this).attr('name') == "xbox-cheap")
                page_type = "cheap";
            if ($(this).attr('name') == "xbox-best")
                page_type = "best";

            current_index_number = parseFloat($(this).attr('id'));
            load_play($(this).attr('kw'));
            buttons_detection();
        }
    });

    //What happens on next button click
    $("#next_button").click(function() {
        button_click("next");
    });

    //What happens on prev button click
    $("#prev_button").click(function() {
        button_click("prev");
    });
});

function button_click(type) {
    //Detect if the user wants to go up or down through the array
    if (type == "next")
        current_index_number += 1;
    else
        current_index_number -= 1;
        
    load_play($("#" + current_index_number).attr('kw'));
    buttons_detection()
}

//This will detect to see which buttons need to be shown
function buttons_detection() {
    $(function() {
        if (item_index.length != 1) {
            if (current_index_number == (item_index.length - 1)) //Detects if there should be a next button
                $("#next_button").fadeOut(250); //Disable the next button
            else
                $("#next_button").fadeIn(250); //Enable the next button

            if (current_index_number == 0) //Detects if there should be a prev button
                $("#prev_button").fadeOut(250); //Disable the prev button
            else
                $("#prev_button").fadeIn(250); //Enable the prev button
        }
        else {
            $("#next_button").fadeOut(250); //Disable the next button
            $("#prev_button").fadeOut(250); //Disable the prev button
        }
    });
}

//Quickly generate HTML to keep down repetitive code
function create_html(html_header, html_body) {
    return "<center><h1>" + html_header + "</center></h1><br><br><p>" + html_body + "</p>";
}

//Will load all the plays information and display it in a XBOX
//Can be called at any time, all it needs is a play name to pull from a database
function load_play(play_name) {
    $("#dialog_body").fadeOut(250, function() {
        $("#dialog_body").empty();
        $("#dialog_body").html('<center style="font-size:xx-large; font-family:Arial;">Loading Content<br /><br /><img src="/images/loading.gif" alt="loading" /><br /></center>').fadeIn(250, function() {
            $.ajax({
                type: "POST",
                url: "/play.aspx/get_info",
                data: "{'play_name': '" + play_name + "', 'page_type': '" + page_type + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(msg) { //Method returned data, do function
                    window.location.hash = play_name;
                    //will detect if links are valid
                    if (msg.d[10] != "" && msg.d[10] != null)
                        msg.d[10] = '<a href="http://' + msg.d[10].replace("http://", "") + '" target="_blank"><img src="/images/Home-icon.png" alt="home page" style="width:35px; border:none;" /></a>';
                    if (msg.d[11] != "" && msg.d[11] != null)
                        msg.d[11] = '<a href="http://' + msg.d[11].replace("http://", "") + '" target="_blank"><img src="/images/Facebook_Image.png" alt="facebook page" style="width:35px; border:none;" /></a>';
                    if (msg.d[12] != "" && msg.d[12] != null)
                        msg.d[12] = '<a href="http://' + msg.d[12].replace("http://", "") + '" target="_blank"><img src="/images/Twitter-icon.png" alt="twitter page" style="width:35px; border:none;" /></a>';
                    if (msg.d[13] != "" && msg.d[13] != null)
                        msg.d[13] = '<a href="http://' + msg.d[13].replace("http://", "") + '" target="_blank"><img src="/images/youtube-icon.png" alt="youtube page" style="width:35px; border:none;" /></a>';
                    if (msg.d[14] == null) //Make sure if there is warnings so string does not display "null"
                        msg.d[14] = "None";

                    do {
                        msg.d[17] = msg.d[17].replace("|", "");
                    } while (msg.d[17].indexOf("|") != -1);

                    for (var i = 0; i <= 17; i++) {
                        if (msg.d[i] == null || msg.d[i] == undefined)
                            msg.d[i] = "";
                    }

                    $("#dialog_body").fadeOut(250, function() {
                        $("#dialog_body").empty();
                        $("#dialog_body").html('<div>\
		                                    <table>\
		                                        <tr><td COLSPAN=5><center><span id="sharethis_dynamic_button"></span></center><br /></td></tr>\
		                                        <tr><td COLSPAN=5 style="font-size:10px"><center>' + msg.d[18] + '</center><br /></td></tr><tr>\
		                                            <td style="min-width:390px; max-width:390px; width:390x; vertical-align:top;">\
		                                            <center><h2>' + msg.d[3] + '</h2>\
		                                            <img src="' + msg.d[6] + '" alt="program image" style="width:100px; height:100px;" /><br /><br />\
		                                            ' + msg.d[1] + '<br />\
		                                            ' + msg.d[2] + '<br />\
		                                            <i>' + msg.d[4] + '</i><br /><br />\
		                                            ' + msg.d[9] + '<br /><br />\
		                                            ' + msg.d[10] + '\
		                                            ' + msg.d[11] + '\
		                                            ' + msg.d[12] + '\
		                                            ' + msg.d[13] + '\
		                                            <br /><br />\
		                                            <B>Recommended For: ' + msg.d[15] + '</B><br />\
		                                            <B>Length: ' + msg.d[16] + '</B><br />\
		                                            <B>Tickets: ' + (msg.d[7].split("|:|"))[0] + '</B><br />\
		                                            <B>' + (msg.d[7].split("|:|"))[1] + '</B><br /><br />\
		                                            <B>' + (msg.d[8].split("|:|"))[0] + '</B><br />\
		                                            <B>' + (((msg.d[8].split("|:|"))[1] == undefined) ? '' : (msg.d[8].split("|:|"))[1]) + '</B><br /><br />\
		                                            <B>Warnings: ' + msg.d[14] + '</B><br />\
		                                            <\center></td>\
		                                            <td style="padding-left:15px;"></td>\
		                                            <td style="min-width:150px; max-width:150px; width:150px; vertical-align:top; padding-right:5px; border:solid 1px black; background-color:#CCC; height:auto; font-weight:bold; ">\
		                                                <center><br /><br /><div>' + msg.d[17] + '</div></center>\
		                                            </td>\
		                                        </tr>\
		                                    </table></div>\
		                                ');
                        try {
                            stWidget.addEntry({
                                "service": "sharethis",
                                "element": document.getElementById('sharethis_dynamic_button'),
                                "url": "http://www.winnipegfringe.com/fr_performer.aspx?kw=" + play_name,
                                "title": msg.d[3],
                                "text": "Share This Performance",
                                "summary": msg.d[9],
                                "type": "chicklet",
                                "image": 'http://www.winnipegfringe.com/' + msg.d[6]
                            });
                        } catch (err) { }

                        $("#dialog_body").fadeIn(250); //displays info
                        window.scrollBy(0, -5000);
                    });
                },
                error: function() { //If can not load vb.net method display generic error message
                    $("#dialog_body").fadeOut(250, function() {
                        $("#dialog_body").empty();
                        var test = ""
                        for (var i = 0; i <= 16; i++) {
                            test += "Item #: " + i.toString() + " | " + msg.d[i] + "<br />";
                        }
                        $("#dialog_body").html(create_html("Error #UnKnown", "error loading data"));
                    });
                }
            });
        });
    });
}
