﻿//FUNCTION: Hide cases
function hideAllCases() {
    $("div#allCases > div").each(
        function (index, value) {
            $(value).css("display", "none")            
        }
    );

    //Reset form
    $("#casesSearchForm")[0].reset();
    $("input#casesSearch").quicksearch("div#allCases > 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 cases
function showAllCases() {
    $("div#allCases > div").each(
        function (index, value) {
            $(value).css("display", "block");
        }
    );
}
//FUNCTION: Count articles
function articleCount() {    
    $("form#casesSearchForm #number").text("(" + $("div#allCases > div.found").size() + ")");
}
//FUNCTION: Reset articles
function resetArticleCount() {    
    $("form#casesSearchForm #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 = "search_nordea";
        var anchor = jQuery.url.attr("anchor");
        //Danish weird characters
        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 == "/cases") {  
            //Check if google is the referrer (due to a search made on google)
            if (ref.indexOf("google") != -1) {                  
                //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(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#casesSearch").val(pQuery.q);
                        //Do the search - quicksearch to due to focus instead of keyup
                        $("input#casesSearch").quicksearch2("div#allCases > div", {
                            "delay": 50,
	                        "show": function () {
                                $(this).children().css("display", "block");
                                $(this).addClass("found");
	                        },
	                        "hide": function () {
		                        $(this).children().css("display", "none");
                                $(this).removeClass("found");
	                        }
                        });
                        $("input#casesSearch").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#casesSearch").val(pQuery.q);
                        //Do the search - quicksearch to due to focus instead of keyup
                        $("input#casesSearch").quicksearch2("div#allCases > div", {
                            "delay": 50,
	                        "show": function () {
                                $(this).children().css("display", "block");
                                $(this).addClass("found");
	                        },
	                        "hide": function () {
		                        $(this).children().css("display", "none");
                                $(this).removeClass("found");
	                        }
                        });
                        $("input#casesSearch").focus();
                    }
                }
            }
            //If google was not the referrer - just act like nothing happened!
            else {
                var defaultHeading = defaultAnchor.substr(0, defaultAnchor.lastIndexOf('_'));
                if (anchor == null) {
                    hideAllCases();                        
                    //The Default article
                    $("#case_" + defaultAnchor).css("display", "block");                    
                }
                else {
                    var anchorHeading = anchor.substr(0, anchor.lastIndexOf('_'));
                    hideAllCases();
                    //The anchored article
                    $("#case_" + anchor).css("display", "block");                    
                }
            }
        }

    
    //SEARCH FUNCTION
        //Disable enter-key from the cases search form
        $("input#casesSearch").keypress(function(event) {
          if (event.keyCode == '13') {
             return event.keyCode!=13
            }        
        });
        //Show all cases prior to search
        $("input#casesSearch").focus(
            function (event) {
                showAllCases();
            }
        );
        //Update number of articles during search
        $("input#casesSearch").keyup(
            function (event) {
                articleCount();
            }
        );
        $("input#casesSearch").blur(
            function (event) {
                articleCount();
            }
        );

    //CASES ARTICLES
        //Show Case on click
        $("div.caseLogos img").mousedown(            
            function (event) {                
                var caseId = "case_" + $(this).attr('id');
                hideAllCases();
                resetArticleCount();
                $("#" + caseId).css("display", "block");
            }
        );
        //Generate Direct link to this article
        $("div#allCases > div").each(
            function (index, value) {
                var directLink = "http://www.hestbaekconsult.com/cases#" + $(value).attr('id').replace('case_', '');
                $(value).append("<p>Direct link to this article:<br /><a href='" + directLink + "' title='Direct link to this article' class='small'>" + directLink + "</a></p>");
            }
        );        
        //Hover for case images
        $("div.caseLogos img").mouseenter(
            function (event) {
                $(this).animate({ opacity: 0.6 }, 200)
            }
        );
        $("div.caseLogos img").mouseout(
            function (event) {
                $(this).animate({ opacity: 1 }, 100)
            }
        );
});
