/*
	application.js
	----------
	
	File		:	application
	SuperClass	:	?
	Project		:	project
	Dependencies:	jQuery http://jQuery.com
	Description :
		This file contains most of the controller-application logic for the website.
		not much stuff, just basic resize...ect...
	
	
	Created by fenrir on 03/03/08.
	Copyright 2008 LPAUDIOLAB. All rights reserved.
 
 */


var has_image = false;
var max_image = 0;
var imageArray = new Array();
var resizeFunc = null;
var adds_box = null;
var max_width = 0;
var enable_opacity=false;

function onloadfunc(event) {
	max_image--;
	if(max_image == 0) {
		resizeFunc(); // trigger the resize func when all images have been loaded
	}
}

function onReadyProc(){
	//enable_opacity = jQuery.browser.msie ? (parseInt(jQuery.browser.version < 7) ? false : true) : true;
	resizeAddsComponent();
	setupNewsletterController();
}

function setupNewsletterController(){
	var button = $('#newsletter-input-button');
	if(!button[0])
		return;
		
	button.click(function() {
		var result_email = $('#newsletter-input-email').val();
		if(
			result_email && 
			result_email != ''
		) {
			var where = $('#newsletter-result');
			var url = document.URL.substring(0, document.URL.lastIndexOf('/'));
			
			url = url.substring(0, url.lastIndexOf('/')) + '/newsadd.php';
			where.css("opacity", '0');
			$.postJSON(
				url, 
				{email:escape(result_email)}, 
				function(data){
					if(data.error)
						where.html("<span class='error'>"+data.error+"</span>");
					else if(data.result)
						where.html(data.result);
					where.fadeTo(1200, '1.0');
				}
			);
			
		}else{
			alert("champs email invalide !");
		}
	});
}

function resizeAddsComponent() {	
	
	
	if(enable_opacity) {	
		$('#content-data').css('opacity', '0');
		$('#adds').css('opacity', '0.0');
		$('#news').css('opacity', '0.0');
	}
	
	adds_box = $('#adds ul').children();
	
	if(adds_box[0])
		max_width = $(adds_box[0]).width();
	if(adds_box[1])
		max_width = Math.max($(adds_box[1]).width(), max_width);
		
	// Force image loading. Delay recalc after
	$('#adds img').each(function(){
		has_image = true;
		var attr = $(this).attr('src');
		if(attr && attr != '') {
			var load = new Image;
			imageArray.push(load);
			++max_image;
			load.src = attr;
			load.onload = onloadfunc;
		}
	});
	
	if(!has_image)
		resizeFunc();
}

function resizeAddsByTwo() {

	var max_height = 0;
	var last, index=0;
	
	if(!adds_box)
		return;
		
	adds_box.each(function() {
		var t=$(this), image;
		
		if(t.width() > max_width)
			t.width(max_width);
			
		if(image = t.children('a')) {
			if(image = image.children('img')) {
				if(image[0] && image[0].width > max_width) {
					image[0].width = max_width;
				}
			}
		}
		
		if(index & 1) { // compare only with last
			max_height = Math.max(t.height(), last.height());
			t.height(max_height);
			last.height(max_height);
		}else
			last = t; // store the one we will need
		
		++index;
	});
	
	if(enable_opacity) {
		$('#content-data').fadeTo(1000, '1.0');
		$('#adds').fadeTo(2000, '1.0');
		$('#news').fadeTo(3000, '1.0');
	}
}

// Resize according to the highest of all
function resizeAdds() {

	var adds_box = $('#adds ul').children();
	var max_height = 0, cur_height;
	adds_box.each(function() {
		var t=$(this), imageChild;
		cur_height = t.height();
		
		/*
		if(imageChild = t.children('img')) {
			imageChild = imageChild[0];
			if(imageChild) {
				//alert(imageChild.height);
				cur_height += imageChild.height; // add height to current
			}
		}
		*/
		
		max_height = Math.max(max_height, cur_height ? cur_height : 0);
	});
	
	if(max_height > 0) {
		adds_box.each(function() {
			$(this).height(max_height);
		});
		
		if(enable_opacity) {
			$('#content-data').fadeTo(1000, '1.0');
			$('#adds').fadeTo(1000, '1.0');
			$('#news').fadeTo(2000, '1.0');
		}
	}
}

function initializeApp() 
{
	resizeFunc = resizeAddsByTwo; // set the resize Func
	$(document).ready(onReadyProc);
}

// Main Proc
initializeApp();