$(init_page);
var start_time =   (new Date()).getTime();
var sux_browser = false;
var current_screen= '';

function init_page(){
  BrowserDetect.init();
  if(BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 8)sux_browser = true;
  $(window).resize(window_resize);
	
	$('.screen_info').find('a[href^="#"]').click(function(event) {
		event.preventDefault();
		
		$('a[name="' + $(this).attr('href').replace('#', '') + '"]').css({
			'display':'block',
			'width': '1px',
			'height':'1px'
		});
		
		var target_offset = $('a[name="' + $(this).attr('href').replace('#', '') + '"]').position();
		var target_top = target_offset.top;
 
		$('#screen_info_wrap').animate({scrollTop:target_top}, 500);
		//alert(target_top);
		return false;
	});
 
  $('.launch_screen').each(function(){
    $(this).click(function(){
      show_screen($(this).attr('href'));
      return false;
    });  
		
			
		$("#contact_button").hover(function()	{
			this.src = this.src.replace("a.gif","b.gif");
		}, function() {
			this.src = this.src.replace("b.gif","a.gif");
		});
	
  }) 

 /* $('body').mousewheel(function(event,delta){
   // alert("here "+ delta);
    if(current_screen !=''){
      
      var mt = $(current_screen).css('margin-top').replace(/px/,'');
      mt= mt *1;
 
      mt+= (30*delta); 
      
      $(current_screen).css({'margin-top':mt});
      //$(current_screen).animate({'margin-top':mt},{queue:false});
      
      //alert(mt);  
    }
  });*/
	
  $('#info,.close_screen').click(function(){
    hide_screen();
    return false;
  })
 
  if(!document.all){ 
   // $('#hotspot').remove();
  }
  
 
  
  /*
  $(document).mouseup(function(e){
    alert("HERE " + e.pageX + ',' + e.pageY);   
    var offset = 
      
  })*/

  window_resize();
  
 
  // alert(window.location.hash);
  
  if(window.location.hash!= undefined && window.location.hash!=''){
    
    var loc = window.location.hash.split('?');
    
    show_screen(loc[0]);  
  }
	
	init_background(); 
}


function show_info(){
   show_screen('#info');
}
function show_screen(key){
  
  current_screen = key;
  $('#screen').css({ 'opacity':0,'display':'block'}).animate({opacity:0.9},{duration:1000});  
  $('#screen_info_wrap').show();  
  //$(current_screen).css({'margin-top':'-200px'}).delay(500).fadeIn(1000);
	$(current_screen).vAlign().delay(500).fadeIn(1000);
	
	$("#screen_info_wrap").css({ 'overflow': 'auto' });
   
}
function hide_screen(){
  //$("#screen_info_wrap").css({ 'overflow':'hidden' });
	
  $(current_screen).fadeOut('slow'); 
  $('#screen').delay(500).fadeOut(1000, function() { $('#screen_info_wrap').hide(); });
  
  current_screen = '';
    
}

function window_resize(){
  $('#wrap,#screen').css({width: $(window).width() + 'px', height: $(window).height()+ 'px' }); 
  if($(window).height()< 350){
  /*  if($('#footer').css('margin-top') == '-100px'){
      $('#footer').animate({'margin-top':'0',opacity:0},{queue:false});
    }
  }else{
    if($('#footer').css('margin-top') == '0px'){
      $('#footer').animate({'margin-top': '-100px',opacity:1},{queue:false});
    }*/
  }
   
  if(current_screen!=''){
		$(current_screen).vAlign();
    //$(current_screen).css({'height': $(window).height()-100 } );  
  }
}

function init_background(){
 // if(background!=''){
		 
    var preload_imgs = [];
    preload_imgs.push($('#background img').attr('src'));
		
		$('#background img').removeAttr('width');
		$('#background img').removeAttr('height');
    
    $.imgpreload(preload_imgs,{
      all:function(){
        $('#background').hide();
        background_resize();
        
        end_time =   (new Date()).getTime();
        if((end_time - start_time) > 1000){
          $('#background').fadeIn(350); // long wait -> fade in
        }else{
          $('#background').show(); // short wait -> just show
        }
        $(window).resize(background_resize);              
      }  
    }) 
              
    
 // }
}

function background_resize(){
	if ($(window).width() > 1000 || $(window).height() > 600){
		if ($(window).width() > $(window).height() / 0.6)
		{
			w = $(window).width();
			h = $(window).width() * 0.6;
		}
		else
		{
			w = $(window).height() / 0.6;
			h = $(window).height();
		}// end else if
	}
	else
	{
		w = 1000;
		h = 600;
	}			


  $('#background img').width(w);
  $('#background img').height(h);
  
  /* position background */

		$('#background img').css('left', '-' + (((w - parseFloat($('#wrap').width())) / 2) ) + 'px');

}


/* Helper functions */

function has_flash(){
  return FlashDetect.installed;
}


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;
}



/* 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"
		}
	]
};


(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	
	if (ph < ah) {  	
  	$(this).css('margin-top', '0px');
  }
  else {
  	var mh = (ph - ah) / 2;
  	$(this).css('margin-top', mh);
  }
	});
};
})(jQuery);


