function detect_device() {
    if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
        return 'iPhone';
    }
    
    if(navigator.userAgent.match(/iPad/i)) {
        return 'iPad';
    }
    
    return 'computer';
}

function isIDevice() {
    return detect_device()=='iPad' || detect_device()=='iPhone';
}

function observe_email_field() {
    Event.observe($('l343979-343979'), 'click', function() { $('l343979-343979').value='' });
}

function show_preview(id) {
	if($(id)!=null)
	    new Effect.Appear(id, {duration: 0.2})
}

function hide_preview(id) {
	if($(id)!=null)
	     new Effect.Fade(id, {duration: 0.2})
}

function target_blank() {
	$$('a:([rel~=new])').each(function(link) {
	  link.onclick = function(){window.open(this.href); return false;};
	});
}

function get_window_height() {
	var windowHeight=0;
	if (typeof(window.innerHeight)=='number') {
	  windowHeight=window.innerHeight;
	}
	else {
	  if (document.documentElement&&
	    document.documentElement.clientHeight) {
	      windowHeight=
	        document.documentElement.clientHeight;
	  }
	  else {
	    if (document.body&&document.body.clientHeight) {
	      windowHeight=document.body.clientHeight;
	    }
	  }
	}

	// if IE7 -> add 18 px to windowHeight
	re = /ie7/ ;
	h=document.getElementsByTagName('html')[0] ;
	is_ie7 = h.className.match(re) ;
	//alert(h.className.match(re)) ;
	windowHeight = is_ie7 ? (windowHeight + 0) : windowHeight ;

	return windowHeight;
}

function resize_browser() {
	// if IE -> add 17 px to window width
	re = /ie/ ;
	h=document.getElementsByTagName('html')[0] ;
	is_ie = h.className.match(re) ;
	width = is_ie ? 1024 : 1000;
	
	height = (screen.height>768) ? 800 : 750;
	
	window.moveTo(0,0);
	window.resizeTo(width,height) ;
}

function resize_zones() {
    if(isIDevice())
        return false;
	
	var window_height			= get_window_height() ;
	// var up_height				= window_height/3 ;
	var up_height				= $('up').getHeight() ;
	var down_height				= window_height - up_height; //(window_height/3)*2 ;
	
	var up_container_height 	= $('up-container').getHeight() ;

    //alert($('panel'));
	if($('panel')!=null) {
	    var title_container_height	= $('title-container').getHeight() ;
    	
    	// set the panel size
	    $('panel').style.height = window_height.toString() + "px" ;
	
    	// set the title size and the centered position of the title container
    	$('title').style.height					= up_height.toString() + "px" ;
    	$('title-container').style.top	= (up_height - title_container_height)/2 + "px" ;
    }
		
	// set the down size	
	$('down').style.height 		= down_height.toString() + "px" ;
	if($('lightwindow_overlay')!=null)
	    $('lightwindow_overlay').style.height = window_height + "px" ;
}

function hide_panel() {
	new Effect.Appear('title-content', { duration: 0.9}) ;
	new Effect.Fade('title-content', { queue: 'end', duration: 1.4}) ;
	new Effect.BlindUp('panel', { queue: 'end', duration: 1.4}) ;
}

function terminate() {
	//$('down').style.overflow	= "auto" ;
	if($('title')!=null)
	    $('title').hide();
}

function show_popup() {
    var mine = window.open('http://baronianfrancey.com/assets/newsletter/index.html','','width=550,height=500,left=0,top=0,scrollbars=yes,toolbar=no,location=no,menubar=no');
    if(mine)
       var popUpsBlocked = false;
    else
       var popUpsBlocked = true;
       
    if (popUpsBlocked) {
        alert('You are using a popup-blocker. Desactivate it if you want to see our news.');
    }
    else {
        if (window.focus) {mine.focus()}
    }
}

function intro_load() {
	// show the content that was hidden at load so it was not partially displayed
	$('content').show();
	
	// calculate sizes and positions
	resize_browser() ;
	resize_zones() ;
	
	// register the resize event
	Event.observe(window, 'resize', resize_zones);
	
	// hide the panel
	hide_panel() ;
	
	// restore overflow and hide the title div
	setTimeout("terminate();", 2400) ;
	setTimeout("start_slideshow();", 2400) ;
	
    // show_popup();
}

function direct_load() {
	// hide the panel
	// $('panel').hide() ;
	
	// show the content that was hidden at load so it was not partially displayed
	// $('content').show();
	
	// restore overflow and hide the title div
	terminate();

	// calculate sizes and positions
	//resize_browser() ;
	resize_zones() ;
	
	// register the resize event
	Event.observe(window, 'resize', resize_zones);
}


window.onload = init ;
