NYWine = function() {
    var contMask;
    
	return {
        ajaxType: 1, // Sub navigation: 0 -> No Ajax, 1 -> In Modal Popup
        
        imgRoot: '',
        
        getViewHeight: function() {
            if(Prototype.Browser.IE && !Prototype.Browser.IE7)
            {
                return (document.viewport.getDimensions().height - 30);
            }
            else
            {
                return (document.viewport.getDimensions().height - 30);
            }
        },
        
		init : function() {

			// set IE6 class
			if (Prototype.Browser.IE && !Prototype.Browser.IE7 && !Prototype.Browser.IE8) {
				$('body').addClassName('IE6');
			}
            
            if (Prototype.Browser.IE && Prototype.Browser.IE7 && !Prototype.Browser.IE8) {
				$('body').addClassName('IE7');
			}
			
			if (Prototype.Browser.Opera) {
				$('body').addClassName('Opera');
			}
			
			Ajax.Responders.register( {
				onCreate : function() {

				},
				onComplete : function() {
					Behaviour.apply();
					setTimeout(function() {
					        Behaviour.apply();
					    }, 1);
				}
			});
			
			contMask = new EBase.Controls.Mask( {
				parent : 'content',
				msg : 'Loading...'
			});

			NYWine.initMenu();

			var myrules = {
				'div.white-round-portlet' : function(element) {
					NYWine.makeRound(element, 'white-curve-portlet', 'white-round-portlet');
				},
				
				'div.color-round-portlet' : function(element) {
					NYWine.makeRound(element, 'color-curve-portlet', 'color-round-portlet');
				},
				
				'div.sub-round-portlet' : function(element) {
					NYWine.makeRound(element, 'sub-curve-portlet', 'sub-round-portlet');
				},

				'ul#top-sub-nav' : function(element) {
				    $(element).cleanWhitespace();
                    if(NYWine.ajaxType > 0)
                    {
					    $(element).getElementsBySelector('a').each(function(aEle) {
					        if($(aEle).readAttribute('rel').strip() != "")
					        {
						        Event.stopObserving(aEle, 'click', NYWine.subNavClick);
						        Event.observe(aEle, 'click', NYWine.subNavClick);
						    }
					    });
					}
				}
			};

			Behaviour.register(myrules);
			
			var mbOptions = {
			    overlayClose: false,
			    method: 'post',
			    autoFocusing: false			    
			};
			
			Modalbox.setOptions(mbOptions);
		},

		makeRound : function(element, outCls, removeCls) {
			var div = new Element('div', {
				'class' : outCls
			});
			var div1 = new Element('div', {
				'class' : 'mc'
			});
			var div2 = new Element('div', {
				'class' : 'mr'
			});
			var div3 = new Element('div', {
				'class' : 'ml'
			});

			var top = '<div class="tl"><div class="tr"><div class="tc"></div></div></div>';
			var bottom = '<div class="bl"><div class="br"><div class="bc"></div></div></div>';

			$(element).removeClassName(removeCls);
			Element.wrap(element, div);
			Element.insert(element, {
				before : top
			});
			Element.insert(element, {
				after : bottom
			});

			div1 = Element.wrap(element, div1);
			div2 = Element.wrap(div1, div2);
			div3 = Element.wrap(div2, div3);
		},

		initMenu : function() {
			new EBase.Controls.Menu('navlnk-wine-country', 'nav-wine-country');
			new EBase.Controls.Menu('navlnk-info-station', 'nav-info-station');
			new EBase.Controls.Menu('navlnk-wine-locator', 'nav-wine-locator');
			//new EBase.Controls.Menu('navlnk-ny-gold', 'nav-ny-gold');
			new EBase.Controls.Menu('navlnk-passport', 'nav-passport');			
		},

		subNavClick : function(evt) {
			Event.stop(evt);
			
			var aEle = Event.element(evt);
			
			if (NYWine.ajaxType == 1)
			{
			    NYWine.processAjaxRequest(aEle, true);
			}
			else if(NYWine.ajaxType == 2)
			{
			    if(!$(aEle).hasClassName('selected'))
		        {
		            contMask.show();
        		    
		            NYWine.clearNavSel();
        			
		            $(aEle).addClassName('selected');

		            NYWine.processAjaxRequest(aEle, false);
		        }
			}
			
		},

		clearNavSel : function() {

			$('top-sub-nav').getElementsBySelector('a').each(function(aEle) {
				$(aEle).removeClassName('selected');
			});
		},

		processAjaxRequest : function(aEle, popup) {

			if ($(aEle).href != '#') {
			    if(popup == false)
			    {
			        new Ajax.Updater('content', $(aEle).href, {
				        method : 'post',
				        parameters : {
					        dt : new Date()
				        },
				        evalScripts : true,
				        onComplete: function() {
				            contMask.hide();
				        }
			        });
			    }
			    else
			    {
			        Modalbox.show($(aEle).href, 
			                        { 
			                            title: $(aEle).rel, 
			                            width: 974,
			                            height: (NYWine.getViewHeight()),
			                            params: { dt: new Date() }
			                        });
			    }
			}
		},
		
		showCntMask: function() {
		    contMask.show();
		},
		
		hideCntMask: function() {
		    contMask.hide();
		},
		
		isDigit : function(ch) {

			return ((ch >= "0") && (ch <= "9"));
		},

		isInteger : function(str) {
			var ii;

			for (ii = 0;ii < str.length; ii++) {
				var c = str.charAt(ii);
				if (!NYWine.isDigit(c))
					return false;
			}
			return true;
		},
		
		isEmail : function(str) {
//            if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str.strip()) == false)
			if(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test(str.strip()) == false)
            {   
	            return false;
            }
            
            return true;  
		}
	};
}();

GotoPagePopup = function() {
	var listMask;

	return {
		divPopup : '',

		updateDiv : '',

		updateUrl : '',

		init : function() {

			var pageRules = {
				'span.pgEllipse' : function(element) {
					$(element).getElementsBySelector('a').each(function(aEle) {
						Event.stopObserving(aEle, 'click', GotoPagePopup.open);
						Event.observe(aEle, 'click', GotoPagePopup.open);
					});
				},

				'div.pagination' : function(element) {
					$(element).getElementsBySelector('a').each(function(aEle) {
						if ($(aEle).hasClassName('aEllipse') == false) {
							Event.stopObserving(aEle, 'click', GotoPagePopup.getPage);
							Event.observe(aEle, 'click', GotoPagePopup.getPage);
						}
					});
				}
			};

			Behaviour.register(pageRules);

			/*if ($('div-goto-page')) {
				GotoPagePopup.divPopup = $('div-goto-page');

				
			}*/
		},
		
		build: function() {
		    var div = new Element('div', { id: 'div-goto-page' });
		    
		    var html = '<label>Page:&nbsp;</label>' +
                       '<input type="text" class="goto" id="txtGP" maxlength="3" />' +
                       '<a href="#" id="lnkGP"><img src="' + NYWine.imgRoot +'go.gif" alt="Go" title="Go" /></a>' +
                       '&nbsp;<a href="#" id="lnkGPClose"><img src="' + NYWine.imgRoot + 'close.gif" alt="Close" title="Close" /></a>';
                       
		    $(div).update(html);
		    
		    $(div).setStyle({
		        display: 'none'
		    });
		    
		    Element.insert($('body'), {
				top : div
			});
			
			Event.stopObserving($('lnkGP'), 'click', GotoPagePopup.go);
			Event.stopObserving($('lnkGPClose'), 'click', GotoPagePopup.close);
			
			if(Prototype.Browser.IE)
			{
			    Event.stopObserving($('txtGP'), 'keydown', GotoPagePopup.kbdObserver);
			    Event.observe($('txtGP'), "keydown", GotoPagePopup.kbdObserver);
			}
		    else
		    {
		        Event.stopObserving($('txtGP'), 'keypress', GotoPagePopup.kbdObserver);
			    Event.observe($('txtGP'), "keypress", GotoPagePopup.kbdObserver);
			}
			
			Event.observe($('lnkGP'), 'click', GotoPagePopup.go);
			Event.observe($('lnkGPClose'), 'click', GotoPagePopup.close);
				
			return $(div);
		},
		
		kbdObserver: function(evt) {
		    switch(evt.keyCode) {
			    case Event.KEY_TAB:
			        Event.stop(evt);
			        $('lnkGP').focus();
			        break;	
			    case Event.KEY_ESC:
			        GotoPagePopup.closePop();			        
			        break;		    
			}
		},

		open : function(evt) {
			Event.stop(evt);
			
			GotoPagePopup.closePop();

			var dd = GotoPagePopup.build();

			if (dd) {
				dd.hide();

				var ele = Event.element(evt);

				GotoPagePopup.updateUrl = $(ele).rel;
				$('txtGP').value = '';
				GotoPagePopup.updateDiv = $(ele).up('.paged-list-wrap');

				var left = $(ele).cumulativeOffset().left;
				var top = $(ele).cumulativeOffset().top;

				dd.setStyle( {
					left : (left) + 'px',
					top : (top + 22) + 'px'
				});

				Effect.toggle(dd, 'blind', {
					afterFinish : function() {
						$('txtGP').focus();
					}
				});
			}

			return false;
		},
        
        closePop: function() {
            if ($('div-goto-page')) {
				$('div-goto-page').remove();
		    }
        },
        
		close : function(evt) {
			Event.stop(evt);

			GotoPagePopup.closePop();
			return false;
		},

		go : function(evt) {
			Event.stop(evt);

			if (listMask == null) {
				listMask = new EBase.Controls.Mask( {
					parent : GotoPagePopup.updateDiv,
					msg : 'Loading...'
				});
			}

			listMask.show();

			if ($('txtGP').value.strip() == '') {
				return;
			} else if (NYWine.isInteger($('txtGP').value.strip()) == false) {
				return false;
			}
			
			var ele = Event.element(evt);
            
			new Ajax.Updater(GotoPagePopup.updateDiv, GotoPagePopup.updateUrl, {
				method : 'post',
				parameters : {
					dt : new Date(),
					p : $('txtGP').value
				},
				evalScripts : true,
				onComplete : function() {
					listMask.hide();
				}
			});
			
			GotoPagePopup.closePop();
		},

		getPage : function(evt) {
			Event.stop(evt);

			aEle = Event.element(evt);

			if (listMask == null) {
				listMask = new EBase.Controls.Mask( {
					parent : $(aEle).up('.paged-list-wrap'),
					msg : 'Loading...'
				});
			}
			listMask.show();

			if ($(aEle).href != '#') {
				new Ajax.Updater($(aEle).up('.paged-list-wrap'), $(aEle).href, {
					method : 'post',
					parameters : {
						dt : new Date()
					},
					evalScripts : true,
					onComplete : function() {
						listMask.hide();
					}
				});
			}
		}
	};
}();


document.observe("dom:loaded", function() {
	NYWine.init();
	GotoPagePopup.init();

	Behaviour.apply();
});
