﻿// GA variable
var _gaq = _gaq || [];

$(document).ready(function() {
    // set up scrollable statements
    setupScrollables();
    // set up accordion
    setupAccordion();
    // process team page ajax-style
    ajaxTeam();
    // alter iContact form
    iContactForm();

    $("#content, #sidebar, .col-1, .col-2, #nav li ul").corner("bl tr 20px");
    $(".threecolumns .col").corner("bl tr 30px");

    // Insert an "Overview" item on each drop down except for 'Portfolio'
    $('#nav li ul').each(function() {
        var xElm = $('li:eq(0)', this);
        if (xElm.find("a:first").attr("href").toLowerCase().indexOf("/portfolio") < 0 && xElm.find("a:first").attr("href").toLowerCase().indexOf("/news") < 0) {
            xElm.before('<li><a href="' + xElm.parents("li:first").find("a").attr("href") + '">Overview</a></li>');
        }
    });

    $("#nav > li:first > ul > li > a[href^='/About/Information-for-']").css({ paddingLeft: "20px" });
    $("#nav > li:first > ul > li > a[href^='/LP-Log-In']").css({ paddingLeft: "20px" });
    $("#nav > li:first > ul > li:eq(1)").before("<li class='info-for'>Information for:</li>");

    equalHeight($(".col-1, .col-2, .col-3"));

    var path = location.pathname.toLowerCase();
    if ((path.indexOf("/our-team/") < 0) && (path.indexOf("/portfolio") < 0) && (path.indexOf("/news") < 0) && (path.indexOf("/faq") < 0)) {
        equalHeight($("#sidebar, #content"));
        $("#sidebar").height($("#sidebar").height() + 20);
    }
    if ((path.indexOf("/news/milestone") > -1) || (path.indexOf("/news/portfolio") > -1) || (path.indexOf("/news/partner") > -1)) {
        var start = path.indexOf("/news/") + "/news/".length;
        var newHeader = $("<h2>").addClass("news-header").append(location.pathname.substring(start, path.indexOf('/', start + 1)).replace('-', ' '));
        $("#main .twocolumns").prepend(newHeader);
    }

    // external links open in new window
    $('#content a[href^="http://"]').attr('target', '_blank');

    // Set up Google Analytics
    _gaq.push(['_setAccount', 'UA-18348872-1']);
    _gaq.push(['_setDomainName', '.milestonepartners.com']);
    _gaq.push(['_trackPageview']);

    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    $.getScript(gaJsHost + "google-analytics.com/ga.js", function() { /*should not need to do anything here*/ });

});

function iContactForm() {
	// fix field on contact form
	var x = $(".CheckBoxField", ".FormPanel").parent();
	$(".CheckBoxField", ".FormPanel").parent().parent().prev().append(x);
	$(".CheckBoxField", ".FormPanel").parent().parent().next().remove();
	$(".CheckBoxField", ".FormPanel").parent().parent().attr("colspan", "2");
	$(".CheckBoxField", ".FormPanel").parent().css("float", "right").css("margin-right", "50px");
	
	$("#icontact-form input[type=submit]").click(function() {
		var errorMessage = "";
		if ($("#icontact-form input[name=fields_fname]").val() == "") {
			errorMessage += "Please enter a first name" + "\n";
		}
		if ($("#icontact-form input[name=fields_lname]").val() == "") {
			errorMessage += "Please enter a last name." + "\n";
		}
		if ($("#icontact-form input[name=fields_email]").val() == "") {
			errorMessage += "Please enter an e-mail address." + "\n";
		}
		if (errorMessage != "") {
			alert(errorMessage);
			return;
		}
		if (window.location.hostname != "milestonepartners.com") {
			$("#icontact-form #icontact-redirect").attr($("#icontact-form #icontact-redirect").attr("value").replace("milestonepartners.com", window.location.hostname));
			$("#icontact-form #icontact-error").attr($("#icontact-form #icontact-error").attr("value").replace("milestonepartners.com", window.location.hostname));
		}
		$("#form1").attr("action", "https://app.icontact.com/icp/signup.php");
	});	
}

function setupScrollables() {
    // If more than one statement, set up a slider
	if ($(".scrollable li", "#header").length > 1) {
        $(".scrollable", "#header").scrollable({ circular: true }).autoscroll({ interval: 12000, autoplay: false });
    }
    
	$(".text-box .scrollable").scrollable({ circular: true }).autoscroll({ interval: 6000, autoplay: true });
}

function setupAccordion() {
	$(".accordion").accordion({ autoHeight: false }).css("height", "280px");
}

function ajaxTeam() {
	//$(".question li").delegate("a", "click", function() {
    $(".question li a").live("click", function() {
        var $a = $(this);
        var href = $a.attr("href");
        window.location.hash = href.replace(location.pathname + "/", "");
        if ($("#sidebar").height() < $("#main #content").height()) {
            $(".main-holder").animate({ height: $("#main #content").height() + 120 + "px" });
        }
        $(".team #content").hide("slide", { direction: "left" }, 250, function() {
            $(".team #content").load(href + " #content .information", function() {
                if ($("#sidebar").height() < $("#main #content").height()) {
                    $(".main-holder").animate({ height: $("#main #content").height() + 120 + "px" });
                }
                $(this).show("slide", { direction: "left" }, 250);
                $(this).corner("bl tr 20px");
                return;
                VSA_initScrollbars();
            })
        });
        $(".question li").removeClass("active");
        $(this).parent().addClass("active");
        return false;
    });

    if (window.location.hash != "") {
        //populate first item and set active state
        var href = location.pathname + window.location.hash.replace("#", "/");
        $(".team #content").load(href + " #content .information", function() { $(this).corner("bl tr 20px"); return; VSA_initScrollbars(); });
        $(".question li a[href='" + href + "']").parent("li").addClass("active");
    }
    else {
        //populate first item and set active state
        $(".team #content").load($(".question li a").first().attr("href") + " #content .information", function() { $(this).corner("bl tr 20px"); return; VSA_initScrollbars(); });
        $(".question li").first().addClass("active");
    }
}

function equalHeight(group) {
    var tallest = 0;
    group.each(function() {
        var thisHeight = $(this).height();
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}
