$ga(document).ready(function() {

    /* -- Generic functions ----------------------------- */

    function gaGetId(idName) {
      if (!document.getElementById(idName)) return false;
      return document.getElementById(idName);
    }

    /* -- Star rating ----------------------------------- */

    $ga("#ga_overall_stars")
    .show()
    .stars({
       split: 2
    });

    $ga("#ga_user_stars")
    .show()
    .click(function() {
        var instance  = $ga(this).data("stars");
        var currValue = instance.options.value;
        var itemId  = $ga("#ga_item_id").val();
        var rateUrl = $ga("#ga_rate_url").val();
        $ga.ajax({
            type: "POST",
            url: rateUrl,
            data: { itemid: itemId, rating: currValue, submit_btn: "submit" },
            cache: false,
            beforeSend: function() {
                $ga("#ga_label_user_rating").hide(); 
                $ga("#ga_rating_spinner").show();
            },
            success: function(html){
                $ga("#ga_rating_spinner").hide();
                $ga("#ga_label_user_rating").show();
            }
        });
    })
    .stars({
        split: 2,
        oneVoteOnly: true,
        captionEl: $ga("#ga_label_user_rating")
    });

    /* -- Play game page play log update ---------------- */

    setTimeout(function() { 
        if ( $ga("#ga_play_object").length ) {
            var itemId   = $ga("#ga_item_id").val();
            var playsUrl = $ga("#ga_plays_url").val();

            $ga.ajax({
                type: "POST",
                url: playsUrl,
                data: { itemid: itemId, submit_btn: "submit" },
                cache: false
            });
        }
    }, 30000);  /* 30 seconds */

    /* -- Make featured --------------------------------- */

    $ga("#ga_but_feature")
    .click(function() {
        var itemId      = $ga("#ga_item_id").val();
        var featureUrl  = $ga("#ga_feature_url").val();
        var is_featured = $ga("#ga_is_featured").val();
        var sLangYes = $ga("#ga_lang_yes").val();
        var sLangNo  = $ga("#ga_lang_no").val();

        $ga.ajax({
            type: "POST",
            url: featureUrl,
            data: { itemid: itemId, isfeatured: is_featured, submit_btn: "submit" },
            cache: false,
            beforeSend: function() {
            $ga("#ga_feature_spinner").show();
            $ga("#ga_main_spinner").show();
            },
            success: function(html){
                var is_featured = html;     /* get saved status value */
                if ( is_featured == 1 ) {
                    $ga("#ga_featured").html(sLangYes); 
                    $ga("#ga_but_feature").html($ga("#ga_lang_unmake_featured").val()); 
                }
                else {
                    $ga("#ga_featured").html(sLangNo); 
                    $ga("#ga_but_feature").html($ga("#ga_lang_make_featured").val()); 
                }
                $ga("#ga_feature_spinner").hide();
                $ga("#ga_main_spinner").hide();
                $ga("#ga_is_featured").val(is_featured);
            }
        });
        return false;
    });

    /* -- Delete game item ------------------------------ */

    $ga("#ga_but_delete")
    .click(function() {
        var itemId    = $ga("#ga_item_id").val();
        var deleteUrl = $ga("#ga_delete_url").val();
        var langDeleteThisGame = $ga("#ga_lang_r_u_sure_delete_this_game").val();

        if ( confirm(langDeleteThisGame) ) {
            $ga.ajax({
                type: "POST",
                url: deleteUrl,
                data: { itemid: itemId, submit_btn: "submit" },
                cache: false,
                beforeSend: function() {
                    $ga("#ga_but_delete").hide();
                    $ga("#ga_delete_spinner").show();
                },
                success: function(html) {
                    $ga("html, body").animate({scrollTop: 0}, "slow");
                    $ga("#ga_game_content").remove();
                    $ga("#ga_message_deleted").show();
                }
            });
        }
        return false;
    });

    /* -- Change game's category ------------------------ */

    $ga("#ga_select_category")
    .change(function() {
        var itemId      = $ga("#ga_item_id").val();
        var categoryUrl = $ga("#ga_category_url").val();
        var newCatId    = $ga("#ga_select_category").val();

        $ga.ajax({
            type: "POST",
            url: categoryUrl,
            data: { itemid: itemId, catid: newCatId, submit_btn: "submit" },
            cache: false,
            beforeSend: function() {
                $ga("#ga_category_spinner").show();
            },
            success: function(html) {
                $ga("#ga_category_spinner").hide();
            }
        });
    });

    /* -- Edit game's description and instruction ------- */

    $ga("#ga_but_edit")
    .click(function() {
        $ga("#ga_but_edit").hide();
        $ga("#ga_description").hide();
        $ga("#ga_instruction").hide();

        $ga("#ga_but_cancel").show();
        $ga("#ga_but_save").show();
        $ga("#ga_instruction_textarea").show();
        $ga("#ga_description_textarea").show();

        return false;
    });

    $ga("#ga_but_cancel")
    .click(function() {
        /* Vars to restore original content */
        var descText = $ga("#ga_description").html();
        var instText = $ga("#ga_instruction").html();
        $ga("#ga_description_textarea").val(descText);
        $ga("#ga_instruction_textarea").val(instText);

        $ga("#ga_but_cancel").hide();
        $ga("#ga_but_save").hide();
        $ga("#ga_instruction_textarea").hide();
        $ga("#ga_description_textarea").hide();

        $ga("#ga_but_edit").show();
        $ga("#ga_description").show();
        $ga("#ga_instruction").show();

        return false;
    });

    $ga("#ga_but_save")
    .click(function() {
        var itemId  = $ga("#ga_item_id").val();
        var editUrl = $ga("#ga_edit_url").val();
        var descriptionText = $ga("#ga_description_textarea").val();
        var instructionText = $ga("#ga_instruction_textarea").val();

        $ga.ajax({
            type: "POST",
            url: editUrl,
            data: { itemid: itemId, 
                    descri: descriptionText, 
                    instru: instructionText, 
                    submit_btn: "submit" },
            cache: false,
            beforeSend: function() {
                $ga("#ga_edit_spinner").show();
            },
            success: function(html) {
                $ga("#ga_description").html(descriptionText);
                $ga("#ga_instruction").html(instructionText);

                $ga("#ga_but_cancel").hide();
                $ga("#ga_but_save").hide();
                $ga("#ga_instruction_textarea").hide();
                $ga("#ga_description_textarea").hide();

                $ga("#ga_but_edit").show();
                $ga("#ga_description").show();
                $ga("#ga_instruction").show();

                $ga("#ga_edit_spinner").hide();
            }
        });
        return false;
    });

    /* -- Review submit --------------------------------- */

    $ga("#ga_but_review_submit")
    .click(function() {
        var itemId  = $ga("#ga_item_id").val();
        var reviewUrl = $ga("#ga_review_url").val();
        var reviewText = $ga("#ga_review_textarea").val();
        var columnType = $ga("#ga_review_column").val();

        $ga.ajax({
            type: "POST",
            url: reviewUrl,
            data: { itemid: itemId, 
                    text: reviewText, 
                    column: columnType,
                    submit_btn: "submit" },
            cache: false,
            beforeSend: function() {
                $ga("#ga_but_review_submit").hide();
                $ga("#ga_review_spinner").show();
            },
            success: function(html) {
                $ga("#ga_review_list").html(html);
                $ga("#ga_review_spinner").hide();
                $ga("#ga_but_review_submit").show();
                $ga("#ga_review_textarea").val('');
            }
        });
        return false;
    });

    /* -- Search bar ------------------------------------ */

    /* Blurred 'Search Games' text and empty when focused */           
    $ga("#ga_search_input").focus(function() {                           
        if (this.value == this.defaultValue) {                         
            this.value = '';                                           
            this.style.color = '#000000';                              
            $ga(this).css('font-style', 'normal');
        }                                                              
    });                                                                

    $ga("#ga_search_input").blur(function() {                            
        if (this.value == '') {                                        
            if ( this.defaultValue ) {                                 
                this.value = this.defaultValue;                        
                this.style.color = '#a0a0a0';                          
                $ga(this).css('font-style', 'italic');
            } else {                                                   
                this.value = '';                                       
                this.style.color = '#000000';                          
                $ga(this).css('font-style', 'normal');
            }                                                          
        }                                                              
    });                                                                

    /* Prevent search submit if empty */
    $ga("#ga_form_search").submit(function() {
        if ($ga("#ga_search_input").val() != "") {
            $ga('#ga_search_submit').hide();
            $ga('#ga_search_submit_spin').show();
            return true;
        }                                                                    
        return false;                                                        
    });                                                                      
                                                                             
    /* Submit form if image button is clicked */                             
    $ga("#ga_search_submit").click(function() {                                
        $ga("#ga_search_input").focus();          // Trigger validation        
        $ga("#ga_form_search").submit();                                       
    });                                                                      

});

