var mdp = mdp || {app:{}};

/** ShareBar
* @author Boz
* @classDescription Share Bar
**/

mdp.app.ShareBar=function(options){
    /* ---[ CLASS VARIABLES ]--- */
    var addThisBoxHeight = 578;
    var self = this;
    var options = options || {};
    var searchLoc;
    if (window.location.href.indexOf("recipe.com") >= 0) {
        searchLoc = "/search/?searchTerm=";
    } else if (window.location.href.indexOf("bhg.com") >= 0) {
        searchLoc = "http://search.bhg.com/search/s?q=";
    } else if (window.location.href.indexOf("midwestliving.com") >= 0) {
        searchLoc = "/"+mdp.site.brand+"/search/results.jsp?searchType=allsearch&searchString=";
    } else {
        searchLoc = "/"+mdp.site.brand+"/search/results.jsp?searchType=content&searchString=";
    }

    var settings = {
        barOffset: options.barOffset || 33,
        dealsPos:{position:"bottom",offsetY:-50,width:520,height:340},
        twitterPos:{position:"bottom",offsetY:-50,width:795,height:380},
        facebookPos:{position:"bottom",offsetY:-50,width:785,height:536},
        hottopicPos:{position:"bottom", height:240, width:300,offsetY:-50,offsetX:-200},
        holidaysPos:{position:"bottom", height:240, width:300,offsetY:-50,offsetX:225}
   };

    /* $.extend(settings,options); */
    if(typeof(options.twitterPos) == 'object')$.extend(settings.twitterPos,options.twitterPos);
    if(typeof(options.dealsPos) == 'object')$.extend(settings.dealsPos,options.dealsPos);
    if(typeof(options.facebookPos) == 'object')$.extend(settings.facebookPos,options.facebookPos);
    if(typeof(options.hottopicPos) == 'object')$.extend(settings.hottopicPos,options.hottopicPos);
    if(typeof(options.holidaysPos) == 'object')$.extend(settings.holidaysPos,options.holidaysPos);

    /* init */
    positionBar();
    setupEventListeners();

    /* ---[ PRIVATE METHODS ]--- */

    function positionBar(event){
        /* Position Share Bar */
        var sb = document.getElementById("sb");

        /*Make sure sb exists*/
        if (sb != null) {
            if( navigator.platform.indexOf("iPad") != -1){
                sb.style.top = window.innerHeight  - settings.barOffset + window.pageYOffset + "px";
            }else{
                sb.style.top = document.documentElement.clientHeight - settings.barOffset + "px";
            }
        }
    }

    function showHolidayLinks() {
        new mdp.app.Dialog($("#holidayLinks").clone().removeClass("hide"),settings.holidaysPos);
    }


    /* ---[ EVENT LISTENERS ]--- */
    function setupEventListeners() {
        /* compound query to get recipe search input when on recipe.com */
        $("#sb_search_input, #sb_recipe_search_input").bind({
            "keydown":function(event){
                if(event.keyCode == 13){
                    window.location.href = searchLoc+this.value;
                }
            },
            "focus":function(){
                this.value = "";
                $(this).css('color','#000');
            },
            "blur":function(){
                if(this.value == ""){
                    this.value = "Search";
                }
                $(this).css('color','#aaa');
            }
        });

        window.onresize = function(){
            positionBar();
        };

        /* Maintains position of share bar on scroll */
	    $(document).bind('scroll touchend', function(){
			positionBar();
	    });

        $("#sb_close").click(function(){
            $("#sb").remove();
            RemoteScrapbookService.storeScrapbookValue("sharebar","hide","true");
        });

        $("#sb_deals").click(function(){
            var iframe = $("<iframe>").attr("src","/parents/templates/common/deals.jsp");
            iframe.attr({"id":"dealsframe","scrolling":"no","marginheight":0,marginwidth:0,"frameborder":0});
            iframe.css({width:"520px",height:"340px"});
            new mdp.app.Dialog(iframe,settings.dealsPos);
        });

        $("#sb_twitter").click(function(){
            var iframe = $("<iframe>").attr("src","/common/socialmedia/twitter/dashboard.jsp");
            iframe.attr({"id":"twitterframe","scrolling":"no","marginheight":0,marginwidth:0,"frameborder":0});
            iframe.css({width:"795px",height:"475px"});
            new mdp.app.Dialog(iframe,settings.twitterPos);
        });

        $("#sb_facebook").click(function(){
            var iframe = $("<iframe>").attr("src","/common/socialmedia/facebook/dashboard.jsp");
            iframe.css({width:"785px",height:"536px"});
            iframe.attr({"scrolling":"no","marginheight":0,marginwidth:0,"frameborder":0});
            new mdp.app.Dialog(iframe,settings.facebookPos);
        });

        $("#sb_hottopic").mouseenter(function(){
            new mdp.app.Dialog($("#hottopics").clone().removeClass("hide"),settings.hottopicPos);
        });

        $("#sb_holiday").bind({
            "mouseenter":function(){
                mdp.sbHolidayTimeout = setTimeout(showHolidayLinks,100);
            },
            "mouseleave":function(){
                clearTimeout(mdp.sbHolidayTimeout);
            },
            "click":showHolidayLinks
        });

        /* compound query to get recipe search input when on recipe.com */
        $("#sb_search_link, #sb_recipe_search_link").click(function(){
            var term = $("#sb_search_input").val();
            if(term != "" && term != "Search"){
                window.location.href = searchLoc+$("#sb_search_input, #sb_recipe_search_input").val();
            }
        });

        /* pop the expand icon because we don't want a tooltip for it */
        var icons = $("#sb .icons");
        if(!$.fn.tips){
            $.ajax({
            url: "/web/js-min/js/mdp/lib/jquery/plugins/tips/jquery.mootips.js",
            dataType: "script",
            success: function(){
                icons.tips({fixed:true,"className":"sb",offsets:{x:-13,y:-25},styles:{
                    'position': 'fixed',
                    'top': '0',
                    'left': '0',
                    'visibility': 'hidden'
                }});
            }
            });
        } else {
            icons.tips({fixed:true,"className":"sb",offsets:{x:-13,y:-25},styles:{
                'position': 'fixed',
                'top': '0',
                'left': '0',
                'visibility': 'hidden'
            }});
        }
    }

    return {};
};
