﻿//FUNCTION: Hide press
function hideAllPress() {
    $("div#allPress > div").each(
        function (index, value) {
            $(value).css("display", "none")
        }
    );

    //Reset form
    $("#pressSearchForm")[0].reset();
    $("input#pressSearch").quicksearch("div#allPress > div", {
        "delay": 50,
	    "show": function () {
            $(this).children().css("display", "block");
            $(this).addClass("found");
	    },
	    "hide": function () {
		    $(this).children().css("display", "none");
            $(this).removeClass("found");
	    }
    });
}
//FUNCTION: Show press
function showAllPress() {
    $("div#allPress > div").each(
        function (index, value) {
            $(value).css("display", "block");
        }
    );
}
//FUNCTION: Hide press headings
function hideAllPressHeadings() {
    $("div.press > img").each(
        function (index, value) {
            $(value).siblings("div.pressBrand").css("display", "none");      
        }
    );
}
//FUNCTION: Count articles
function pressArticleCount() {    
    $("form#pressSearchForm #number").text("(" + $("div#allPress > div.found").size() + ")");
}
//FUNCTION: Reset articles
function pressResetArticleCount() {    
    $("form#pressSearchForm #number").text("");
}

//Press page functionality
$(document).ready(function () {

    //WHEN PAGE LOADS

        //Variables
        var url = window.location.pathname;
        var ref = document.referrer;        
        //Declare the default article     
        var defaultAnchor = "campaign_291006";
        var anchor = jQuery.url.attr("anchor");
        //Danish weird characters in UTF HEX
        var aa = "%C3%A5";
        var ae = "%C3%A6";
        var oe = "%C3%B8";
        var AA = "%C3%85";
        var AE = "%C3%86";
        var OE = "%C3%98";
        var Line = "%7C";
        var Quote = "%22";
        //Declare anchor if press page
        if (url == "/about-hestbaek-consult/press") {  
            //Check if google is the referrer (due to a search made on google)
            if (ref.indexOf("google") != -1) {
                hideAllPressHeadings();
                //Check if the referrer string contains a q query (google default)
                if (ref.indexOf("q=") != -1) {         
                    //Check if the click came from page 1 on google
                    if (ref.indexOf("?") != -1) {
                        //Get querystring from ?
                        var queryStringFull = ref.substr(ref.indexOf("?"), ref.length);
                        //Parse querystring by replacing danish weird characters
                        //var queryString = queryStringFull.replace(/aa/g, "å").replace(/ae/g, "æ").replace(/oe/g, "ø").replace(/AA/g, "Å").replace(/AE/g, "Æ").replace(/OE/g, "Ø").replace(/Line/g, "|").replace(/Quote/g, "");
                        var queryString = queryStringFull
                                            .replace(new RegExp(aa,"g"),"å")
                                            .replace(new RegExp(ae,"g"),"æ")
                                            .replace(new RegExp(oe,"g"),"ø")
                                            .replace(new RegExp(AA,"g"),"Å")
                                            .replace(new RegExp(AE,"g"),"Æ")
                                            .replace(new RegExp(OE,"g"),"Ø")
                                            .replace(new RegExp(Line,"g"),"|")
                                            .replace(new RegExp(Quote,"g"),"");
                        //Parse querystring
                        var pQuery = $.parseQuery(queryString);
                        //Set q parameter to search value
                        $("input#pressSearch").val(pQuery.q);
                        //Do the search - quicksearch to due to focus instead of keyup
                        $("input#pressSearch").quicksearch2("div#allPress > div", {
                            "delay": 50,
	                        "show": function () {
                                $(this).children().css("display", "block");
                                $(this).addClass("found");
	                        },
	                        "hide": function () {
		                        $(this).children().css("display", "none");
                                $(this).removeClass("found");
	                        }
                        });
                        $("input#pressSearch").focus();
                    }
                    //or if the link came from page to on google
                    else {
                        //Get querystring from &
                        var queryStringFull = ref.substr(ref.indexOf("&"), ref.length);
                        //Parse querystring by replacing danish weird characters
                        var queryString = queryStringFull
                                            .replace(new RegExp(aa,"g"),"å")
                                            .replace(new RegExp(ae,"g"),"æ")
                                            .replace(new RegExp(oe,"g"),"ø")
                                            .replace(new RegExp(AA,"g"),"Å")
                                            .replace(new RegExp(AE,"g"),"Æ")
                                            .replace(new RegExp(OE,"g"),"Ø")
                                            .replace(new RegExp(Line,"g"),"|")
                                            .replace(new RegExp(Quote,"g"),"");
                        //Parse querystring
                        var pQuery = $.parseQuery(queryString);
                        //Set q parameter to search value
                        $("input#pressSearch").val(pQuery.q);
                        //Do the search - quicksearch to due to focus instead of keyup
                        $("input#pressSearch").quicksearch2("div#allPress > div", {
                            "delay": 50,
	                        "show": function () {
                                $(this).children().css("display", "block");
                                $(this).addClass("found");
	                        },
	                        "hide": function () {
		                        $(this).children().css("display", "none");
                                $(this).removeClass("found");
	                        }
                        });
                        $("input#pressSearch").focus();
                    }
                }
            }
            //If google was not the referrer - just act like nothing happened!
            else {
                var defaultHeading = defaultAnchor.substr(0, defaultAnchor.lastIndexOf('_'));
                if (anchor == null) {
                    hideAllPress();
                    hideAllPressHeadings();         
                    //The Default article
                    $("#press_" + defaultAnchor).css("display", "block");
                    //And the default pressBrand unfolded
                    $("div.press > img[src*='" + defaultHeading + "']").next(".pressBrand").css("display", "block");
                }
                else {
                    var anchorHeading = anchor.substr(0, anchor.lastIndexOf('_'));
                    hideAllPress();
                    hideAllPressHeadings();
                    //The anchored article
                    $("#press_" + anchor).css("display", "block");
                    //And the pressBrand related unfolded
                    $("div.press > img[src*='" + anchorHeading + "']").next(".pressBrand").css("display", "block");
                }
            }
        }

    
    //SEARCH FUNCTION
        //Disable enter-key from the press search form
        $('input#pressSearch').keypress(function(event) {
          if (event.keyCode == '13') {
             return event.keyCode!=13
            }        
        });
        //Show all press prior to search
        $("input#pressSearch").focus(
            function (event) {
                showAllPress();
                hideAllPressHeadings();
            }
        );
        //Update number of articles during search
        $("input#pressSearch").keyup(
            function (event) {
                pressArticleCount();
            }
        );
        $("input#pressSearch").blur(
            function (event) {
                pressArticleCount();
            }
        );

    //PRESS ARTICLES
        //Show Press on click
        $("div.press p").mousedown(
            function (event) {
                var pressId = "press_" + $(this).attr('id');
                pressResetArticleCount();
                hideAllPress();            
                $("#" + pressId).css("display", "block");
            }
        );    
        //Generate read this article at source
        $("div#allPress > div").each(
            function (index, value) {            
                var mediaName = $(value).find('h2').text().substr($(value).find('h2').text().lastIndexOf('|') + 1,$(value).find('h2').text().length);
                var mediaLink = $(value).find('a').attr('href');
                //Check if the article is still online
                if ($(value).find("p.red").text().indexOf("Source") != -1) {
                    $(value).append("<p><a class='more' target='_blank' title='Read this article at" + mediaName + "' href='" + mediaLink + "'>Read this article at " + mediaName + "</a></p>");
                }
                else {
                    $(value).append("<em><i>This article is not online at " + mediaName + ".</em></p>");                    
                }
            }
        );
        //Generate Direct link to this article
        $("div#allPress > div").each(
            function (index, value) {
                var directLink = "http://www.hestbaekconsult.com/about-hestbaek-consult/press#" + $(value).attr('id').replace('press_', '');
                $(value).append("<p>Direct link to this article:<br /><a href='" + directLink + "' title='Direct link to this article' class='small'>" + directLink + "</a></p><p><a href='#top'>To the top</a></p>");
            }
        );   

    //PRESS NAVIGATION (RIGHT COLUMN)
        //Count articles and add before .pressBrand
        $("div.pressBrand").each(
            function (index, value) {
                var numberOfArticles = $(value).children().size();
                $("<p class='articleNumber'>(" + numberOfArticles + ")</p>").insertBefore(value);
            }
        );    
        //Show selected brandPress p
        $("div.press > img").mousedown(
            function (event) {
                if ($(this).next().next(".pressBrand").css("display") == "block") {
                    hideAllPressHeadings();
                }
                else {
                    hideAllPressHeadings();
                    $(this).next().next(".pressBrand").css("display", "block");
                }
            }  
        );
    
});
