// load style sheet
function loadStyles(id, url){var link = document.createElement("link");link.id = id;link.rel = "stylesheet";link.type = "text/css";link.href = url;var head = document.getElementsByTagName("head")[0];head.appendChild(link);}

// open/close faceboxes
function openFacebox(facebox) {var left = $("#centerPanel").offset().left, top = $("html, body").offset().top;$(facebox).css( { "left": (left+35)+"px", "top": (-top+200)+"px" } ); $("#overlay").fadeIn("fast", function () { $(facebox).show(); });return false;}
function closeFacebox() {$(".facebox").hide();$("#overlay").fadeOut("fast");return false;}


//## Custom jQuery functions
(function($){
    $.fn.extend({
	    // Center in window
        center : function () {
			        return this.each(function() {
					    var top = ($(window).height() - $(this).outerHeight()) / 2;
					    var left = ($(window).width() - $(this).outerWidth()) / 2;
						$(this).css({position:'absolute', margin:0, top: 205 +'px', left: (left > 0 ? left : 0)+'px'});
					});
				},
        // ELement exists
        exists : function(){return $(this).length>0;},
        // Value is an integer
        isInt : function(){return parseInt($(this).val()) == $(this).val();},
        // Sliding left hide and show
        slideRightShow: function(speed, callback) {return this.each(function() { $(this).show('slide', {direction: 'right'}, speed, callback);});},
        slideLeftHide: function(speed, callback) {return this.each(function() { $(this).hide('slide', {direction: 'left'}, speed, callback);});},
        slideRightHide: function(speed, callback) {return this.each(function() {$(this).hide('slide', {direction: 'right'}, speed, callback);});},
        slideLeftShow: function(speed, callback) {return this.each(function() {$(this).show('slide', {direction: 'left'},  speed, callback);});}
    });
})(jQuery);



//## Document ready
$(document).ready(function() {
    $("#overlay").css({ 'opacity': 0.5 });

    // Low resolution
    if ($(window).width() < 730 || $(window).height() < 656) {if(!$("#size-stylesheet").exists()) {loadStyles("size-stylesheet", "/static/plp2/www/css/low-rez.css");}}
    $(window).resize(function(){
        if ($(window).width() < 730 || $(window).height() < 656) {if(!$("#size-stylesheet").exists()) {loadStyles("size-stylesheet", "/static/plp2/www/css/low-rez.css");}}
        else {$("#size-stylesheet").remove();}
    });

    // Fix position of facebook invite box
    //$("#fb_dialog_loader_close").live(function() { $(this).parent().parent().parent().parent();});

    // Toggles
    $('h2.toggle').click(function(){$(this).next('div.toggle').slideToggle();});
    $("form.toggle").submit(function () {$("div.toggle").slideUp("slow");});

    // Linked table rows
    $('table.data td.name a').parent().parent().addClass('linkRow').click(function() {window.location=$(this).find("td:first a").attr('href');}).find('td:last').append('<span class="link-arrow"></span>');
    $('div.info ul li a').append('<span class="link-arrow"></span>');

	// Level number link
    $("#levelIndicator").click(function(){window.location=app.path+'/page/levels';return false;});

    // Close facebox
    $(".closeThis, #overlay").click(function() {closeFacebox();});

    // Invite functions
    $("a.inviteFacebook").click(function() {
        // Analytics
        _kmq.push(['record', 'Invite Facebook Clicked', {'View':app.view, 'Page':app.page, 'File':app.file}]);
        _gaq.push(['_trackPageview', app.path+'/invite-facebook/clicked/'+app.file]);
        if(app.logging) {app.log(app.path+'/invite-facebook/clicked/'+app.file);}

        // Close any faceboxes
        closeFacebox();

        // Hide loading dialog
        setTimeout("$('#fb_dialog_loader_close').parent().parent().parent().parent().hide();", 100);

        // Show facebook form
	    return app.inviteFacebook({'method':'Facebook','type':'Invite'});
	});
    $("a.inviteEmail").click(function() {
        // Analytics
        _kmq.push(['record', 'Invite Email Clicked', {'page':app.file}]);
        _gaq.push(['_trackPageview', app.path+'/invite-email/clicked/'+app.file]);
        if(app.logging) {app.log(app.path+'/invite-email/clicked/'+app.file);}

        // Show facebox
        var pos = $("#centerPanel").offset();
		$("#faceboxInvite").css( { "left": (pos.left + 35) + "px" } );
	    $("#overlay").fadeIn("fast", function () { $("#faceboxInvite").show(); });
	});
    $("form.inviteEmail").submit(function () {
       // Hide facebox
       $("#faceboxInvite").hide();$("#overlay").fadeOut("fast");

        // Get form arguments and send invites
        var args = {};
        $.each($(this).serializeArray(), function(i, field) {args[field.name] = field.value;});
        return app.inviteEmail(args);
    });
    $("form.inviteFacebook").submit(function () {
        // Analytics: log click
        _kmq.push(['record', 'Invite Facebook Clicked', {'Page':app.file}]);
        _gaq.push(['_trackPageview', app.path+'/invite-facebook/clicked/'+app.file]);
        if(app.logging) {app.log(app.path+'/invite-facebook/clicked/'+app.file);}

        // Close any faceboxes
        closeFacebox();

        // Hide loading dialog
        setTimeout("$('#fb_dialog_loader_close').parent().parent().parent().parent().hide();", 100);

        // Get form arguments and send invites
        var args = {};
        $.each($(this).serializeArray(), function(i, field) {args[field.name] = field.value;});
        return app.inviteFacebook(args);
    });
    $("form.inviteEmail").submit(function () {
        $("#msg").slideUp("slow").html("<h3>Your invites have been sent</h3>").slideDown("slow");
        if(app.view == 'fb'|| app.view == 'tab') {FB.Canvas.scrollTo(0,0);} else {$("html, body").animate({ scrollTop: 0 }, "slow");}
    });

    // Comment functions
    $("form.postComment").submit(function () {
        return app.postComment($(this));
    });
    $("div.comment span.del").live('click',function () {
        var id = $(this).attr('data-id');
        $(this).parent().parent().hide();
        $.get(app.path+'/comment/'+id+'/delete');
    });
    //$("div.comment span.edit").click(function () {
       //$(this).parent().parent().find('form').show().prev().hide();
    //});

    // Requests
    $('div.request:last').slideDown('slow');
    // Decline request
    $('div.request .decline').live('click',function() {
        app.log('request decline');
        $(this).parent().parent().parent().hide().prev('div.request').slideDown('slow');  // Hide parent and show next request
        $.get($(this).attr('href')+'?_return=json');  // Decline request
        return false;
    });
    // Accept request
    $('div.request .accept').live('click',function() {
        app.log('request accept');
        var parent = $(this).parent().parent().parent().hide().prev('div.request').slideDown('slow');  // Hide parent and show next request
        $.get($(this).attr('href')+'?_return=json');  // Accept request
        return false;
    });

});
