﻿jQuery.fn.center = function () {
    this.css("top", (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop() + "px");
    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");
    return this;
}


var Legal = {
    init:function(){Legal.init = self},
    show:function(){
        //if (Legal.el == null){
        //    Legal.init()
        //}    
            
            div = '<div id="legal-layer" style="width:'+$(document).width()+'; height:'+$(document).height()+';"></div>';
            div2 = '<div id="legal-content"></div> ';
            div3 = '<div id="legal-sub-layer"></div>';
            $('body').append(div);
            $('body').append(div3);
            $('body').append(div2);
            $('#legal-sub-layer').center();
            $('#legal-content').center();
            $('#legal-content').css('top', '50px');
            $('#legal-sub-layer').css('top', '45px');
            try{
            $(window).scrollTo('#header');
            }catch (e){}
            $.get('/legal-text/',function(r){$('#legal-content').html(r)});
        
        
    },
    hide: function(){
        $('#legal-layer').remove()
        $('#legal-sub-layer').remove()
        $('#legal-content').remove()
    }
    
};



var SR_trending = {
    get_trending:function(){
            $.ajax({
            url: '/get-trending-sidebar/',
            success: function(r){
                    var i = 0;
                    
                    for (app in r.results){
                            if (i == 0){
                                i=i+1;
                                //Put first app
                                if (r.results[app].appName.length > 7){
                                    appName = r.results[app].appName.slice(0,7)+'...';
                                }else{
                                    appName = r.results[app].appName;
                                }
                                if ( r.results[app].developerName.length > 10){
                                    devName = r.results[app].developerName.slice(0,10)+'...';
                                }else{
                                    devName = r.results[app].developerName;
                                }
                                
                                $('#sdr-first-app').append('<a href="/'+r.results[app].url+'"><img class="sdr-app-frame" src="/site-media/img/home/app-frame.png"> <img width="60" height="60" class="sdr-app-icon" src="'+r.results[app].appIcon+'" />'+
                                                           '<div class="sdr-first-app-name"><a href="/'+r.results[app].url+'">'+appName+'</a></div> <div class="sdr-first-app-dev">'+ devName +'</div></a>')
                            }else{
                                 // Put the next apps
                                if (r.results[app].appName.length > 12){
                                    appName = r.results[app].appName.slice(0,12)+'...';
                                }else{
                                    appName = r.results[app].appName;
                                }
                                
                                switch (i){
                                    case 1:place='2nd'; break;
                                    case 2:place='3nd'; break;
                                    case 3:place='4th'; break;
                                    case 4:place='5th'; break;
                                }
                                i = i + 1;
                                $('#sdr-next-apps').append('<div class="sdr-place">'+place+'</div><div class="sdr-more-apps-name"><a href="/'+r.results[app].url+'">'+appName+'</a></div> <div class="sdr-separator"></div>')
                            }	
                    }
                    $('#sdr-next-apps').append('<div class="sdr-place"></div><div class="sdr-more-apps-name"><a href="/apps-trending/">View All</a></div> <div class="sdr-separator"></div>')

            },
            dataType: 'json'
            });
    }
}

var Tags = {
    activate: function(){
        $('.sdr-tag').mouseenter(function(){
            $(this).css(
                        {"color":"black","background-image":"url('/site-media/img/sidebar-right/tag-bg-h.png')", "border":"1px solid #aaa"}
                        )})
        .mouseleave(function(){
            $(this).css(
                        {"color":"#666","background-image":"url('/site-media/img/sidebar-right/tag-bg.png')", "border":"1px solid #ccc"}
                        )})
    }
    }
    
$(document).ready(function(){

        

        $("#categories-button").click(function() { //When trigger is clicked...
                
                //Following events are applied to the subnav itself (moving subnav up and down)
                $(this).parent().find("#categories-menu").slideDown('fast').show(); //Drop down the subnav on click
                if ($("#interests-menu").css('display') == "block"){$(this).parent().find("#interests-menu").slideUp('fast');}
                
                $(this).parent().hover(function() {
                }, function(){
                        $(this).parent().find("#categories-menu").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
                });

                //Following events are applied to the trigger (Hover events for the trigger)
                }).hover(function() {
                        $(this).addClass("subhover"); //On hover over, add class "subhover"
                }, function(){	//On Hover Out
                        $(this).removeClass("subhover"); //On hover out, remove class "subhover"
        });
        
        $("#interests-button").click(function() { //When trigger is clicked...
                
                //Following events are applied to the subnav itself (moving subnav up and down)
                $(this).parent().find("#interests-menu").slideDown('fast').show(); //Drop down the subnav on click
                if ($("#categories-menu").css('display') == "block"){$(this).parent().find("#categories-menu").slideUp('fast');}

                $(this).parent().hover(function() {
                }, function(){
                        $(this).parent().find("#interests-menu").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
                });

                //Following events are applied to the trigger (Hover events for the trigger)
                }).hover(function() {
                        $(this).addClass("subhover"); //On hover over, add class "subhover"
                }, function(){	//On Hover Out
                        $(this).removeClass("subhover"); //On hover out, remove class "subhover"
        });


});
