﻿$(document).ready(function() {
    loadHero();
    loadWork("MOSTRECENT");
    loadNewsAndIdeas("MOSTRECENT");

    $('#solutions').change(function() {
        loadWork($(this).val());
    });

    $('#news_categories').change(function() {
        loadNewsAndIdeas($(this).val());
    });
});

function listFadeIn(target, items) {
    $(target).html($(items).hide());
    for (var i = 0; i < items.length; i++) { $(items[i]).delay(200 * i); $(items[i]).fadeIn(400); }
}

function loadHero(){
    var swfVersionStr = "9.0.124";
    var xiSwfUrlStr = "App_Themes/AcquityCorporate/flash/playerProductInstall.swf";

	var flashvars = {
	    configPath: "assets/data/config.aspx"
	};
	
	var params = {
		quality: "high",
		bgcolor: "#ffffff",
		allowscriptaccess: "sameDomain",
		allowfullscreen: "true",
		wmode: "transparent"
	};
	
	var attributes = {
		id: "HomePageSlideShow",
		name: "HomePageSlideShow",
		align: "middle"
	};
	
	swfobject.embedSWF(
		"App_Themes/AcquityCorporate/flash/HomePageSlideShow.swf", "flashContent", 
		"960", "400", 
		swfVersionStr, xiSwfUrlStr, 
		flashvars, params, attributes);
}

function loadNewsAndIdeas(filter) {
    var path = "";
    var className = "";
    var orderBy = "";
    var where = "";

    if (filter.toString().toUpperCase() == "EVENTS") {
        var now = new Date();
        
        path = "/news-and-ideas/Events/%";
        className = "agcorporate.event_module";
        orderBy = "PublishDate";
        where = escape("PublishDate > '" + (now.getMonth() + 1) + "-" + (now.getDate() - 1) + "-" + now.getFullYear() + "'");
    }
    else if (filter.toString().toUpperCase() == "NEWS") {
        path = "/news-and-ideas/News/%";
        className = "agcorporate.news_module";
        orderBy = "PublishDate DESC";
    }
    else if (filter.toString().toUpperCase() == "WHITEPAPERS") {
        path = "/news-and-ideas/WhitePapers/%";
        className = "agcorporate.white_papers_module";
        orderBy = "PublishDate DESC";
    }

    if (path == "") {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: $("#siteBasePath").val() + "/acquityservices.asmx/GetMostRecent",
            data: "",
            dataType: "json",
            success: function(msg) {
                listFadeIn('#news_and_ideas', $(msg.d).filter('li'));
            },
            error: function(msg) {
                errorDialog('#news_and_ideas');
            },
            beforeSend: function(xhr) {
                loadingDialog('#news_and_ideas');
            }
        });
    }
    else {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: $("#siteBasePath").val() + "/acquityservices.asmx/GetModules",
            data: "{\
                    'path': '" + path + "',\
                    'moduleClass': '" + className + "',\
                    'transformation': 'Preview',\
                    'count': '3',\
                    'where': '" + where + "',\
                    'orderBy': '" + orderBy + "',\
                    'noColumns': '3'\
                }",
            dataType: "json",
            success: function(msg) {
                listFadeIn('#news_and_ideas', $(msg.d).filter('li'));
            },
            error: function(msg) {
                errorDialog('#news_and_ideas');
            },
            beforeSend:function(xhr){
                loadingDialog('#news_and_ideas');
            }
        });
    }
}

function loadWork(filter) {
    if (filter.toString().toUpperCase() == "MOSTRECENT") {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: $("#siteBasePath").val() + "/acquityservices.asmx/GetCaseStudyModules",
            data: "{ 'pageSize': '3' }",
            dataType: "json",
            success: function(msg) {
                listFadeIn('#case_studies', $(msg.d).filter('li'));
            },
            error: function(msg) {
                errorDialog('#case_studies');
            },
            beforeSend:function(xhr){
                loadingDialog('#case_studies');
            }
        });
    }
    else {
        var where = escape("SolutionTypes LIKE '%" + filter + "%'");

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: $("#siteBasePath").val() + "/acquityservices.asmx/FilterCaseStudiesBySolution",
            data: "{\
                    'path': '/work/%',\
                    'moduleClass': 'agcorporate.casestudy_module',\
                    'transformation': 'Preview',\
                    'count': '3',\
                    'where': '" + where + "',\
                    'orderBy': 'PublishDate DESC',\
                    'noColumns': '3'\
                }",
            dataType: "json",
            success: function(msg) {
                listFadeIn('#case_studies', $(msg.d).filter('li'));
            },
            error: function(msg) {
                errorDialog('#case_studies');
            },
            beforeSend: function(xhr) {
                loadingDialog('#case_studies');
            }
        });
    }
}