EA.namespace('Ao2.Home');

$home = EA.Ao2.Home = {
    init: function() {

		$home.bindEvents();
        $home.setupDialogs();

        $home.loadQuestionAndAnswers();
    
        // need to check if agegate and region cookies have been set and then show
        if(AgeGateTerritoryStatus.defaultRegionFlag) 
        {
        	$j("#flash").hide();
			$j("#homeContents").hide();
        } 
        else 
        {
        	if ( (AgeGateTerritoryStatus.ageCheckVal < 0) || 
        	   (AgeGateTerritoryStatus.ageCheckVal > -1 && AgeGateTerritoryStatus.ageCheckVal < 17))
        	{
            	$j("#flash").hide();
				$j("#homeContents").hide();
    	    }
        	else
        	{
        		$j("#flash").show();
				$j("#homeContents").show();

				var flashvars = {};
				flashvars.dynamicXML = "/flash-data";
				flashvars.staticXML = "http://ll.assets.ea.com/nawp/na/u/f/GPO/eagames/armyoftwo/tfd/"+flashLimelightFolder+"/data/static.xml";
				flashvars.showInteractive = showInteractive;
				flashvars.testSWF = "http://ll.assets.ea.com/nawp/na/u/f/GPO/eagames/armyoftwo/tfd/"+flashLimelightFolder+"/src/test.swf";
				var params = {};
				params.quality = "high";
				params.scale = "noscale";
				params.wmode = "transparent";
				params.bgcolor = "#000000";
				params.allowscriptaccess = "always";
				var attributes = {};
				attributes.id = "flash";
				attributes.name = "AO2Home";
				attributes.align = "middle";
				
				swfobject.embedSWF("http://ll.assets.ea.com/nawp/na/u/f/GPO/eagames/armyoftwo/tfd/"+flashLimelightFolder+"/src/AOHome.swf", "flash", "448", "100%", "8.0.0", false, flashvars, params, attributes); //init main flash
				
				$j("#tabs").tabs({ cache: false, selected: ($hometab-1) }); // Init tabs
        	}
        }  
    },
    

    setupDialogs: function() {
        $j('#postTweetBox').dialog({
            autoOpen: false,
            bgiframe: true,
            buttons: {
                'Tweet Reply' : function() {
                    EA.Twitter.postHomePageReply(
                        $j('#postTweetBox .textBox').val(),
                        $j('#postTweetBox .username').val(),
                        $j('#postTweetBox .password').val(),
                        function (data, textStatus) {
                            if (data == "SUCCESS") {
                                $j("#postTweetBox").dialog('close');
                                $home.loadAnswers();
                            } else if (data == "TWITTER_ERROR") {
                                $j("#postTweetBox .statusUpdate").html("Invalid username or password.");
                            } else if (data == "SSFAILURE") {
                                $j("#postTweetBox .statusUpdate").html("Error communicating with server.");
                            }
                        }
                    );
                },
                'Cancel': function() {
                    $j(this).dialog('close');
                }
            },
            modal: true,
            open: $home.onDialogOpen
        });
    },

    onDialogOpen: function(event, ui) {
        $j('.ao2-dialog .textLimit').html(EA.Twitter.postCharCount);
        $j('.ao2-dialog textarea').val('').css('color', '#999');
        $j('.ao2-dialog .statusUpdate').html(' ');
    },

    bindEvents: function() {
        $j('#twitter-toggler a').click(function() {
            $j('#twitter').show();
            $j('#twitter-toggler').addClass("on");
            $j('#qa').hide();
            $j('#qa-toggler').removeClass("on");

            return false;
        });

        $j('#qa-toggler a').click(function() {
            $j('#qa').show();
            $j('#qa-toggler').addClass("on");
            $j('#twitter').hide();
            $j('#twitter-toggler').removeClass("on");

            return false;
        });

        $j('.tweet-back a').live('click', function() {
            $j('#postTweetBox').dialog('open');
            return false;
        });
    },

    loadQuestionAndAnswers: function() {
        $j.getJSON('/twitter/twitter.json',
                function(data) {
                    var question = data.question,
                        answers = data.answers,
                        answersList = '';

                    $j('#latest-question').append(
                            '<img src="' + question.profileImage + '" alt="" />' +
                            '<div class="details">' +
                                '<div class="text">' + question.text + '</div>' +
                                '<div class="tweet-back"><a href="#">Tweet a reply</a></div>' +
                                '<div class="author">Posted by ' + question.author + '</div>' +
                                '<div class="date">' + question.simpleDate + '</div>' +
                            '</div>'
                    );

                    $home.loadAnswers(answers);
                });
    },

    loadAnswers: function(data) {
        function displayAnswers(answers) {
            var answersList = '';

            $j.each(answers, function(i, answer) {
                answersList += '<div class="answer clearfix">' +
                               '<img src="' + answer.profileImage + '" alt="" />' +
                               '<div class="details">' +
                               '<div class="text">' + answer.text + '</div>' +
                               '<div class="author">' + answer.author + '</div>' +
                               '<div class="date">' + answer.simplePublishDate + '</div>' +
                               '</div></div>';

                if (i === 2) return false;
            });

            $j('#answers').replaceWith(answersList);
        }

        if (arguments.length == 1) {
            displayAnswers(data);
        } else {
            $j.getJSON('/twitter/twitter.json',
            function(data) {
                displayAnswers(data.answers);
            });
        }
    }
    
};

$j(document).ready($home.init);