$(function(){

    //grid tooltips
    $(".gridtooltips").each(function(){
        $(this).tooltip({ 
            delay: 0,
            track: true, 
            bodyHandler: function() { 
                return "<div id='floaterheader'></div><div id='floaterbody'><h3>"+$(this).attr("caption")+"</h3><p>"+$(this).attr("desc")+"</p><h4>"+$(this).attr("rating")+" Like | "+$(this).attr("played")+"</h4></div><div id='floaterfooter'></div>"; 
            }, 
            showURL: false 
        });
    });



    //tooltips popular games
    $(".gamelistclass").each(function(){
        $(this).tooltip({ 
            delay: 0,
            track: true,
            extraClass: "gamelistpopularfixing",
            bodyHandler: function() { 
                return "<div id='floaterheaderlist'></div><div id='floaterbodylist'><img style='width: 120px; height: 90px;' src='"+$(this).attr("thumbnail")+"' /></div><div id='floaterfooterlist'></div>"; 
            }, 
            showURL: false 
        });
    });


    //featured slide show
    $('#slideshowfeatured').cycle({
        fx: "fade",
        speed:  200,
        timeout: 5000,
        pagerEvent: 'mouseover', 
        pauseOnPagerHover: true,
        pager:  '#nav',

        pagerAnchorBuilder: function(idx, slide) {
            return '<li><a><img src="' + $(slide).find("img")[0].src + '" /></a></li>';
        }
    });

    //rategame
    $("#like").click(function(){

        $.ajax({
            type: "GET",
            url: "action.php",
            data: "act=rate&gameid="+$("#gameid").val()+"&val=like",
            beforeSend: function(){
                $("#likedislike").hide();
                $("#loader").show();
            },
            success: function(msg){
                if($(msg).find("result").text() == "notallowed"){ 
                    alert('Error - Rate. You can give rate for this game once a day. \n\nYou will able to vote again in '+$(msg).find('note').text()); 
                }
                $("#likedislike").show();
                $("#loader").hide();
                if($(msg).find('likecount').text() !== ""){
                    $("#likecount").text($(msg).find('likecount').text());
                }
            }
        })
    })

    $("#dislike").click(function(){
        $.ajax({
            type: "GET",
            url: "action.php",
            dataType: "xml",
            data: "act=rate&gameid="+$("#gameid").val()+"&val=dislike",
            beforeSend: function(){
                $("#likedislike").hide();
                $("#loader").show();
            },
            success: function(msg){
                if($(msg).find("result").text() == "notallowed"){ 
                    alert('Error - Rate. You can give rate for this game once a day. \n\nYou will able to vote again in '+$(msg).find('note').text()); 
                }
                $("#likedislike").show();
                $("#loader").hide();
                if($(msg).find('likecount').text() !== ""){
                    $("#likecount").text($(msg).find('likecount').text());
                }

            }
        })
    })

    //search
    $("#search").focus(function(){
        $(this).val("");
    })

    $("#search").blur(function(){
        if($(this).val()==""){
            $(this).val("Search...");
        }
    })

    $("#searchform").submit(function(){
        if($("#search").val()==""){
            alert("Enter your keyword!");
            return false;
        }
    })

    //commentbox
    $("#commentboxdata").load("templates/default/commentbox.php?gameid="+$("#gameid").val());

    $("#commenttext").focus(function(){
        $(this).select();
    })

    $("#namecomment").focus(function(){
        $(this).select();
    })

    $("#shout").click(function(){
        if($("#commenttext").val() == "Add Your Comment..." || $("#commenttext").val()==""){
            alert("Error - Comment Box. Please fill comment field.");
            $("#commenttext").val("");
            $("#commenttext").focus();
        }else{
            $.ajax({
                type: "GET",
                url: "action.php",
                data: "act=comment&comment="+$("#commenttext").val()+"&gameid="+$("#gameid").val()+"&name="+$("#namecomment").val(),
                beforeSend: function(){
                    $("#confirmsending").show();
                    $("#shout").hide();
                },
                success: function(msg){
                    if(msg=="success"){
                        $("#commentboxdata").load("templates/default/commentbox.php?gameid="+$("#gameid").val());
                        $("#commenttext").val("");   
                    }else{
                        alert("Error - Commentbox. We fail to connect to our server, please try again latter.");
                    }
                    $("#confirmsending").hide();
                    $("#shout").show();
                }
            })
        }
    })

    //add to fav
    $("#addtofav").click(function(){
        $.ajax({
            type: "GET",
            url: "action.php",
            data: "act=addgametofav&gameid="+$("#gameid").val(),
            beforeSend: function(){
                $("#addtofav a").hide();
                $("#addingtofav").show(); 
            },
            success: function(msg){
                $("#addtofav").text("Added To My Games");
                $("#addtofav").css({background: "#FF6600",color: "black"})
                $("#addingtofav").hide(); 
            }
        })
    })

    //delele fav
    $(".deletefav span").each(function(){
        var item = $(this);
        $(this).click(function(){
            $.ajax({
                type: "GET",
                url: "action.php",
                data: "act=removegamefromfav&gameid="+$(this).attr("gameid"),
                beforeSend: function(){

                },
                success: function(msg){
                    $(item).text("deleted");
                    $(item).css("margin-left","72px"); 
                }
            })
        }) 
    });

    //contact us
    $("#btcontact").click(function(){
        $.ajax({
            type: "GET",
            url: "action.php",
            data: "act=contactus&name="+$("#name").val()+"&email="+$("#email").val()+"&subject="+$("#subject").val()+"&description="+$("#description").val(),
            beforeSend: function(){
                $("#btcontactcontainer").html("Sending..."); 
            },
            success: function(msg){
                $("#btcontactcontainer").html("Thanks For Contacting Us.");
            }
        }) 
    })
    
    //ads
    $("#closeads").click(function(){
        $("#adsbox").hide();
    })
    
    $("#covertimer").countdown({
        until: +20, 
        compact: true, 
        format: 'S', 
        onExpiry: function(){
            $("#adsbox").hide();
        }
    });
});
