﻿var CHERRY = {
	Globals: {
		menuHideDelaySeconds: 2,
		timeout: null,
		doFancyLetter: true,
		showHero: false,
		jsReady: false,
		swfReady: false
	},

	get_Timeout: function get_Timeout() {
		return CHERRY.Globals.timeout;
	},

	firstLetterFormat: function(paragraph) {
		var node = paragraph;
		while (node.childNodes.length) {
			node = node.firstChild;
		}
		if (node.nodeValue != null) {
			var text = $.trim(node.nodeValue);
			var first_letter = text.substr(0, 1);
			var match = /[a-zA-Z]/.test(first_letter);
			if (match) {
				node.nodeValue = text.slice(1);
				$('<img />')
						    .attr('src', '/_images/letters/' + first_letter.toLowerCase() + '.png')
						    .attr('alt', first_letter)
						    .addClass('fancy-letter')
						    .prependTo(paragraph);
				$('<div class="clear" />').appendTo(paragraph);
			}
		}
	},

	showNav: function(menu) {
		$(menu)
            .show('fast')
            .siblings('div').hide('fast');
		var secondaryNav = $("#secondaryNav");
		if (!secondaryNav.visible)
			secondaryNav.slideDown("normal");
	},

	queueHideNav: function(timeoutId) {
		var o = this;
		if (timeoutId != null)
			clearTimeout(timeoutId);
		CHERRY.Globals.timeout = setTimeout(function() {
			if (!$("#secondaryNav").data('show')) {
				o.doNavHide();
			}
		}, CHERRY.Globals.menuHideDelaySeconds * 1000);
	},

	doNavHide: function() {
		$("#secondaryNav")
        .slideUp("normal")
        .find('div').hide('fast');
	},

	embedFlash: function(showHero) {
		var flashvars = {
			showHero: showHero
		};
		var params = {
			menu: "false",
			scale: "noScale",
			wmode: "transparent"
		};
		swfobject.embedSWF("/swf/cherryAmerica.swf", "flashHeader", "984px", "284px", "9.0.0", "swf/expressInstall.swf",
			flashvars, params);
	},


	// ------- functions called by ActionScript -------

	// called to check if the page has initialized and JavaScript is available
	isReady: function isReady() {
		return CHERRY.Globals.jsReady;
	},

	// called to notify the page that the SWF has set it's callbacks
	setSWFIsReady: function setSWFIsReady() {
		// record that the SWF has registered it's functions (i.e. that JavaScript
		// can safely call the ActionScript functions)
		CHERRY.Globals.swfReady = true;
	}
}

$(function() {
	/*secondary nav*/
	var $secondaryNav = $("#secondaryNav");
	$('#siteNav a')
        .click(function(e) {
        	e.preventDefault();
        })
        .hover(
            function(e) {
            	$secondaryNav.data('show', true);
            	CHERRY.showNav(this.hash);
            },
            function(e) {
            	$secondaryNav.data('show', false);
            	CHERRY.queueHideNav(CHERRY.Globals.timeout);
            }
        )

	$secondaryNav.hover(
        function(e) {
        	$(this).data('show', true);
        },
        function(e) {
        	$(this).data('show', false);
        	CHERRY.queueHideNav(CHERRY.Globals.timeout);
        }
    )

	$('#siteNav a, #footer h4 a').text('');
	$('#hiddenResources img')
        .filter('.siteNavImg')
            .each(function(i, el) {
            	$('#nav' + $(this).attr('name').substring(3) + ' a').append(this);
            })
        .end()
        .filter('.footerNavImg')
            .each(function(i, el) {
            	$('#nav' + $(this).attr('name').substring(3) + ' h4 a').append(this);
            })

	$("#secondaryNav ul > li:first-child").css("border-left", "none");

	/*image replacement for first letter in paragraph*/
	var first_paragraph = $("#mainColumn p")[0];
	if (first_paragraph && CHERRY.Globals.doFancyLetter)
		CHERRY.firstLetterFormat(first_paragraph);

	/*flash text replace*/
	$('h1').flash(
        {
        	src: '/swf/TextReplace.swf',
        	flashvars: {
        		css: [
                    '* { color: #4C1B1B; }',
                    'a { font-size:28px; text-align:center; width:227px; }'
                ].join(' ')
        	},
        	wmode: "transparent"
        },
        { version: 7 },
        function(htmlOptions) {
        	htmlOptions.flashvars.txt = this.innerHTML;
        	this.innerHTML = '<div>' + this.innerHTML + '</div>';
        	var $alt = $(this.firstChild);
        	htmlOptions.height = '40px';
        	htmlOptions.width = $alt.width();
        	$alt.addClass('alt');
        	$(this)
                .addClass('flash-replaced')
                .prepend($.fn.flash.transform(htmlOptions));

        }
    );

	//side nav (variety etc.)
	$('ul.sideNav a').click(function(e) {
		e.preventDefault();
		$(this.hash).removeClass('hiddenVariety').addClass('visibleVariety')
            .siblings('.visibleVariety')
                .removeClass('visibleVariety').addClass('hiddenVariety');
		$(this).parent('li').addClass('selected').siblings().removeClass('selected');
	});

	// record that JavaScript is ready to go, polled by flash.
	CHERRY.Globals.jsReady = true;
	
	// embed the flash
	CHERRY.embedFlash(CHERRY.Globals.showHero);
});