$(init_page);

var menu_hover_idx = undefined;
var current_menu_idx = undefined;
var gallery_tooltip = undefined;
var hover_tooltip = false;
var www_template = www_root + 'template/legal';
function init_page(){
  
  /* Setup Debug Element */
  // $('#content').prepend('<div id="debug"></div>');

  init_top_menu();
  init_gallery();
  
  $('.gallery_pan a, .lightbox').lightBox({
    fixedNavigation: true,
    overlayOpacity:			0.8,
    imageLoading: www_template + '/js/jquery-lightbox-0.5/images/lightbox-ico-loading.gif',
		imageBtnPrev:	www_template + '/js/jquery-lightbox-0.5/images/lightbox_previous.gif',			// (string) Path and the name of the prev button image
		imageBtnNext:	www_template + '/js/jquery-lightbox-0.5/images/lightbox_next.gif',			// (string) Path and the name of the next button image
		imageBtnClose:www_template + '/js/jquery-lightbox-0.5/images/lightbox_close.gif',		// (string) Path and the name of the close btn
		imageBlank:		www_template + '/js/jquery-lightbox-0.5/images/lightbox-blank.gif'
  });

  init_footer_menu();
  
}

function debug(text){
   //  $('#debug').append('<div>'+ text + '</div>');
}

function init_gallery(){

    $('.gallery_bar').each(
      function(){
        var id = $(this).attr('id');
        var size = $(this).find('.gallery_pan img').length;
        var max_size = 8;
        var thumb_width =  $($(this).find('.gallery_pan img')[0]).width();
        var thumb_height=  $($(this).find('.gallery_pan img')[0]).height();
        if(thumb_width==0)thumb_width = 56;
        if(thumb_height==0)thumb_height= 56;
        
        var thumb_margin =  parseFloat($($(this).find('.gallery_pan a')[0]).css('margin-right').replace(/px/,''));
        
        this.offset = 0;  
        this.pan = $(this).find('.gallery_pan').get(0);
        $(this.pan).css({position: 'absolute', display:'block'});
        this.thumb_width = thumb_width;
 
        if(size > max_size){
          
          this.window_size = max_size;
          this.size = size;
        
          container_width = (max_size * ( thumb_width +  thumb_margin)) - thumb_margin ;
          // Setup prev next button;
          $(this).append('<div class="nav"><a href="#" class="prev"><span>Prev</span></a><a href="#" class="next"><span>Next</span></a></div>');
          
          
          var bt_prev = $(this).find('.prev').addClass('end');
          var bt_next = $(this).find('.next');
          

          bt_prev.get(0).gallery = this;
          bt_next.get(0).gallery = this;
 
                    
          this.slide_to = function(offset){
            if(offset >= this.size - this.window_size){
              offset = this.size - this.window_size;
              bt_next.addClass('end');
            }
            if(offset <= 0){
              bt_prev.addClass('end');
              offset = 0;
            }
            if(this.offset!= offset){
              if(this.offset > offset){
                bt_next.removeClass('end');
                  
              }else{
                bt_prev.removeClass('end');
              
              }
              this.offset = offset;
              $(this.pan).animate({left: -(this.offset* (this.thumb_width + 6 ))},{queue:false});
            }
          }
          
          this.next = function(){
            if(this.offset + this.window_size >= this.size) return false;
            this.slide_to(this.offset+max_size);
          }
          this.prev = function(){
            if(this.offset <=0) return false;
            this.slide_to(this.offset-max_size);
          }
          
          bt_prev.bind('click',
            function(){
              this.gallery.prev();
              return false;
            }
          );
          
          bt_next.bind('click',
            function(){
 
              this.gallery.next();
              return false;
            }
          );
          
        }else{
          container_width = (size * (thumb_width +  thumb_margin)) - thumb_margin ;
        }
        
        gal_container  = $(this).find('.gallery_container');
        gal_container.css({'overflow':'hidden',position:'relative'});                        
        gal_container.width(container_width);                        
        gal_container.height(thumb_height);                        
        gal_container.find('.gallery_pan').width(size * (thumb_width +  thumb_margin));
        gal_container.find('.gallery_pan').css({top:0,left:0});
        gal_container.css('margin-left', - (container_width + 6));
        

        
        // Icon Tooltip 

        gal_container.find('.gallery_pan a').bind('mouseover',
          function(){
            hover_tooltip = true;
            show_tooltip(this, $(this).attr('title'));
          }
        )

        gal_container.find('.gallery_pan a').bind('mouseout',
          function(){
            hover_tooltip = false;
            setTimeout(function() { hide_tooltip(); }, 500);
          }
        )
        
        
        
        
        
      }
    )
    
}
function call_show_tooltip(obj,html,top, left){
  hover_tooltip = true;
  show_tooltip(obj, html,top,left);
}
function call_hide_tooltip(){
  hover_tooltip = false;
  setTimeout(function() { hide_tooltip(); }, 500);
}
function show_tooltip(obj, html, offset_top, offset_left){
  if(gallery_tooltip==undefined){
    $('body').append('<div id="gallery_tooltip"><div id="gt_content"></div><div class="gt_footer"></div><em></em></div>').get(0);
    gallery_tooltip = $('#gallery_tooltip').get(0);
    
    $(gallery_tooltip).bind('mouseover', 
      function(){
        hover_tooltip = true;
      }
    );
    
    $(gallery_tooltip).bind('mouseout', 
      function(){
        hover_tooltip = false;
            setTimeout(function() { hide_tooltip(); }, 500);        
      }
    );
    
  }  
  $('#gt_content').html(html);
  $(gallery_tooltip).hide();  
  
  var obj_offset = $(obj).offset();
   
  if(offset_top==undefined){
    offset_top = (document.all?20:63);
  }
  if(offset_left==undefined){
    offset_left = $(obj).width()/2;
  }

  
  $(gallery_tooltip).css('left', obj_offset.left + offset_left);
  $(gallery_tooltip).css('top', obj_offset.top  -  ( $(gallery_tooltip).height() + offset_top ) );
  $(gallery_tooltip).show();
  
}

function hide_tooltip(){
  if(gallery_tooltip!=undefined){
    if(!hover_tooltip){
      // $(gallery_tooltip).fadeOut('slow');
      $(gallery_tooltip).hide();

    }
  }      
}


function init_top_menu(){
  
  $('.top_button .sub_menu').each(
    function(i){
      
      var offset = $(this).offset();
      var page_offset = $('#page').offset();
      var parent = $(this).parent();
      
      parent.children('a').get(0).sub_menu = this;
      parent.children('a').attr('href','#');

      $(this).attr('idx', i);
      $(this).appendTo('#page');
      $(this).wrap('<div class="submenu_container"></div>');
      $(this).parent().css(
        {position:'absolute',
        overflow:'hidden', 
        'left':offset.left - page_offset.left,
        top:offset.top - page_offset.top,
        width: $(this).width()
        
        });
      $(this).css({position:'absolute'});
      
      parent.children('a').bind('mouseover', 
        function(){
          menu_hover_idx = $(this).parent().parent().children().index( $(this).parent());
          // slide down
          //alert($(this.sub_menu).css('top'));

          if($('#page .submenu_container').css('display') == 'none' ){
            debug('open show'); 
            $('#page .submenu_container').show();
            $(this.sub_menu).css('top', -$(this.sub_menu).height());

          }
          
          $(this.sub_menu).animate({top:0},{queue:false});
          hide_sub_menus();        
        }
      );
      
      parent.children('a').bind('mouseout', 
        function(){
          if(current_menu_idx!=undefined){
            menu_hover_idx = current_menu_idx;
          }else{
            menu_hover_idx = undefined;
          } 
          setTimeout(function(){ hide_sub_menus() },1000);
        }
      );

      $(this).children('li').children('a').bind('mouseover', 
        function(){
          menu_hover_idx = $(this).parent().parent().attr('idx');          
         
        }
      );
      
      $(this).children('li').children('a').bind('mouseout',
        function(){
          

          menu_hover_idx = undefined;

          setTimeout( function(){ hide_sub_menus()},1000); 
          
        }
      );

      $(this).css('top',-$(this).height());
      $(this).parent().height($(this).height());
    }
    
  );
  
  
  
  $('#page .submenu_container').bind('mousemove',function(){ 
    if(menu_hover_idx==undefined || menu_hover_idx=='none'){
      
      if(BrowserDetect.browser =='Explorer' ){
        // IE 6 is so stupid, it doesn't even know it overlap the other element
        return;  
      }

      $(this).hide(); 
    }
  })
  
}


function hide_sub_menus(){
  if(menu_hover_idx!=undefined) return;

  $('#page .sub_menu').filter(
    function(i){
      if($(this).attr('idx') == menu_hover_idx ){
        return false;  
        
      }
      if(menu_hover_idx == undefined){
        menu_hover_idx = $(this).attr('idx');
        return true;
        
      }
      return  $(this).css('display')=='' || $(this).css('display')=='block'; 
    }
  ).animate({top:-$('#page .sub_menu').height()},{
    queue:false, 
    complete:function(){
          menu_hover_idx = 'none';
      }
    }
    );

  
  //slideUp('fast');
}

function init_footer_menu(){
    $('.footer_menu li a').each(
      function(i){
        $(this).bind('mouseover', function(){
          if($(this).attr('show')!=''){
            
            $('.footer_menu li a').removeClass('selected');
            $(this).addClass('selected');
            
            show_footer_office($(this).attr('show'));
          }
        });

        /*

        $(this).bind('mouseout', function(){
          $('.footer_menu li a').removeClass('selected');
          $('.footer_menu li:first-child a').addClass('selected');
          show_footer_office('office_houston');
        });
        */

      }
    
    );
}

function show_footer_office(id){
  $('.office_info').hide();
  $('#'+id).show();  
}


function is_valid_email(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   }
   return true;
}

function strrpos( haystack, needle, offset){
    var i = (haystack+'').lastIndexOf( needle, offset ); // returns -1
    return i >= 0 ? i : false;
}


/* Browser Detection Class */
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
};

BrowserDetect.init();