﻿var CSD = {
    fontReplace: function () {
        var elements = $("h1, #csd_site_footer h3"); // .homeColumn h2 a, 
        elements.each(function (i) {
            var id = $(this).attr("id");
            var width = $(this).width();
            var height = $(this).height() + 2;
            var text = $(this).text();
            var size = $(this).css("font-size");
            var link = false;
            id = "fontReplace" + i;
            $(this).html("<span id='" + id + "'></span>");
            /*if(this.nodeName.toLowerCase().match(/^a$/)) {
            link = true;
            height += 7;
            }*/
            swfobject.embedSWF(skinPath + "text_replace.swf", id, width, height, "9.0.0", null, { text: text, fontSize: size, asLink: link }, { wmode: "transparent" });
        });
    },
    addAnchorLinks: function () {
        //Add the class "anchor" to a page element to create an anchor link on the contents pane
        if ($('div.ContentPane .anchor').length > 0) {
            $('div.ContentPane .anchor').each(function () {
                $(this).attr('id', $(this).text());
                $('span.shareLinks span.contents').append('<a href="#' + $(this).text() + '">' + $(this).text() + '</a>');
            });
            $('span.shareLinks span.contents').css('display', 'block');
        }
    },
    addImageCaptions: function () {
        //Add a rollover caption to images using the image_leftcol or image_rightcol container
        if ($('.captionImage').length > 0) {
            $('.captionImage').each(function () {
                if ($(this).find('img').attr('alt') != "") {
                    $(this).append('<div class="caption">' + $(this).find('img').attr('alt') + '</div>');
                    $(this).hover(function () {
                        $(this).children('.caption').stop().fadeTo(500, 1);
                    }, function () {
                        $(this).children('.caption').stop().fadeTo(500, 0);
                    });
                }
            });
        }
    },
    addMenuClasses: function () {
        //Add classes to the Superfish menu list items because superfish doesnt
        var menuClassName;
        $('ul.sf-menu > li > a').each(function (i) {
            menuClassName = "menu" + i;
            $(this).addClass(menuClassName);
        });
    },
    init: function () {
        CSD.fontReplace();
        CSD.addAnchorLinks();
        CSD.addImageCaptions();
        CSD.addMenuClasses();
    },
    Modules: {
        print: function (id) {
            var printTitle = $("#dnn_ctr" + id + "_dnnTITLE_lblTitle").html();
            var printBody = $("#dnn_ctr" + id + "_HtmlModule_lblContent").html();
            var headerImg = $("#dnn_csdHeader_dnnLOGO_hypLogo").html();

            var newwin = window.open('', 'printwin', 'left=100,top=100,width=400,height=400');
            newwin.document.write('<HTML>\n<HEAD>\n');
            newwin.document.write('<TITLE>' + printTitle + '</TITLE>\n');
            newwin.document.write('<script>\n');
            newwin.document.write('function chkstate(){\n');
            newwin.document.write('if(document.readyState=="complete"){\n');
            newwin.document.write('window.close()\n');
            newwin.document.write('}\n');
            newwin.document.write('else{\n');
            newwin.document.write('setTimeout("chkstate()",2000)\n');
            newwin.document.write('}\n');
            newwin.document.write('}\n');
            newwin.document.write('function print_win(){\n');
            newwin.document.write('window.print();\n');
            newwin.document.write('chkstate();\n');
            newwin.document.write('}\n');
            newwin.document.write('<\/script>\n');
            newwin.document.write('<style>.document{display:none;}</style>');
            newwin.document.write('</HEAD>\n');
            newwin.document.write('<BODY onload="print_win()">\n');
            newwin.document.write('<div style="text-align:center;">' + headerImg + '</div>');
            newwin.document.write('<H2>' + printTitle + '</H2>');
            newwin.document.write(printBody);
            newwin.document.write('</BODY>\n');
            newwin.document.write('</HTML>\n');
            newwin.document.close();
        }
    },
    Videos: {
        expand: function (id) {
            var idString = ".vc_" + id;
            var ui = $(idString);
            if (ui.find('.sizeToggle').attr('value') == "close") {
                ui.find('.sizeToggle').attr('value', 'open');
                if (ui.parent().attr('class') == 'HomeBannerVideoContainer') { //Open video, overlay over front page banner
                    ui.parent().css('display', 'block');
                }
                else if (ui.parent().hasClass('ModalVideoContainer')) { //Open video in modal
                    $.modal.close();
                    setTimeout(function () {
                        ui.find('.VideoWrapper').modal({
                            opacity: 60,
                            overlayCss: { backgroundColor: "#000" },
                            close: false
                        });
                    }, 100);
                }
                else { //Expand videos in footer pane to full column when they are played
                    ui.find('.VideoWrapper').css('border', '#333 20px solid').css('width', '928px').css('left', '-656px');
                    ui.find('.VideoWrapper object').attr('width', '715').attr('height', '402');
                    ui.find('.VideoWrapper .VideoInfo').css('display', 'block');
                }
            }
        },
        collapse: function (id) {
            var idString = ".vc_" + id;
            var ui = $(idString);
            if (ui.find('.sizeToggle').attr('value') == "open") {
                ui.find('.sizeToggle').attr('value', 'close');
                if (ui.parent().attr('class') == 'HomeBannerVideoContainer') {
                    ui.parent().css('display', 'none');
                }
                else if (ui.parent().hasClass('ModalVideoContainer')) { //Open video in modal
                    $.modal.close();
                }
                else {
                    ui.find('.VideoWrapper').css('border', '0').css('width', 'auto').css('left', '0');
                    ui.find('.VideoWrapper object').attr('width', '312').attr('height', '176');
                    ui.find('.VideoWrapper .VideoInfo').css('display', 'none');
                }
            }
        }
    }
};

$(document).ready(function () {
	CSD.init();
	//  Form input clearing
	inputClear('input.newsletter');
	
	$('#dnn_dnnBREADCRUMB_lblBreadCrumb a').last().css('color','#424242').css('text-decoration','none').css('cursor','text');
});

//  Clear inputs on focus
function inputClear(target) {
	var target = target || "input";
	$(target).each(function() {
		if($(this).attr("type") == "text" || $(this).attr("type") == "password") {
			var value = $(this).val();
			$(this).focus(function() {
				if($(this).val() == value) {
					$(this).val("");
				}
			});
			$(this).blur(function() {
				if($(this).val() == "") {
					$(this).val(value);
				}
			});
		}
	});
}
