var focusedElement = null;
var showAsGrid = true;

var SkanskaOld = {

    Tabs: function () {
        jQuery(".tabs").tabs();

        $("#show-all-people-hits").bind("click", function () {
            $(this).parents(".tabs").tabs("select", "people-hits");
        });

        $("#show-all-offices-hits").bind("click", function () {
            $(this).parents(".tabs").tabs("select", "offices-hits");
        });
    } 
};

$(document).ready(function () {

    /** check US IP cookie **/
   // Skanska.CheckCookie();
    /** make tabs **/
    SkanskaOld.Tabs();

    $(".teaser-row div:first-child").addClass("first-child");
    $(".highlighters div:first-child").addClass("first-child");
    $("h1:first-child").addClass("first-child");
    $("#flash-fallback h4:first-child").addClass("first-child");
    $("#pp-display .tabs > ul:first-child").addClass("first-child");
    $("#pp-display .tabs > ul:first-child li:first-child").addClass("first-child");

    $("input").focus(function () {
        focusedElement = this;
    });

    $(".search-box input[type=button]").each(function () {
        $(this).wrap("<div class=\"js-styled\">");
        $(this).show();
    });

        $(".nationalsearchbutton input[type=button]").each(function () {
            $(this).wrap("<div class=\"js-styled\">");
            $(this).hide();
        });

    /** project display - show on map, open in new window **/
    $("#show-on-map").bind("click", function () {
        var url = $(this).attr("href");
        window.open(url, "map", "width=960,height=640,resizable=yes,scrollbars=yes,status=yes");
        return false;
    });


    $(".mainsearchbutton").bind("click", function () {
        if ($('#mainSearch').val() != "") {
            if ($('#mainSearch').val().length > 1) {
                location.href = $('#mainSearch').attr('url') + '&q=' + $('#mainSearch').val();
            }
        }
    });

    $(".nationalsearchbutton").bind("click", function () {
        SearchNational(this);
    });
    /*
    $("#search-button").bind("click", function () {
        if ($('#quickSearch').val() != "") {
            if ($('#quickSearch').val().length > 1) {
                location.href = $('#quickSearch').attr('url') + '&q=' + $('#quickSearch').val();
            }
        }
    });
    $("#searchbutton").bind("click", function () {
        if ($('#quickSearch').val() != "") {
            if ($('#quickSearch').val().length > 1) {
                location.href = $('#quickSearch').attr('url') + '&q=' + $('#quickSearch').val();
            }
        }
    });*/
    $('input').keypress(function (e) {
        //var quicksearchElement = document.getElementById('quickSearch');
        var code = (e.keyCode ? e.keyCode : e.which);
        /*if (code == 13 && focusedElement.id == 'quickSearch') {
            e.preventDefault();
            if ($('#quickSearch').val().length > 1) {
                location.href = $('#quickSearch').attr('url') + '&q=' + $('#quickSearch').val();
            }
        }*/
        var mainsearchElement = document.getElementById('mainSearch');
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13 && focusedElement.id == 'mainSearch') {
            e.preventDefault();
            if ($('#mainSearch').val().length > 1) {
                location.href = $('#mainSearch').attr('url') + '&q=' + $('#mainSearch').val();
            }
        }
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13 && focusedElement.id == 'search-what-terms') {
            e.preventDefault();
            SearchNational(this);
        }
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13 && focusedElement.id == 'search-where-terms') {
            e.preventDefault();
            SearchNational(this);
        }
    });

    // show/hide sections (display news item)
    $("#news-display .expandable .more").bind("click", function () {
        /* $(this).parents(".expandable-body").children(".wrap").animate({ "max-height": "100em" }); */
        $(this).parents(".expandable-body").children(".wrap").css({ "height": "auto" });
        $(this).parent().children("a").toggle();
    });

    $("#news-display .expandable.less").bind("click", function () {
        $(this).parents(".expandable-body").children(".wrap").css({ "height": "105px" });
        $(this).parent().children("a").toggle();
    });

    // show/hide sections (contact us - global)
    $("#contact-us .expandable").bind("click", function () {
        $(this).parents(".expandable-section").children(".expandable-body").toggle();
        $(this).parents(".expandable-section").find(".expandable-body + p a").toggle();
    }).attr("href", "javascript:void(0)");

    // show/hide sections (contact us - national)
    $("#contact-us-national .expandable .more").bind("click", function () {
        /* $(this).parents(".expandable-body").children(".wrap").animate({ "max-height": "100em" }); */
        $(this).parents(".expandable-body").children(".wrap").css({ "height": "auto" });
        $(this).parent().children("a").toggle();
    });
    $("#contact-us-national .expandable.less").bind("click", function () {
        $(this).parents(".expandable-body").children(".wrap").css({ "height": "3em" });
        $(this).parent().children("a").toggle();
    });

    // detailed search expand button
    $("#detailed-search").bind("click", function () {
        $(this).toggleClass("expanded");
        $(this).parent().prev().toggle();
    });

    // detailed search form dropdown click event
    $(".detailed-search-dropdown-link").click(function (e) {

        this_id = $(this).attr("id");

        // close other open dropdowns
        $(".detailed-search-dropdown-link.active").each(function () {
            if (this_id != $(this).attr("id")) {
                $(this).removeClass("active").next().hide();
            }
        });

        // toggle this dropdown
        $(this).toggleClass("active");
        $(this).next().toggle();

        // stop event propagation
        if (!e) var e = window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
    });

    // clicking on a link in the dropdown
    $(".detailed-search-dropdown a").bind("click", function (e) {
        if (!e) var e = window.event;
        var target = e.target ? e.target : e.srcElement;
        var elm = $(this).parents(".detailed-search-dropdown").prev();
        var txt = $(target).text();

        // add selected class
        $(this).parents(".detailed-search-dropdown").find("li").removeClass("selected");
        $(this).parent().addClass("selected");

        // add "..." if text is too long (might need tweaking)
        if (txt.length > 38) {
            txt = txt.substring(0, 35) + "...";
        }

        // set hidden input element
        elm.parent().children("input").attr("value", $(target).attr("data-search-id"));

        // update text in dropdown trigger link
        elm.text(txt);

        // add class for placeholder text if the first link was clicked
        if ($(target).attr("data-search-id") == "") {
            elm.addClass("placeholder-text-active");
        } else {
            elm.removeClass("placeholder-text-active")
        }

        // close dropdown
        elm.removeClass("active").next().hide();
    });

    // stop event propagation if clicking in the dropdown area (but not on a link)
    $(".detailed-search-dropdown").bind("click", function (e) {
        if (!e) var e = window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
    });

    // close dropdown if clicking in document (other than the dropdown area)
    $(document).bind("click", function () {
        $(".detailed-search-dropdown-link").removeClass("active").next().hide();
    });

    // reset detailed search
   /* $(".reset-detailed-search a").bind("click", function () {
        $(".detailed-search-dropdown-link").each(function () {
            // reset dropdowns
            $(this).prev("input").attr("value", "");
            $(this).text($(this).attr("title"));
            $(this).next(".detailed-search-dropdown").find("li").removeClass("selected");
            $(this).addClass("placeholder-text-active");

            // reset input fields
            $("#search-what-terms").attr("value", $("#search-what-terms").attr("title")).addClass("placeholder-text-active");
            $("#search-where-terms").attr("value", $("#search-where-terms").attr("title")).addClass("placeholder-text-active");
        });
    });*/

    // placeholder support for input fields
  /*  $(".placeholder").each(function () {
        var elm = $(this);
        if (elm.attr("value") == "") {
            elm.attr("value", elm.attr("title"));
            elm.addClass("placeholder-text-active");
        } else if (elm.attr("value") == elm.attr("title")) {
            elm.addClass("placeholder-text-active");
        }
    }).bind("focus", function () {
        var elm = $(this);
        if (elm.attr("value") == elm.attr("title")) {
            elm.attr("value", "");
            elm.removeClass("placeholder-text-active");
        }
    }).bind("blur", function () {
        var elm = $(this);
        if (elm.attr("value").length == 0) {
            elm.addClass("placeholder-text-active");
            elm.attr("value", elm.attr("title"));
        }
    });*/
});

$.easing.custom = function (x, t, b, c, d) {
	return c * ((t = t / d - 1) * t * t + 1) + b;
}


function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);

    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

//-------------------------- National serach start ----------------------------------------
function SearchNational(obj) {
	var where = $('#search-where-terms').val() != $('#search-where-terms').attr("data-placeholder") ? $('#search-where-terms').val() : '';
	var what = $('#search-what-terms').val() != $('#search-what-terms').attr("data-placeholder") ? $('#search-what-terms').val() : '';
	//var catFunc = $('#specific-function-input').val();
	//var catMsCivil = $('#market-segment-civil-input').val();
	//var catMsBuild = $('#market-segment-building-input').val();
	//var catMsConst = $('#market-segment-construction-input').val();
	if (where == '' && what == '')// && catFunc == '' && catMsCivil == '' && catMsBuild == '' && catMsConst == '')
	{ } else {
		//location.href = $(obj).attr('url') + '&where=' + where + '&what=' + what + '&catFunc=' + catFunc + '&catMsCivil=' + catMsCivil + '&catMsBuild=' + catMsBuild + '&catMsConst=' + catMsConst;
		if (where.length > 1 || what.length > 1) {
		    location.href = $(obj).attr('url') + '&wh=' + where + '&wt=' + what;
		}
	}
}

//-------------------------- National serach end ----------------------------------------
