/*

	Filename             sitewide.js
	Detail               Site functionality
	Author:              thunder::tech inc.
	License:             CLIENT is defined as the owner of online property from which this file resides or this code is referenced in.
						 ADDITIONAL PARTY is defined as anyone other than thunder::tech or CLIENT.
						 No right is granted to ADDITIONAL PARTY to sell, distribute, modify or otherwise transfer the following source code without explicit written permission by CLIENT or thunder::tech.

*/

/*  ================================
     Sitewide JavaScript
    ================================ */

thunder.client.project.pageLoaded = function() {
    //thunder.client.project.flashReplace();
    thunder.client.modify.rollImages();
    //thunder.client.modify.linkOptions();
    //thunder.client.modify.tabSet();
    thunder.client.project.customTreeMenu();
    thunder.client.project.feature.start();
    thunder.client.project.subfeature.start();
    thunder.client.modify.selfLabelFields();
    //thunder.client.modify.requireFields();
    thunder.client.workarounds.alphaImages();
    $('.nudge').mousemove(thunder.client.project.nudge);
    $('.location-field').focus(thunder.client.project.showLocations).blur(thunder.client.project.hideLocations);
    $('.location-hints').children().mouseenter(thunder.client.project.getLocation);
    $('.social').children('.alpha-mode').find('.mode-toggler').click(thunder.client.project.expandSocial);
    $('.social').children('.beta-mode').find('.mode-toggler').click(thunder.client.project.contractSocial);
    $('.fbLink').click(function() { window.open('http://www.facebook.com/pages/Bedford-Heights-OH/Winking-Lizard-Tavern/62196296205'); });
	$('.lizardville-accent').mouseenter(thunder.client.project.highlightLizardville).mouseleave(thunder.client.project.highlightLizardville);
}

thunder.client.project.highlightLizardville = function(evt) { $(this).stop(true, false).animate({left: evt.type == 'mouseenter' ? -160 : -140, top: evt.type == 'mouseenter' ? 280 : 290}, {duration: 300}); };
thunder.client.project.showLocations = function() { $('.location-hints').css('display', 'block'); thunder.client.project.scrollPastCurrent(100); }
thunder.client.project.hideLocations = function() { $('.location-hints').css('display', 'none'); }
thunder.client.project.getLocation = function() { $('.location-field').val(this.innerHTML); }
thunder.client.project.expandSocial = function() { $('.beta-mode').css('display', 'block'); $('.social').animate({height: 520}, {duration: 300, queue: false}); $('.social').children('.alpha-mode').animate({top: '-400px'}, {duration: 300, queue: false}); $('.social').children('.beta-mode').animate({top: '0px'}, {duration: 300, queue: false, complete: thunder.client.project.postExpandSocial}); };
thunder.client.project.postExpandSocial = function() { $('.alpha-mode').css('display', 'none'); thunder.client.project.scrollPastCurrent(100); }
thunder.client.project.contractSocial = function() { $('.alpha-mode').css('display', 'block'); $('.social').animate({height: 250}, {duration: 300, queue: false}); $('.social').children('.alpha-mode').animate({top: '0px'}, {duration: 300, queue: false}); $('.social').children('.beta-mode').animate({top: '-500px'}, {duration: 300, queue: false, complete: thunder.client.project.postContractSocial}); }
thunder.client.project.postContractSocial = function() { $('.beta-mode').css('display', 'none'); }
thunder.client.project.nudge = function(e) { $(this).animate({left: thunder.client.project.nudgeAmount(e, this, true), top: thunder.client.project.nudgeAmount(e, this, false)}, {duration: 100, queue: false}); }
thunder.client.project.nudgeAmount = function(e, f, g) { if(g==true) { g = (e.pageX - $(f).offset().left) < 25; } else {  g = (e.pageY - $(f).offset().top) < 25; } g = g ? '+=' : '-='; return g + '10px'; }
thunder.client.project.scrollPastCurrent = function(n) { $('html, body').animate({'scrollTop': $('body').height() + n}, {duration: 2000, queue: false}); }

thunder.client.project.feature = function()
{
	var _feature = {};
	_feature.counting = false;
	_feature.index = -1;
	
	_feature.direction = 1;
	_feature.precommence = function()
	{
		_feature.timeout = setTimeout(_feature.change, 6000);
		_feature.counting = true;
	}
	_feature.commence = function()
	{
		if(_feature.counting==true)
		{
			_feature.counting = false;
			clearTimeout(_feature.timeout);
		}
		_feature.change();
	}
	_feature.start = function()
	{
		_feature.images = $('.feature').children('.rotator').find('img');
		_feature.images.css('left', '-483px');
		if(_feature.images.length>0) { _feature.commence(); _feature.images[0].style.left = 0; }
		$('.feature').children('.left').click(_feature.previous);
		$('.feature').children('.right').click(_feature.next);
	}
	_feature.next = function() { _feature.direction = 1; _feature.commence(); }
	_feature.previous = function() { _feature.direction = -1; _feature.commence(); }
	_feature.change = function()
	{
		_feature.previndex = _feature.index;
		_feature.index += _feature.direction;
		if(_feature.index >= _feature.images.length) _feature.index = 0;
		if(_feature.index < 0) _feature.index = _feature.images.length - 1;
		if(_feature.previndex>=0)
		{
			if(_feature.direction>0)
				$(_feature.images[_feature.previndex]).animate({'left': -483}, {duration: 600, queue: false});
			else
				$(_feature.images[_feature.previndex]).animate({'left': 483}, {duration: 600, queue: false});
		}
		if(_feature.index>=0)
		{
			if(_feature.direction>0)
				_feature.images[_feature.index].style.left = '483px';
			else
				_feature.images[_feature.index].style.left = '-483px';
			$(_feature.images[_feature.index]).animate({'left': 0}, {duration: 300, queue: false});
		}
		_feature.precommence();
	}
	return _feature;
}();

thunder.client.project.subfeature = function()
{
	var _feature = {};
	_feature.counting = false;
	_feature.index = -1;
	_feature.slot = -1;
	_feature.prevslots = [];
	_feature.prevdirection = 1;
	_feature.direction = 1;
	_feature.initialized = false;
	_feature.start = function()
	{
		_feature.images = $('.subfeatures').children('.rotator').find('img');
		_feature.images.css('left', '-186px');
		if(_feature.images.length>0) { _feature.change(); _feature.images[0].style.left = 0; }
		$('.subfeatures').children('.left').click(_feature.previous);
		$('.subfeatures').children('.right').click(_feature.next);
	}
	_feature.next = function() { _feature.direction = 1; _feature.change(); }
	_feature.previous = function() { _feature.direction = -1; _feature.change(); }
	_feature.change = function()
	{
		var i, ii;
		if(_feature.initialized == false)
		{
			_feature.prevslots[0] = _feature.images[_feature.images.length - 4];
			_feature.prevslots[1] = _feature.images[_feature.images.length - 3];
			_feature.prevslots[2] = _feature.images[_feature.images.length - 2];
			_feature.prevslots[3] = _feature.images[_feature.images.length - 1];
	
			for(i = 1; i< _feature.prevslots.length; i++)
			{
				$(_feature.prevslots[_feature.slot]).css('z-index', '5000');
				_feature.prevslots[i].style.left = (i * 190) + 'px';
			}
			_feature.initialized = true;
		}
		_feature.previndex = _feature.index;
		_feature.index += _feature.direction;
		_feature.slot += _feature.direction;
		_feature.slot = _feature.slotWrap(_feature.slot);
		_feature.index = _feature.indexWrap(_feature.index);
//		if(_feature.prevdirection == _feature.direction)
//		{
			
			if(_feature.previndex>=0)
			{
				$(_feature.prevslots[_feature.slot]).css('z-index', '4000');
			}
			if(_feature.index>=0)
			{
				if(_feature.direction>0)
					_feature.images[_feature.index].style.left = '760px';
				else
					_feature.images[_feature.index].style.left = '-186px';
					$(_feature.images[_feature.index]).css('z-index', '60000');
				$(_feature.images[_feature.index]).animate({'left': (190 * _feature.slot)}, {duration: 300, queue: false, complete: function() { $(_feature.prevslots[_feature.slot]).css('z-index', '5000'); } });
			}
			_feature.prevslots[_feature.slot] = _feature.images[_feature.index];
			//$(_feature.images[_feature.index]).css('z-index', '60000');
/*		}
		else
		{
			for(i=0; i<_feature.images.length; i++)
			{
				ii = _feature.getSlotOf(_feature.images[i]);
				if((ii>=0)&&(ii<=3))
				{
					$(_feature.images[i]).animate({'left': (190 * ii)}, {duration: 300, queue: false, complete: function() { $(_feature.prevslots[_feature.slot]).css('z-index', '5000'); } });
					$(_feature.images[i]).css('z-index', '6000');
				}
				else
				{
					$(_feature.images[i]).css('z-index', '4000');
				}
			}
			_feature.prevdirection = _feature.direction;
		}*/
	}
	_feature.slotWrap = function(n)
	{
		if(n >= 4) return 1;
		else if(n < 1) return 3;
		else return n;
	}
	_feature.indexWrap = function(n)
	{
		if(n >= _feature.images.length) return 1;
		else if(n < 0) return _feature.images.length - 1;
		else return n;
	}
	return _feature;
}();

thunder.client.project.flashReplace = function()
{
	$('.flash-billboard').flash(
		{
			swf: 'flash/billboard.swf',
			height: 200,
			width: 800,
			allowFullScreen: true,
			allowScriptAccess: 'always',
			hasVersion: 9,
			wmode: 'transparent',
			flashvars: {
				basePath: ''
			}
		}
	);
}

thunder.client.project.customTreeMenu = function()
{
	$('.menu').find('.nav').mouseenter(function()
	{
		$(this).children('.spinner').find('img').animate({top: '-68px'}, {duration: 300, queue: false});
		$(this).children('.beta-spinner').animate({height: (($(this).children('.beta-spinner').find('.double').length * 17) + ($(this).children('.beta-spinner').find('a').length * 25))+'px'}, {duration: 300, queue: false});
	}).mouseleave(function()
	{
		$(this).children('.spinner').find('img').animate({top: '0px'}, {duration: 300, queue: false});
		$(this).children('.beta-spinner').animate({height: '0px'}, {duration: 300, queue: false});
	});
};

$(thunder.client.project.pageLoaded);

$(document).ready(function() {
    $("#locateStores").click(function() {
        var locateZip = $("#locateZip").val();
        if (locateZip.match(/^\d{5}/)) {
            window.location = '/plugins/storelocator/default.aspx?zip=' + locateZip;
        }
        else { alert('Invalid zip code.\n\nPlease enter a valid 5-digit zip code.'); }
        return false;
    });

    $("#newsletterSubmitBtn").click(function() {
        var name = $("#newsletterName").val();
        var location = $("#newsletterLocation").val();
        var email = $("#newsletterEmail").val();


        processNewsletterSubmission(name, location, email);

        return false;
    });

    $("#newsletterSubmitBtn2").click(function() {
        var name = $("#newsletterName2").val();
        var location = $("#newsletterLocation2").val();
        var email = $("#newsletterEmail2").val();

        processNewsletterSubmission(name, location, email);

        return false;
    });
});

var eNewJBox;

function processNewsletterSubmission(n, l, e) {    
    if (n == 'name' || l == 'choose location' || e == 'email address') {
        showNewsletterMessage("Invalid Information!<br/><br/>Please enter your name, email address and your home location.", true);
        return; 
    }


    showNewsletterMessage("Processing...", false);


    var mData = {"action":"submitNewsletterSignUp", "name":n, "location":l , "email": e};
    $.ajax({
        type: "POST",
        url: "/themes/main/services/sitehandler.ashx",
        data: mData,
        dataType: "json",
        success: function(response) {
            if (response.statusCode == '200') {
                //we are good
                showNewsletterMessage("Congratulations! <br/><br/>You are now registered to receive our e-newsletter!" + response.response, true);
            }
            else {
                //something bad happened
                showNewsletterMessage("Better luck next time! <br/><br/>We were unable to successfully submit your request to join our mailing list! <br/><br/>Please try again soon.", true);
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            //if error, will just show the default test data
        showNewsletterMessage("Better luck next time! <br/><br/>We were unable to successfully submit your request to join our mailing list! <br/><br/>Please try again soon.", true);
        }
    });
}


function showNewsletterMessage(message, showClose) {
    if (eNewJBox) eNewJBox.close();
    eNewJBox = $(this).jBox({ 'fadeInDelay': 150, 'fadeOutDelay': 250, 'clickToClose': false, 'showClose': showClose, 'autoHide': 0}).showWindow('<div style="margin: 10px; text-align: center;"><strong>Newsletter Sign Up</strong><br/><br/>' + message + '</div>');    
}


