/*
    ----------------------------------------
    Alberta Innovates Main JS
    Sean Mitchell, sean@seanmitchell.ca
    06 / 26 / 11
    ----------------------------------------
*/



$(document).ready(function () {

    var theReferrer = document.referrer.substring(0, document.referrer.indexOf("/", 14));

    $("#header ul").append('<li class="linkedinpop" ><a href="/linkedin.html" id="linkedinpop">Visiting from LinkedIn?</a></li>');

    $("#linkedinpop").fancybox({
        "width": 800,
        "height": 600,
        "padding": 8,
        "overlayOpacity": 0.6,
        "overlayColor": "#000",
        "autoScale": false,
        "transitionIn": "none",
        "transitionOut": "none",
        "type": "iframe"
    });

    if(theReferrer == "http://www.linkedin.com") {
        $("#linkedinpop").trigger("click");
    }

    $("#banner ul").slideshow({timeout: 8000, speed: 500});

    $("li.twitter a").click(function(event) {
        var width = 550, height = 420, left = ($(window).width()-width)/2, top = ($(window).height()-height)/2, url = this.href, opts = 'status=1'+', width='+width+', height='+height+', top='+top+', left='+left;
        window.open(url, "twitter", opts);
        return false;
    });

    $("li.linkedin a").click(function(event) {
        var width = 600, height = 420, left = ($(window).width()-width)/2, top = ($(window).height()-height)/2, url = this.href, opts = 'status=1'+', width='+width+', height='+height+', top='+top+', left='+left;
        window.open(url, "linkedin", opts);
        return false;
    });

    if($("#home").length > 0) {

        $("#utility ul li a").click(function() {
            var filterName = $(this).attr("data-filter");
            $("#home div.inner").isotope({
                filter: filterName
            });
            return false;
        });

        $("#utility").find("div.inner ul li a").click(function() {
            var $this = $(this);
            if(!$this.hasClass("selected")) {
                $this.parents("div.inner").find(".selected").removeClass("selected");
                $this.addClass("selected");
            }
        });

        function setPlayPosition() {
            $("div.play p").each(function() {
                $(this).css("height", $(this).parents("a:first").height());            
                $(this).css("width", $(this).parents("a:first").width());            
            });
        };

        setPlayPosition();

        function setExcerptWidth() {
            $("#home div.entry div.excerpt").each(function() {
                $(this).css("width", $(this).parent("div.entry:first").css("width")-30);    
            });
        };

        $("#home div.entry a").hover(function() {
            $(this).find("div.excerpt").animate({left: "0"}, {queue: false, duration: 300});
        }, function() {
            $(this).find("div.excerpt").animate({ left: "287"}, {queue: false, duration: 300});
        });

        function getColumnWidth() {
            $("div.entry").each(function() {
                if($(this).hasClass("feature") == false) {
                    myColumnWidth = $(this).width()+12;
                }
            });
            return myColumnWidth;
        };

        $("#home div.inner").isotope({
            animationEngine: "jquery",
            animationOptions: {
                duration: 300,
                easing: "swing",
                queue: false
            },
            itemSelector: "div.entry",
            masonry: {
                columnWidth: getColumnWidth()
            }
        });

        $.extend($.Isotope.prototype, {
            _masonryResizeChanged: function() {
                $("#home div.inner").isotope("option", {
                    masonry: {
                        columnWidth: getColumnWidth()
                    }
                });
                $("#home div.inner").isotope("reLayout");
                setPlayPosition();
                setExcerptWidth();
            }
        });

    }

});

$(window).load(function() {
    if($("#home").length > 0) {
        $("#home div.inner").isotope("reLayout");
    }
});

function submitForm() {
	var form = document['formlayout'];
	var formName = form.name.value;
	var formContact0 = form.contact[0].checked;
	var formContact1 = form.contact[1].checked;
	var formPhone = form.phone.value;
	var formEmail = form.email.value;
	var formCountry = form.country.value;
	var formBackground0 = form.background[0].checked;
	var formBackground1 = form.background[1].checked;
	var formBackground2 = form.background[2].checked;
	var formQuestion0 = form.question[0].checked;
	var formQuestion1 = form.question[1].checked;
	var formQuestion2 = form.question[2].checked;
	var formQuestion3 = form.question[3].checked;
	var formQuestion4 = form.question[4].checked;
	if(formName == "") {
		alert("Please Enter Your Name.");
		form.name.focus();
		return false;
	} else if(formContact0 == 0 && formContact1 == 0) {
		alert("Please Select A Contact Preference.");
		form.contact[0].focus();
		return false;
	} else if ((formPhone == "") || (formPhone.length < 10)) {
		alert("Please Enter A Valid Phone Number.");
		form.phone.focus();
		return false;
	} else if((formEmail.length<6) || (formEmail.indexOf(",")>=0) || (formEmail.indexOf(";")>=0) || (formEmail.indexOf(":")>=0) || (formEmail.indexOf("/")>=0) || (formEmail.indexOf(" ")>=0) || (formEmail.indexOf("@")<=0) || (formEmail.indexOf("@") != formEmail.lastIndexOf("@")) || (formEmail.lastIndexOf(".")<formEmail.indexOf("@")) || ((formEmail.lastIndexOf(".")+3)>formEmail.length)) {
		alert("Please Enter A Valid Email Address.");
		form.email.focus();
		return false;
	} else if(formCountry == "") {
		alert("Please Select A Country.");
		form.country.focus();
		return false;
	} else if(formBackground0 == 0 && formBackground1 == 0 && formBackground2 == 0) {
		alert("Please Let Us Know Your Background.");
		form.background[0].focus();
		return false;
	} else if(formQuestion0 == 0 && formQuestion1 == 0 && formQuestion2 == 0 && formQuestion3 == 0 && formQuestion4 == 0) {
		alert("Please Let Us Know What Your Question Is About.");
		form.question[0].focus();
		return false;
	} else {
		form.action = "/_assets/_asp/submit.asp";
		/* form.submit(); */  /* removed because it was causing the form to email twice */
		return true;
	}
};
