$(document).ready(function (){
    /*
     * Rule: IDs are UpperCamelCase (e.g. <div id="LongDescription">Hello</div>
     * Rule: input names are underscore (e.g. <input name="first_name" />)
     * Rule: CSS Framework foundation is dashed based (e.g. bgColor-blue)
     * Rule: CSS Theme/Styles should be camelCase (e.g. <span class="firstName">Sam</span>)
     */
    
    //if we forgot to remove console.log anywhere, then create it for non-firefox
    if (typeof console === "undefined") {
        console = { log: function(){} };
    }
   
    pages = {
       
        /**
         * @site Code Inside Executes in ALL Pages
         */
        common : {
            init : function (detailed) {
                //common code on all pages goes here
				/*var $callToAction = $('.btn-callToAction');
				if ($.browser.msie || $.browser.opera ) {
					$callToAction.css('position','relative');
					$callToAction.append('<span class="tl"></span>');
					$callToAction.append('<span class="tr"></span>');
					$callToAction.append('<span class="bl"></span>');
					$callToAction.append('<span class="br"></span>');
				}*/
            },
            finalize : function (){
                //executes at the very end
            }
        },
		
       
        /**
         * @page Innkeeper's Sell Page
         */
        Page_Innkeeper  : {
            init : function(){
                var $SellPageSlideShow = $('#SellPageSlideShow');
				
				$SellPageSlideShow.find('.menu').delegate('li', 'click', function(e){
					e.preventDefault();
					var btn, slide;
					
					btn    =  '#'+ $(this).attr('id');
					slide  =  btn.replace('btn','');
					
					$SellPageSlideShow.find('.menu li').removeClass('selected');
					
					$(btn).addClass('selected');
					
					$(slide).show();
					$(slide).siblings('.slide').hide();
				});
            }
        },
		

       
        /**
         * @page Innkeeper's Sell Page
         */
        Page_Alerts  : {
            init : function(){

				function validateForm() {
					var errorCount = 0;
					alert('efef');
					if (document.getElementById("name").value == "" || /^\s+$/.test(document.getElementById("name").value)) {
						document.getElementById("nameInfo").style.color='red';
						errorCount++;
					}
					if (!/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/.test(document.getElementById("email").value)) {
						document.getElementById("emailInfo").style.color='red';
						errorCount++;
					}
					if (document.getElementById("affiliations").value == "" || /^\s+$/.test(document.getElementById("affiliations").value)) {
						document.getElementById("mediaInfo").style.color='red';
						errorCount++;
					}
					if (errorCount > 0) {
						document.getElementById("info").style.color='red';
					}else {
						document.myForm.submit();
					}
				}
				
				$('#SubmitBtn').click(function(){
					validateForm();
				});
				
            }
        },
		
        /**
         * @page Sample Template to Use in Future Pages.
         */
        Page_Mailto  : {
            init : function(){
				function verifyForm(verify) {
					if (verify != 'none') {
						if (document.getElementById(verify).value=='') {
							alert('Please fill in all required fields.');
						}else {
							document.myForm.submit();
						}
					}else {
						document.myForm.submit();
					}
				}
            }
        },
		
        /**
         * @page Sample Template to Use in Future Pages.
         */
        Page_InnSignup  : {
            init : function(){
				
				$('#NeedHelpBtn').click(function(e){
					e.preventDefault();
					$('#NeedHelp').slideToggle();
				});
				
            }
        },
		
        /**
         * @page Sample Template to Use in Future Pages.
         */
        Page_PricingOptions  : {
            init : function(){
				var $pricingTable = $('.pricingTable');
                $pricingTable.tableHover({
					colClass    :  'highlight', 
					rowClass    :  'highlight-blue', 
					clickClass  :  'highlight', 
					headRows    :  true,  
                    footRows    :  true, 
					headCols    :  true, 
					footCols    :  true
				});
            }
        },
		
        /**
         * @page Canadian Pricing
         */
        Page_PricingOptionsCA  : {
            init : function(){
				$('.table_pricing').tableHover({colClass: 'highlight', rowClass: 'highlight-blue', clickClass: 'highlight', headRows: true,  
                    footRows: true, headCols: true, footCols: true});
            }
        },
		
        /**
         * @page Sample Template to Use in Future Pages.
         */
        Page_MyInns  : {
            init : function(){
				var $tab, $innCount, $compareInns, $deleteInns, $emailInns, queueInn;
				
				$tab          =  $('input[name="tab"]');
				$innCount     =  $('input[name="innCount"]');
				$compareInns  =  $('#CompareInns');
				$deleteInns   =  $('#DeleteInns');
				$emailInns    =  $('#EmailInns');
				$queueInn     =  $('.QueueInn');
				
				innCount  =  $innCount.val();
				currIds   =  new Array();
				limit     =  4;
				
				$('#tabs').tabs();
				
				function showHideContent(id) {
					var divs = document.getElementsByTagName("div");
					for(var i=0;i<divs.length;i++) {
				  		if(divs[i].getAttribute('name')=='content') {
							var ids= divs[i].getAttribute('id');
							document.getElementById(ids).style.height=1;
							document.getElementById(ids).style.visibility='hidden';
				  		}
					}
					document.getElementById(id).style.height='auto';
					document.getElementById(id).style.visibility='visible';
				}
				
				function makeWindow(URL) {
					popX=450
					popY=450
					posX=((screen.availWidth/2)-(popX/2))
					posY=((screen.availHeight/2)-(popX/2))
					var theWindow = window.open(URL,"largeImage","resizable=yes,height=450,width=450,toolbar=0,menubar=0,directories=0,scrollbars=0,left="+posX+",top="+posY);
					theWindow.focus();
				}

				function queueInn(innId) {
				
					innArray = false;
				
					for(i=0; i<currIds.length; i++) {
					    if (currIds[i] == innId) {
						    innArray = true;
					    }
					}
				
					if ((eval('document.myInnForm.inn'+innId + '.value') == '' || !innArray) && currIds.length != limit) {
				  		eval('document.myInnForm.inn'+innId + '.value="checked"');
				  		eval('document.myInnForm.checkbox' + innId + '.src="/images/checkboxCheckedPurple.gif"');
				  		currIds[currIds.length] = innId;
					} else if (eval('document.myInnForm.inn'+innId + '.value') == 'checked') {
				  		eval('document.myInnForm.inn'+innId + '.value=""');
				  		eval('document.myInnForm.checkbox' + innId + '.src="/images/checkboxPurple.gif"');
				
				  		for(i=0; i<currIds.length; i++) {
							if (currIds[i] == innId) {
					  			currIds.splice(i,1);
							}
				  		}
					} else if (currIds.length == limit) {
						alert("A limit of " + limit + " inns can be choosen at one time.");
					}			
				}
				
				function compareInns() {
					if (currIds.length > 1) {
				  		document.myInnForm.innIds.value = currIds;
				  		document.myInnForm.action.value = 'myInnCompare';
				  		document.myInnForm.submit();
					} else {
				  		alert("Please choose at least\ntwo inns to compare.");
					}
				}
				
				function emailInns() {
					if (currIds.length > 0) {
				  		document.myInnForm.innIds.value = currIds;
				  		document.myInnForm.action.value = 'myInnEmail';
				  		document.myInnForm.submit();
					} else {
				  		alert("Please choose at least\none inn to email.");
					}
				}
				
				function deleteInns() {
					if (currIds.length > 0) {
				  		document.myInnForm.innIds.value = currIds;
				  		document.myInnForm.action.value = 'myInnsDeleteView';
				  		document.myInnForm.submit();
					} else {
				  		alert("Please choose at least\none inn to delete.");
					}
				}
				
				$compareInns.click(function(e){
					e.preventDefault();
					compareInns();
				});
				
				$deleteInns.click(function(e){
					e.preventDefault();
					deleteInns();
				});
				
				$emailInns.click(function(e){
					e.preventDefault();
					emailInns();
				});
				
				$queueInn.click(function(e){
					e.preventDefault();
					queueInn( $(this).attr('title') );
				});
				
				if($tab.val()!='') {
					showHideContent($tab.val());
				}

            }
        },

       
        /**
         * @page Sample Template to Use in Future Pages.
         */
        Page_Template  : {
            init : function(){
                //page named template goes here
               
                var myVar1, myVar2, myVar3, myComponent, $myElement; //all definitions in one line
               
                myVar1      =  'Hello World';    // variables are ALL camelCase
                myVar2      =  2;                //
                myVar3      =  'Long String';    // if more than 2 variables use double spaces for legibility
                $myElement  =  $('#myElement');  // store jQuery selectors in variables starting with $ for faster readibility. ( a bit ugly but it helps development )
               
                function myFunction() {
                    // functions go second
                    if(myVar1 == myVar3){
                        // no space between "if" and "("
                        // one space on both sides of operator
                    }
                }
               
                myComponent = function() {
                    // wrap components into functions so it is easier to manage
                }
               
                myElement.click(function(e){
                    // bindings are defined after functions and component functions
                    e.preventDefault();
                });
               
                myFunction();
            }
        }
    }
   
    UTIL = {

        fire : function (func,funcname, args){
    
            var namespace = pages;  // indicate your obj literal namespace here
    
            funcname = (funcname === undefined) ? 'init' : funcname;
           
            if (func !== '' && namespace[func] && typeof namespace[func][funcname] == 'function') {
                namespace[func][funcname](args);
            }
    
        },
    
        loadEvents : function (){
             //use the body id to determine page & convert page url from /page/subpage to page_subpage
            var bodyId = document.body.id;
                bodyId = bodyId.replace(/-/g,'_');  // if double definition indent when possible to enhance legibility;

            // hit up common first.
            UTIL.fire('common');
    
            // do all the classes too.
            $.each(document.body.className.split(/\s+/),function (i,classnm){
                UTIL.fire(classnm);
            });
           
            UTIL.fire(bodyId);
            UTIL.fire('common','finalize');
    
        }
    
    };  //pages obj ends
   
    // kick it all off here
    $(document).ready(UTIL.loadEvents);
});
