
var modal = null;
var smallmodal = null;

function initModal(){
	modal = $('<div id="modal_layout"><div id="modal_inner_layout"></div></div>').gifModal();
}

function openModal(url, onResponse) {
	if (modal == null) {
		initModal();
	}
	if (modal != null) {
		$(' > div#modal_inner_layout', modal).html('<div class="loading" style="height: 95%"></div>');
		modal.dialog('open');
		$(' > div#modal_inner_layout', modal).load(url, onResponse);
		//modal.css('z-index', 200);
	}
}

function fillModal(content) {
	if (modal == null) {
		initModal();
	}
	if (modal != null) {
		$(' > div#modal_inner_layout', modal).html(content);
	}
}

function showModal() {
	if (modal == null) {
		initModal();
	}
	if (modal != null) {
		modal.dialog('open');
	}
}

function closeModal() {
	if (modal != null) {
		$(' > div#modal_inner_layout', modal).html('');
		modal.dialog('close');
	}
}







function initSmallModal(){
	smallmodal = $('<div id="small_modal_layout"><div id="small_modal_inner_layout"></div></div>').gifSmallModal();
}

function openSmallModal(url, onResponse) {
	if (smallmodal == null) {
		initSmallModal();
	}
	if (smallmodal != null) {
		$(' > div#small_modal_inner_layout', smallmodal).html('<div class="loading" style="height: 95%"></div>');
		smallmodal.dialog('open');
		$(' > div#small_modal_inner_layout', smallmodal).load(url, onResponse);
		//modal.css('z-index', 200);
	}
}

function fillSmallModal(content) {
	if (smallmodal == null) {
		initSmallModal();
	}
	if (smallmodal != null) {
		$(' > div#small_modal_inner_layout', smallmodal).html(content);
	}
}

function showSmallModal() {
	if (smallmodal == null) {
		initSmallModal();
	}
	if (smallmodal != null) {
		smallmodal.dialog('open');
	}
}

function closeSmallModal() {
	if (smallmodal != null) {
		$(' > div#small_modal_inner_layout', smallmodal).html('');
		smallmodal.dialog('close');
	}
}





function prodAssocClicked(artId, masterId, index) {
	openModal("/catalogue/ajaxProduitsAssoc?articleId="+artId+"&masterId="+masterId, null);
}

$(document).ready(function() {
	// As-t-on un fil d'ariane ?
	var filAriane = $('span#ariane');
	if (filAriane != null && filAriane != undefined
			&& filAriane[0] != null && filAriane[0] != undefined) {
		filAriane.filAriane();
	}
	
});


$.widget("gif.filAriane", {
	_init: function() {
		this.options.title = $(' > span', this.element);
		this.options.links = $(' > a', this.element);
		this.options.linksOrigTxt = [];
		for (var i = 0; i < this.options.links.length; i++) {
			this.options.linksOrigTxt[this.options.linksOrigTxt.length] = $(this.options.links.get(i)).html();
		}
		
		var index = 0;
		var changedSomething = false;
		
		while (this.options.title.position().top != 0) {
			if (index == 0) changedSomething = false;
			
			var link = $(this.options.links.get(index));
			var linkStr = link.html();
			var newLinkStr = this.reduce(linkStr);
			if (newLinkStr != linkStr) {
			//if (strlen(newLinkStr) < strlen(linkStr)) {
				link.html(newLinkStr);
				changedSomething = true;
			}
			
			index++;
			if (index == this.options.links.length) {
				if (changedSomething == true) {
					index = 0;
					continue;
				} else {
					break;
				}
			}
		}
	},
	reduce: function(str) {
		if (str == null) return "";
		var trimed = trim(str);
		if (trimed.length < str) return trimed;

		if (strlen(str) > 0) {
			var index = strrpos (str, ' ');
			if (index > 0) {
				return substr(str, 0, index)+"...";
			}
		}
		
		return str;
	},
	options: {
		// This are not te be used as option parameters
		links: null,
		linksOrigTxt: null,
		
		title: null
	}
});

$.widget("gif.gifModal", {
	_init: function() {
		this.element.dialog({
			title: 'Produits associ&eacute;s',
		    autoOpen: false,
		    modal: true,
		    width: 806,
		    height: 442,
		    resizable: false,
		    draggable: false,
		    closeText: ''
		  });
		
		var elem = this.element.parents('.ui-dialog.ui-widget.ui-widget-content.ui-corner-all');
		if (elem != null && elem[0] != undefined) {
			elem.addClass("gif_modal");
		}
	},
	options: {
		filter: null,
		firstClassName: 'first',
		// This are not te be used as option parameters
		elements: null
	}
});

$.widget("gif.gifSmallModal", {
	_init: function() {
		this.element.dialog({
			title: 'D&eacute;clinaisons produit',
		    autoOpen: false,
		    modal: true,
		    width: 558,
		    height: 229,
		    resizable: false,
		    draggable: false,
		    closeText: ''
		  });
		
		var elem = this.element.parents('.ui-dialog.ui-widget.ui-widget-content.ui-corner-all');
		if (elem != null && elem[0] != undefined) {
			elem.addClass("gif_small_modal");
		}
	},
	options: {
		filter: null,
		firstClassName: 'first',
		// This are not te be used as option parameters
		elements: null
	}
});

$.widget("gif.prodsAssoc", {
	_init: function() {
		this.element.layout({
		    south: {
		        spacing_open: 0,
		        size: 71,
		        resizable: false
		      },
		      center: {
		      }
		    });
		
		$('#article_assoc_center_container', this.element).css('z-index', 1004);
		
		this.options.article_div = $('.article_assoc_page', this.element);
		this.options.assocs_div = $('#prodsAssocList', this.element);
		
		this.options.noneScrollableDiv = $('div#produits_associes_div', this.element);
		this.options.scrollableDiv = $(' > div', this.options.noneScrollableDiv);

		this.checkCurrentSelectionVisible();
		
		$('td.td_assoc', this.options.assocs_div).each(function(index, elem) {
			elem.onclick = null;
		});
		$('td.td_assoc', this.options.assocs_div).bind('click', jQuery.proxy(this.clicked, this));
	},
	checkCurrentSelectionVisible: function() {
		var current = $('td.td_assoc.selected');
		if (current.size() > 0) {
			var pos = current.position().left;
			var width = current.width();
			
			var missing = pos+width-this.options.noneScrollableDiv.width();
			if (missing > 0) {
				this.options.scrollableDiv.scrollLeft(this.options.scrollableDiv.scrollLeft()+missing+5);
			}
		}
	},
	clicked: function(event) {
		var index = substr(event.currentTarget.id, 9);
		var artid = $('.art_id', event.currentTarget).html();
		this.loadArticle(artid);
		this.setSelectedIndex(index);
		//alert(index+' - '+artid);
		return false;
	},
	setSelectedIndex: function(index) {
		$('td.td_assoc.selected', this.options.assocs_div).removeClass('selected');
		$('td#td_assoc_'+index, this.options.assocs_div).addClass('selected');
	},
	loadArticle: function(articleid) {
		this.options.article_div.html("<div class='loading' style='height: 95%'></div>");
		this.options.article_div.load('/catalogue/ajaxFicheProd?articleId='+articleid);
	},
	options: {
		// This are not te be used as option parameters
		article_div: null,
		assocs_div: null,
		
		noneScrollableDiv: null,
		scrollableDiv: null
	}
});

$.widget("gif.ficheProd", {
	_init: function() {
		this.options.declinaison_div = $('#declinaison', this.element);
		if (this.options.declinaison_div.length > 0) {
			this.options.declinaison_div.bind('click', jQuery.proxy(this.declinaisonClicked, this));
			this.options.declinaison_link = $('#declinaison_link', this.element).html();
			this.options.list_declinaisons = $('#list_declinaisons');
		}
		
		// IE Bug
		if(jQuery.browser.msie == true) {
			this.options.img = $('table.fiche_prod_img_table_container img', this.element);
			this.options.img.bind('load', jQuery.proxy(this.imgLoaded, this));
		}
		
		// Video
		var vid = $('a.fiche_prod_video_link', this.element);
		if (vid.length > 0) {
			this.options.video_link = vid;
			var vid_div = $('#video_player_div', this.element);
			if (vid_div.length > 0) {
				this.options.video_div = vid_div;
				this.options.video_player = $('#flash_video_player', this.element);
				this.options.video_link.bind('click', jQuery.proxy(this.videoClicked, this));
			}
		}
	},
	initList: function() {
		$('tr.decl_tr', smallmodal).bind('click', jQuery.proxy(this.decInListClicked, this));
	},
	declinaisonClicked: function(event) {
		fillSmallModal("<div class='list_declinaisons_page'>"+this.options.list_declinaisons.html()+"</div>");
		this.initList();
		showSmallModal();
		//alert('** '+this.options.declinaison_link);
	},
	decInListClicked: function(event) {
		var artid = $('.declinaison_art_id', $(event.currentTarget)).html();
		closeSmallModal();
		document.location.href = html_entity_decode(this.options.declinaison_link)+artid;
	},
	imgLoaded: function(event) {
		this.options.img.css({'width': '232px', 'height': 'auto', 'max-height': 100000});
		if (this.options.img.height() > 187) {
			var width = intval(232 / this.options.img.height() * 187);
			//alert('width: '+width+' - height: '+img.height());
			this.options.img.css({'width': width+'px', 'height': '187px'});
		}
		//$('table.fiche_prod_img_table_container img', this.element).css('width', $('table.fiche_prod_img_table_container img', this.element).width()+'px');
	},
	videoClicked: function(event) {
		//this.options.video_div.css({'opacity': '0', 'display': 'block'});
		//this.options.video_div.fadeIn(400);
		if (this.options.video_div.css('display') == 'block') return false;
		this.options.video_div.css('display', 'block');
		/*var height = this.options.video_div.height();
		this.options.video_div.css('height', 0);
		this.options.video_div.animate({'height': height});*/
		$('html, body').animate({scrollTop: $(document).height() - $(window).height() }, 750, jQuery.proxy(this.startPlayer, this));
		
		return false;
	},
	startPlayer: function() {
		this.options.video_player[0].SetVariable("player:jsPlay", "");
	},
	options: {
		// This are not te be used as option parameters
		declinaison_div: null,
		declinaison_link: null,
		list_declinaisons: null,
		img: null,
		
		video_link: null,
		video_div: null,
		video_player: null
	}
});
