  


jQuery(
  function ($) {
   
    var	URI 	    = new URIParser($),
        $Nav      = new Nav($('div#nav')),
        $About    = new About($('div#content-about')),
        $Taproom  = new Taproom($('div#content-taproom'));
		
   function __construct(){
   		
   		init_flash();
   		
      $body = $('html,body').attr('scrollTop',0);
   		
      $.ajaxSetup({
        cache: false,
        dataType: 'html',
         global: false,
        timeout: 5000
      });
		
      new Scroller();
      
      new BlankLinks();
		
   	}
   	
   	function init_flash() {

			
			var flashvars = {
 			 	hello: "world"
			};
			
			var params = {
  				menu: "false",
  				allowFullScreen: "true",
  				wmode: "transparent"
			};


			swfobject.embedSWF("greatdividebrewing.swf", "flash-wrapper", "100%", "475", "9.0.115", "assets/swf/expressInstall.swf",flashvars,params);

   	
   	}
   	
   	
   	 __construct();

  }
  
);

function Scroller(){
	
	function __construct (){
		
		$('body').bind('ACTIVATE_NAV',on_activate_nav);
	
	}
	
	function on_activate_nav(e,id){
		switch (id){
			case "beer":
				var position = 0;
			break;
			case "about":
				var position = $('div#nav').offset().top;
			break;
			case "taproom":
				var position = $('div#content-taproom').offset().top;
			break;
			
		}
		
		switch (id){
		  case "blog":
		    window.open('http://blog.greatdivide.com/blog.php','_blog');
		  break;
		  
		  case "hopshop":
		    window.open('http://hopshop.greatdivide.com/','_blank');
		  break;
		  
		  default:
		    $body.stop().animate({scrollTop: position},1200,'easeInOutQuart',on_scroll_complete);
		  break;
		  
		  
		}
	
	}
	
	function on_scroll_complete(e){
	
	}

	__construct();
	
}

function Nav($self){
	var $footer = $('div#footer');

	function __construct(){
	
		$self
			.find('a')
			.each(function(){new NavItem($(this))});
			
		$footer
			.find('a')
			.each(function(){new NavItem($(this))});
			
	}
	
	__construct();

}

function NavItem($self){

	var id 	= $self.attr('rel');
	
	function __construct(){
	
		$self.bind('click',on_click)
		
		$('body').bind('ACTIVATE_NAV',on_activate_nav);
		
	}
	
	function on_click(e){
	
		$('body').triggerHandler('ACTIVATE_NAV',id)
	
		//e.preventDefault();
	
	}
	
	function on_activate_nav(e,i){
	
		if (i==id)
			$self.removeClass().addClass('active')
		else
			$self.removeClass()

	}
	
	__construct();
	
	
}


function About($self){
  
  var $Items      = $self.find('div.items'),
      active      = 0,
      acitve_pos  = 0;
  
  function __construct(){
  
     $self.bind('SUBNAV_ACTIVATE',on_subnav_activate);
     
     
     new SubNav($self.find('div.aux'),$self)
     
     new DistributorMap($self.find('div#distributor-map'))
  
     new Gallery($self.find('div.gallery'));
     
     new Awards($self.find('div.awards-all'));
    
  }
  
  function on_subnav_activate(e,id,position){
  
    active = id;
    
    active_pos = position;
    
    hide_scrollerbars();
    
    $Items
      .animate({marginLeft:-650*position},1100,'easeInOutQuart',show_scrollerbars);
      
  }
  
  function hide_scrollerbars() {
    
    $Items
    .find('p.item-text')
    .each(
      function()
      {
        $(this).css({overflow:'hidden'})
      })
    
  }
  
  function show_scrollerbars() {
    
    $Items
    .find('p.item-text')
    .each(
      function()
      {
        $(this).css({overflow:'auto'})
      })
    
  }
  
  function Awards($self) {
    
    new AwardSelect($self.find('select#awards-year'),$self,'year');
    
    new AwardSelect($self.find('select#awards-beer'),$self,'beer');
    
    $self
    .bind('ON_AWARD',on_award)
    
    
    function on_award(e,id,type){
    
     $.ajax({
				beforeSend: function () {},
				data: {id:id,gateway:'awards',type:type},
				error: function () {},
				success: bind_awards,
				url: 'gateway/gateway.php'
			 });
		      
    }
    
    function bind_awards(response) {
      
      new PopUpDOM(
            $(response),
            'events',
            {
             x: 0,
             y: 550,
             w: 0,
             h: 0
            });
        
        
    }

    
    function AwardSelect($self,$owner,award_type){
      
      function __construct() {
        
        $self
        .bind('change',on_change)
        
        $owner
        .bind('ON_AWARD',on_award)
        
        function on_award(e,id,type) {
        
          if (type != award_type) {
            
             $self.val(0)
            
          }
        
        }
      
      }
      
      function on_change(e) {
        
        var id = $(this).val()
        
        if (id!=0)
          $owner.triggerHandler('ON_AWARD',[id,award_type])
        
      }
      
      __construct();
      
    }
  
  
  }
  
  function DistributorMap($self) {
    
    var $ImageMap = $('map#dist-map'),
        $Select   = $self.find('div.dist-select select');
        
    function __construct() {
    
      
      $self
      .bind('ON_STATE_CLICK',on_state_click);
      
      new MapSelect($Select,$self);
      
      $ImageMap
      .find($('area'))
      .each (
        function()
        {
        
          new MapLocation($(this),$self);
          
        });
        
    }
    
    function on_state_click(e,id,ele) {
    
      loading = true
      loaded  = active + 1;
      
      $.ajax({
				beforeSend: function () {},
				data: {id:id,gateway:'distributor'},
				error: function () {},
				success: bind_distributor,
				url: 'gateway/gateway.php'
			 });
		
      
    }
    
    function bind_distributor(response) {
      
      new PopUpDOM(
            $(response),
            'events',
            {
             x: 0,
             y: 550,
             w: 0,
             h: 0
            });
        
        
    }
    
    function MapSelect ($self,$owner) {
    
      function __construct() {
        
        $self
        .bind('change',on_change)
      
      }
      
      function on_change(e) {
        
        var id = $(this).val()
        
        if (id!=0)
          $owner.triggerHandler('ON_STATE_CLICK',[id,$self])
        
        
      
      }
      
        
        __construct();
        
        
    }
    
    
    function MapLocation($self,$owner) {
      
      function __construct() {
      
       $self
       .bind('click',on_click)
      
      }
      
      function on_click(e) {
        
        var id = $(this).attr('alt')
        
        $owner.triggerHandler('ON_STATE_CLICK',[id,$self])
        
        
      
      }
      
      
      __construct();
      
    }
    
    __construct();
  
  
  }
  
  
  function Gallery($self) {
    var  $content = $self.find('div.gallery-content-items'),
         is_ajax = false,
         active  = 0,
         loaded  = 1,
         loading = false,
         total   = 2,
         $next    = $self.find('a.next-btn'),
         $prev    = $self.find('a.prev-btn');
    
    __construct();
    
    function __construct() {
    
      $content.css({opacity:1});

      init_gallery_items();
    
      init_prev_next();
    
    }
    
    function init_prev_next() {
    
      $prev
      .bind('click',on_prev_click)
      
       $next
      .bind('click',on_next_click)
      
      function on_prev_click(e) {

        seek('prev');
        
        e.preventDefault();
      
      }
      
      function on_next_click(e) {
        
        seek('next');
        
        e.preventDefault();
      
      }
    
    }
    
    function seek(dir) {
    
      active+=(dir=="next") ? 1 : -1;
      
      if (active > total) active = 0;
      
      if (active < 0) active = total;
      
      if (!loading){
      
        
      
        is_ajax = true;
        
        if (dir=="next" && loaded < active + 1) {
          
           $next
          .removeClass()
          .addClass('next-btn-loading') 
          
          load_ajax();
        
        }else{
        
          scroll()
        
        }
        
          
      }
      
      
    }
    
    function load_ajax() {
       
      loading = true
      loaded  = active + 1;
      
      $.ajax({
				beforeSend: function () {},
				data: {id:active,gateway:'gallery'},
				error: function () {},
				success: on_query_success,
				url: 'gateway/gateway.php'
			 });
			 
    }
    
    function on_query_success(response){
      
      loading = false;
      
      $next
      .removeClass()
      .addClass('next-btn');
        
      $content.append(response);
      
      scroll()
      
      init_gallery_items();
  
    }
    
    function scroll() {
    
      $content
      .animate({marginTop:-198*active},1000,'easeInOutQuart')
    
    }
    
    function init_gallery_items(){

      
      $content
        .find('div.gallery-item')
        .each(function() { new GalleryItem($(this)); });
      
        
    }
    
    function GalleryItem($self) {
    
      
      function __construct() {
        
      
        $self
          .css({cursor:'pointer'})
          .bind('click',on_click)
          
    
      }
      
      function on_click(e){
        
        var ele_attr = {
            x: $self.offset().left,
            y: $self.offset().top,
            w: $self.width(),
            h: $self.height()
            }
        
        $html = $self.clone();
        
        new PopUpDOM($html,'about',ele_attr);
        
        e.preventDefault();
        
      }
      
      __construct();
      
    }
    
  }
  
  __construct()
  
}


function Taproom($self){
  
  var $Items             = $self.find('div.items'),
      $FestivalsEvents   = $self.find('div#events-festivals'),
      $TaproomEvents     = $self.find('div#events-taproom'),
      $TaproomEventsNav  = $self.find('div#taproom-event-nav')
      active      = 0,
      acitve_pos  = 0;
      
  function __construct(){
  
    new SubNav($self.find('div.aux'),$self);
    
    new Events($FestivalsEvents,$FestivalsEvents);
    
    new Events($TaproomEvents,$TaproomEventsNav);
   
    $TaproomEventsNav.css({display:'none'});
    
    init_links();
    
    $self.bind('SUBNAV_ACTIVATE',on_subnav_activate);
  
    
  }
  
  
  function init_links(){
  
    $self
    .find('a._blank')
    .each(
      function()
      {
       // $(this).bind('click',on_blank_click)
        $(this).attr("target","_blank")
      });
      
    function on_blank_click(e){
  
     // window.open($(this).attr('href'),'_blank')
      
     // e.preventDefault();
      
    }
  
  }
  

  function check_event_status() {
    
    if ($('div#taproom-item-' + active).find('div#events-taproom').length > 0)
      $TaproomEventsNav.css({display:'block'});
    else
      $TaproomEventsNav.css({display:'none'});
    
  }
  
  function on_subnav_activate(e,id,position){
    
    active = id;
    
    active_pos = position;
    
    check_event_status();
    
    $Items
      .animate({marginLeft:-650*position},1100,'easeInOutQuart')
  
  }
  
  
  __construct()
  
  
}

function Events($self,$nav) {

  var $content = $self.find('div.events-content-items'),
      $next    = $nav.find('a.next-btn'),
      $prev    = $nav.find('a.prev-btn'),
      active   = 0,
      total    = 0
  
  function __construct(){
  
    init_prev_next();
    
    init_events();
    
  }
  
  
  
  function init_events(){
  
     $self
      .find('div.event-item')
      .each(
        function(i)
        {
          total++;
          new EventItem($(this));
        });
      
      total = Math.ceil(total/3) - 1

        
  }
  
 function init_prev_next() {
    
      $prev
      .bind('click',on_prev_click)
      
       $next
      .bind('click',on_next_click)
      
      function on_prev_click(e) {

        seek('prev');
        
        e.preventDefault();
      
      }
      
      function on_next_click(e) {
        
        seek('next');
        
        e.preventDefault();
      
      }
    
  }
    
  function seek(dir) {
      
      active+=(dir=="next") ? 1 : -1;
      
      if (active > total) active = 0;
      
      if (active < 0) active = total;
     
      $content
      .animate({marginLeft:-594*active},1000,'easeInOutQuart')
    
  }

  function EventItem($self) {
  
    var $a       = $self.find('a.event-item-btn'),
        $content = $self.find('div.event-item-content'),
        html     = $content.html();
    
    function __construct(){
      
      $a.bind('click',on_click)
      
    }
    
    function on_click(e){
        
        $html = html;//$a.html();
        
        
        
         var ele_attr = {
            x: $self.offset().left,
            y: $self.offset().top,
            w: $self.width(),
            h: $self.height()
            }
            
        new PopUpDOM($html,'events',ele_attr);
        
        e.preventDefault();
        
    }
    
    __construct();
    
  }
  
  __construct();

}


function Categories($self,$owner){

  var active = 0;
  
  function __construct(){
   
    $self
    .find('li.category')
    .each(
      function(i){
        new Category($(this),i);
      });
      
  }

  
  function Category($self,position){
    
    var $a  = $self.find('a'),
        pos = position,
        id  = $a.attr('rel');
    
    $self
      .bind('click',on_click);
    
    $owner
      .bind('CATEGORY_ACTIVATE',on_category_activate)          
   
    function on_click(e){
      
      
      $owner.triggerHandler('CATEGORY_ACTIVATE',[id,position])
     
      e.preventDefault();
      
    }
    
    function on_category_activate(e,a_id){
    
      if (a_id == id)
        $a.removeClass().addClass('active');
      else
        $a.removeClass();
    
    }
    
  }
  
   __construct()
  
  
}




function SubNav($self,$owner){

  var active = 0;
  
  function __construct(){
   
    $self
    .find('li.subnav')
    .each(
      function(i){
        new SubNavItem($(this),i);
      });
      
  }

  
  function SubNavItem($self,position){
    
    var $a  = $self.find('a'),
        pos = position,
        id  = $a.attr('rel');
    
    $self
      .bind('click',on_click);
    
    $owner
      .bind('SUBNAV_ACTIVATE',on_subnav_activate)          
   
    function on_click(e){
      
      $owner.triggerHandler('SUBNAV_ACTIVATE',[id,position])
     
      e.preventDefault();
      
    }
    
    function on_subnav_activate(e,a_id){
    
      if (a_id == id)
        $a.removeClass().addClass('active');
      else
        $a.removeClass();
    
    }
    
  }
  
   __construct()
  
  
}


function PopUpDOM($html,type,ele_attr) {

  var $self     = $('div#popup'),
      $content  = $self.find('div#popup-content'),
      $close    = $self.find('a.close');
  
  function __construct(){
    
    $self.removeClass().addClass(type);
    
    $content.html($html);
    
    $content
        .find('a')
        .each(
          function(){
            $(this).attr('target','_blank')
          }
        );
    show();
    
  }
  
  function show() {

      $body = $.browser.safari ? $('body') : $('html')
      
      var top =  $body.attr('scrollTop') + $(window).height()/2 - 243;
     
      $self
      .stop()
      .css({position:'absolute',opacity:0,height:ele_attr.h+10+'px',width:ele_attr.w+10+'px',top:ele_attr.y-10+'px',left:ele_attr.x-10+"px",margin:0})
      .animate({opacity:1,height:486,width:486,marginLeft:-243,top:top,left:$(window).width()/2},1000,'easeInOutQuart')
      
      $self.find('img')
      .stop()
      .css({height:ele_attr.h+'px',width:ele_attr.w+'px'})
      .animate({height:446,width:446},900,'easeInOutQuart')
  
      $close
        .bind('click',close)
  }
  
  function close(e) {
    
      $self
      .stop()
      .css({left:$(window).width()/2,marginLeft:-243})
      .animate({
        opacity:0,
        height:ele_attr.h+10+'px',
        width:ele_attr.w+10+'px',
        left:ele_attr.x-10+"px",
        top:ele_attr.y-10+'px',
        marginLeft:0
        }
        ,1000,
        'easeInOutQuart',
        on_close_complete);
      
      $self
        .find('img')
        .animate({height:ele_attr.h+'px',width:ele_attr.w+'px'},1100,'easeInOutQuart');
      
      e.preventDefault();
  }
  
  function on_close_complete(e){
    $self.css({height:0,width:0})
  }
  
  __construct();
  
}

function BlankLinks() {
  
  function __construct() {
  
    $('div.wrapper').find('a.extra-blog').each(function(){$(this).attr("target","_blank")})
    
    $('div.wrapper').find('a.extra-newsletter').each(function(){$(this).attr("target","_blank")})

  }
  
  __construct();
}



/*
**
** URI PARSING
**
*/

function URIParser($){
	
	this.Hash 			= location.hash;
	this.HashObjects	= this.Hash.split("/");
		
	function 	__construct() {
		
	}

	__construct();
	
}

URIParser.prototype.segment=function(id){
	if (this.HashObjects[id])
		return this.HashObjects[id];
	else
		return '';
}

/*
** FLASH
**
*/

function call_back(){

	$('body').triggerHandler('FLASH_CALLBACK');
	
}

