﻿//Define Kpw object only if it's not already defined
if ("undefined" == typeof Kpw) {Kpw = {};}

//Initialize ImageClick
$().ready(function(){
	Kpw.ExpertAdvice.init();
});

if ("undefined" == typeof Kpw.ExpertAdvice) {
    Kpw.ExpertAdvice = {
        Article: {
            animate: function(ui, isAjaxRequired) {
                var advice = $(ui).parents("div.articlesWrapper");
                var adviceOffset = advice.offset();
                var articleWrapper = $("div.expertadvice div.articleWrapper");
                var left = adviceOffset.left + 250;
                articleWrapper
                    .css("top", adviceOffset.top)
                    .css("left", left + "px");

                var advice = $("div.advice");

                /* buffers */
                var padding = 10;
                var margin = 5;
                var borders = 2;
                var adviceHeight = (parseInt(advice.eq(0).outerHeight(), 10) * 2) - padding - margin - borders;
                var articleLink = $(ui).parent().find(".articleLink").val() + " div.article";

                if (isAjaxRequired) {
                    articleWrapper.load(articleLink, function() {
                        Kpw.ExpertAdvice.Article.display(articleWrapper, adviceHeight);
                    });
                } else {
                    Kpw.ExpertAdvice.Article.display(articleWrapper, adviceHeight);
                }

            },
            close: function(ui, callback) {
                $("div.articleWrapper").fadeOut("slow", function() {
                    $(this).children("div.article").remove();
                    if ("function" == typeof callback) {
                        callback();
                    }
                });
            },
            display: function(articleWrapper, adviceHeight) {
                if ($.browser.msie && parseInt($.browser.version, 10) < 8) {
                    $("div.articleWrapper div.article div.copy").addClass("copyIe7");
                }
                $("div.article").height(adviceHeight + "px");
                articleWrapper.fadeIn("slow");
                Kpw.PhotoInfo.init();

                var contentUrl = $('div.article .contentUrl').val();
                var contentTitle = $('div.article .contentTitle').val();
                $(".contentBookmarks").bookmark({
                    url: contentUrl,
                    title: contentTitle,
                    sites: ['twitter']
                });

                Kpw.Facebook.init();

            },
            show: function(ui, isAjaxRequired) {
                $.scrollTo("div.articlesWrapper", 800, { easing: "swing" });
                var article = $("div.articleWrapper div.article");
                if (article.length > 0 && isAjaxRequired) {
                    Kpw.ExpertAdvice.Article.close(ui, function() {
                        Kpw.ExpertAdvice.Article.animate(ui, isAjaxRequired);
                    });
                } else {
                    Kpw.ExpertAdvice.Article.animate(ui, isAjaxRequired);
                }
            }
        },
        bindEvents: function() {
            $("li.adviceWrapper div.imageWrapper div.highlight").fadeTo("fast", .001);
            $("li.adviceWrapper div.imageWrapper img").mouseover(function() {
                var image = $(this);
                var imageOffset = image.offset();
                var highlight = image.prev();
                if (highlight.attr("data-kpw-highlight") != "true") {
                    highlight
                        .attr("data-kpw-highlight", "true")
                        .css("width", image.outerWidth(true))
                        .css("height", image.outerHeight(true))
                        .css("left", imageOffset.left)
                        .fadeTo("fast", 0.4, function() {
                            $(this).fadeTo("def", 0.01, function() {
                                highlight
                                    .css("width", "0px")
                                    .css("height", "0px");
                                setTimeout(function() {
                                    highlight.removeAttr("data-kpw-highlight");
                                }, 1);
                            });
                        });
                }
            });

        },
        init: function() {
            //bind events
            Kpw.ExpertAdvice.bindEvents();
            Kpw.Filter.init();

            //if there is a selected article show it now.
            if ($("li.selectedArticle").length > 0) {
                //var imageWrapper = $("li.selectedArticle div.advice div.imageWrapper")
                //Kpw.ExpertAdvice.Article.show(imageWrapper, false);
                var advice = $("div.articlesWrapper");
                var adviceOffset = advice.offset();
                var articleWrapper = $("div.expertadvice div.articleWrapper");
                var left = adviceOffset.left + 250;
                articleWrapper
                    .css("top", adviceOffset.top)
                    .css("left", left + "px");
            }
        }
    }
}