/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function(){
    $(".infobox").css("display","none");

    function showDesc(obj,visibility)
    {
        if(visibility == 'none')
        {
            //change the icon from down to up
            $(obj).children("img").attr("src","/images/up.png");
            //show the content
            $(obj).parent().parent().next(".rwdDesc").css("display","block");
        }
        else
        {
            //change the icon from down to up
            $(obj).children("img").attr("src","/images/down.png");
            $(obj).parent().parent().next(".rwdDesc").css("display","none");
        }
    }
    $(".showDescription").click(function(event){
        event.preventDefault();
        var isShowing = $(this).parent().parent().next(".rwdDesc").css("display");
        showDesc(this,isShowing);
    });
    $(".showDescription").live("click",function(event){
        event.preventDefault();
        var isShowing = $(this).parent().parent().next(".rwdDesc").css("display");
        showDesc(this,isShowing);
    });
    $(".wishListReward").click(function(event){
        event.preventDefault();
        var currentWLStatus = $(this).children().attr("title");
        //fade this out

        if(currentWLStatus == "add")
        {
            $(this).animate({
                opacity: 0
            },500);
            $(this).children().attr({
                src: "/images/remove_wishlist.png",
                title:"remove"
            });
            $(this).animate({
                opacity: 1
            },1000);

            //put a message in the message div
            var headerVal = $(this).parents(".rwdHolder").children(".rwdTitle").find("h3").text();
            $(".infobox .content").html("<span class='fadeMessage'>Added " + headerVal + " to Wishlist</span><br />");
            $(".infobox").css("display","block");
            $(".infobox").fadeOut(2000);
        }
        else
        {
            $(this).animate({
                opacity: 0
            },1000).children().attr({
                src: "/images/add_to_wishlist_small.png",
                title:"add"
            });
            $(this).animate({
                opacity: 1
            },1000);

            headerVal = $(this).parents(".rwdHolder").children(".rwdTitle").find("h3").text();

            $(".infobox .content").html("<span class='fadeMessage'>Removed " + headerVal + " from Wishlist</span><br />");
            $(".infobox").css("display","block");
             $(".infobox").fadeOut(2000);
        }
    });

    //search
    $("#rewardSearchText").keyup(function(e){
        var thisLength = $(this).val().length;

        if(thisLength > 2)
        {

            //send the values to a search script
            var thisSearch = escape($(this).val());
            sendSearch(thisSearch,'reward','#rewardSearchHolder','list');
        }
    });

    function sendSearch(searchText,type,divID,output)
    {
        var url = '/scripts/search.php';
        var outputDiv = divID;
        //set the type
        url += (type == 'reward')? '?type=reward' : '?type=promo';
        url += '&search='+searchText;
        url += '&output='+output;

        //send the search request
        $.post(url, function(data){
            $(outputDiv).css("display","block");
            $(outputDiv).html(data);
        });
    }

    $("#rewardSearchText").blur(function(){
        $("#rewardSearchHolder").fadeOut(1000);
    });

    $(".rewardsForm").submit(function(e){
        e.preventDefault();
        var thisSearch = escape($("#rewardSearchText").val());
        $("#gridTitleRewards").text("Search Results for "+thisSearch);
        $("#gridTitleRewards").after('<a href="#" class="clearSearch">Clear Search</a>');
        sendSearch(thisSearch,'reward','#rwdsGrid','grid');
    });

    $(".showFullRewardResults").live("click",function(e){
        e.preventDefault();
        var thisSearch = escape($("#rewardSearchText").val());
        $("#gridTitleRewards").text("Search Results for "+thisSearch);
        $("#gridTitleRewards").after('<a href="#" class="clearSearch">Clear Search</a>');
        sendSearch(thisSearch,'reward','#rwdsGrid','grid');
    });

    $("#promoSearchText").keyup(function(e){
        var thisLength = $(this).val().length;

        if(thisLength > 2)
        {

            //send the values to a search script
            var thisSearch = escape($(this).val());
            sendSearch(thisSearch,'promo','#promoSearchHolder','list');
        }
    });

    $("#promoSearchText").blur(function(){
        setTimeout(function(){$("#promoSearchHolder").fadeOut(1000);},500);
    });

    $(".promoSearch").submit(function(e){
        e.preventDefault();
        var thisSearch = escape($("#promoSearchText").val());
        $("#gridTitlePromo").text("Search Results for "+$("#promoSearchText").val());
        $("#gridTitlePromo").after('<a href="#" class="clearSearch">Clear Search</a>');
        sendSearch(thisSearch,'promo','#promoGrid','grid');
    });

    $(".showFullPromoResults").live("click",function(e){
        e.preventDefault();
        var thisSearch = escape($("#promoSearchText").val());
        $("#gridTitlePromo").text("Search Results for "+$("#promoSearchText").val());
        $("#gridTitlePromo").after('<a href="#" class="clearSearch">Clear Search</a>');
        sendSearch(thisSearch,'promo','#promoGrid','grid');
    });
    $(".showLoginForm").click(function(e){
         var signInDisplay = $("#signinForm").css("display");
         if(signInDisplay == "none")
         {
              $("#loginButtonDiv").css({"background-color":"#C2E1EF","-moz-border-radius-bottomleft":"0px", "-webkit-border-bottom-left-radius":"0px","-moz-border-radius-bottomright":"0px", "-webkit-border-bottom-right-radius":"0px", "border-bottom":"2px solid #C2E1EF"});
              $("#signinForm").css("display","block");
         }
         else
         {
            $("#signinForm").css("display","none");
            $("#loginButtonDiv").css({"background-color":"#5396B7","-moz-border-radius-bottomleft":"5px", "-webkit-border-bottom-left-radius":"5px","-moz-border-radius-bottomright":"5px", "-webkit-border-bottom-right-radius":"5px", "border-bottom": "2px solid #FFF"});
         }
    });
});



