$(document).ready(function(){
	$('.select_box').selectbox();
	$('.select').selectbox({inputClass:'selectbox2',containerClass:'selectbox2-wrapper'});
	$('.select3').selectbox({inputClass:'selectbox3',containerClass:'selectbox3-wrapper'});
});

function ajax_request(url){
	 return $.ajax({url: url, async: false}).responseText;
}

function form2request(obj) {
  var getstr = "";
	var ipt_fields = obj.getElementsByTagName("input");
	for(i=0; i<ipt_fields.length; i++){
		if(ipt_fields[i].type == "text" || ipt_fields[i].type == "hidden" || ipt_fields[i].type == "password"){
			getstr += ipt_fields[i].name + "=" + encodeURIComponent(ipt_fields[i].value) + "&";
		}
		if(ipt_fields[i].type == "checkbox"){
			if(ipt_fields[i].checked){
				getstr += ipt_fields[i].name + "=" + encodeURIComponent(ipt_fields[i].value) + "&";
			}else{
				getstr += ipt_fields[i].name + "=&";
			}
		}
		if(ipt_fields[i].type == "radio"){
			if(ipt_fields[i].checked){
				getstr += ipt_fields[i].name + "=" + encodeURIComponent(ipt_fields[i].value) + "&";
			}
		}  
  }
	var ipt_fields = obj.getElementsByTagName("select");
	for(i=0; i<ipt_fields.length; i++){
		getstr += ipt_fields[i].name + "=" + encodeURIComponent(ipt_fields[i].options[ipt_fields[i].selectedIndex].value) + "&";
	}
	var ipt_fields = obj.getElementsByTagName("textarea");
	for(i=0; i<ipt_fields.length; i++){
		getstr += ipt_fields[i].name + "=" + encodeURIComponent(ipt_fields[i].value) + "&";
	}
	return getstr;
}

function nl2br(str, is_xhtml) {
	if(typeof is_xhtml != 'undefined' && !is_xhtml) breakTag = '<br>';
	else breakTag = '<br />';
  return (str + '').replace(/([^>]?)\n/g, '$1'+ breakTag +'\n');
}

function submit_form(formID){
	setTimeout('	\
	var form = document.getElementById(\''+formID+'\');	\
	if(!(form.onsubmit) || form.onsubmit()){	\
		if(form.submit.value) form.submit.click();	\
		else form.submit();	\
	}',0);
}

function sleep(delay){ 
	var start = new Date().getTime(); 
	while (new Date().getTime() < start + delay); 
}

function loc_replace(url){
	setTimeout('window.location.replace(\''+url+'\');',0);
}

function show_ifrm_dialog(link, title, maximized, height, width){
	if(typeof(title) == 'undefined') title = false;
	if(typeof(maximized) == 'undefined') maximized = true;
	if(typeof(height) == 'undefined') height = '60%';
	if(typeof(width) == 'undefined') width = false;
	modal_dialog({
		type:'iframe', 
		href:link, 
		width: (maximized?'90%':(width?width:500)), 
		height: height, 
		title: title, 
		position:'center',
		bgiframe:true,
		stack:true,
		zIndex: 5000 
	});
}

//modal_dialog({type:'img', href:'http://www.raymondselda.com/wp-content/uploads/2009/04/contactform.png'});
//modal_dialog({type:'iframe', href:'photos_ajout.php?tpl_inline=1', width: 740, height: 530});
var modal_dialog_opts = [];
function modal_dialog_init(modal_box_id){
	if(typeof(modal_box_id) == 'undefined') modal_box_id = '#modal_dialog_spc';
	//add modal dialog div
	if($(modal_box_id).attr('id') != modal_box_id.substr(1)){
		var $dialog_div = $('<div style="display:none;overflow:hidden;" id="'+modal_box_id.substr(1)+'"></div>');
		$('body').append( $dialog_div );
	}
}

function modal_dialog(opts){
	if(typeof(opts.modal_box_id) == 'undefined') opts.modal_box_id = '#modal_dialog_spc';

	modal_dialog_init(opts.modal_box_id);
	
	if(typeof(opts.width) != 'undefined' && typeof(opts.width) == 'string' && opts.width.substr(opts.width.length - 1, 1) == '%'){
		opts.width_percent = opts.width;
		opts.width = $(window).width() * (parseInt(opts.width.substr(0, opts.width.length - 1)) / 100);
	}
	if(typeof(opts.height) != 'undefined' && typeof(opts.height) == 'string' && opts.height.substr(opts.height.length - 1, 1) == '%'){
		opts.height_percent = opts.height;
		opts.height = $(window).height() * (parseInt(opts.height.substr(0, opts.height.length - 1)) / 100);
	}

	//alert(opts.type);
	//check which type of dialog is it
	switch(opts.type){
		case "img":
			if(typeof(opts.width) == 'undefined' || typeof(opts.height) == 'undefined'){
				var tmp_img = new Image();
				tmp_img.onload = function(){
					opts.width = this.width;
					opts.height = this.height;
					modal_dialog(opts);
				}
				tmp_img.src = opts.href;
				return;
			}
			$(opts.modal_box_id).html('<div align="center"><img src="'+opts.href+'" width="'+opts.width+'" height="'+opts.height+'" alt="" /></div>').width(opts.width).height(opts.height);
			opts.width += 20;
			opts.height = false;
		break;
		case "iframe":
			opts.width += 20;
			$(opts.modal_box_id).html('<iframe id="modal_dialog_ifrm" onreadystatechange="this.src = \''+opts.href+'\'; this.onreadystatechange = \'\';" src="'+opts.href+'" frameborder="0" style="border:0;width:'+opts.width+'px;height:'+opts.height+'px;" />').width(opts.width).height(opts.height);
			opts.width += 10;
			opts.height = false;
		break;
		case "html":
			if(typeof(opts.width) == 'undefined') opts.width = 500;
			$(opts.modal_box_id).html('<div style="width:'+opts.width+'px;'+(typeof(opts.height) != 'undefined'?('height:'+opts.height+'px;overflow:auto;'):'')+'">'+opts.html+'</div>').width(opts.width).height(opts.height);
		break;
	}

	opts.width += 20;
	opts.height = false;

	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	$(opts.modal_box_id).dialog("destroy");
	
	$(opts.modal_box_id).dialog({
		width: opts.width,
		modal: true,
		draggable: false,
		resizable: false,
		bgiframe: true,
		minHeight:10,
		stack:true
	});
	//$(".ui-widget-header").css({background:'none', border: 'none'});

	if(typeof(opts.title) != 'undefined') $(opts.modal_box_id).dialog("option", "title", opts.title);
	if(typeof(opts.top) != 'undefined' || typeof(opts.left) != 'undefined'){
		if(typeof(opts.top) == 'undefined') opts.top = 'top';
		if(typeof(opts.left) == 'undefined') opts.left = 'left';
		$(opts.modal_box_id).dialog("option", "position", [opts.left, opts.top]);
	}
	if(typeof(opts.buttons) != 'undefined') $(opts.modal_box_id).dialog("option", "buttons", opts.buttons);
	
	modal_dialog_opts = opts;
}

function modal_dialog_close(modal_box_id){
	if(typeof(modal_box_id) == 'undefined') modal_box_id = '#modal_dialog_spc';
	$(modal_box_id).dialog("destroy");
}


//simplify the command
function alertWindow(content, modal_box_id){
	if(typeof(modal_box_id) == 'undefined') modal_box_id = '#modal_dialog_spc';
	modal_dialog_init(modal_box_id);
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	$(modal_box_id).dialog("destroy");
	
	$(modal_box_id).html(nl2br(content));
	$(modal_box_id).dialog({
		width: 500,
		modal: true,
		draggable: false,
		resizable: false,
		bgiframe: true,
		minHeight:10,
		stack:true
	});	

	var buttons = {};
	buttons[lang_const.lang_alert_ok_btn] = function(){$(this).dialog('close');};
	$(modal_box_id).dialog("option", "buttons", buttons);
}

function alertWindowWithAction(content, callbackFunction, modal_box_id) {
	if(typeof(modal_box_id) == 'undefined') modal_box_id = '#modal_dialog_spc';
	modal_dialog_init(modal_box_id);
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	$(modal_box_id).dialog("destroy");
	
	$(modal_box_id).html(nl2br(content));
	$(modal_box_id).dialog({
		width: 500,
		modal: true,
		draggable: false,
		resizable: false,
		bgiframe: true,
		minHeight:10,
		stack:true
	});	

	var buttons = {};
	buttons[lang_const.lang_alert_ok_btn] = function(){
		$(this).dialog('close');
		if(typeof(callbackFunction) != 'undefined') callbackFunction(true);
	};
	$(modal_box_id).dialog("option", "buttons", buttons);
}

function confirmWindow(content, callbackFunction, modal_box_id){
	if(typeof(modal_box_id) == 'undefined') modal_box_id = '#modal_dialog_spc';
	modal_dialog_init(modal_box_id);
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	$(modal_box_id).dialog("destroy");
	
	$(modal_box_id).html(nl2br(content));
	$(modal_box_id).dialog({
		width: 500,
		modal: true,
		draggable: false,
		resizable: false,
		bgiframe: true,
		minHeight:10,
		stack:true
	});	

	var buttons = {};
	buttons[lang_const.lang_alert_ok_btn] = function(){
		$(this).dialog('close');
		if(typeof(callbackFunction) != 'undefined') callbackFunction(true);
	};
	buttons[lang_const.lang_confirm_cancel_btn] = function(){
		$(this).dialog('close');
		if(typeof(callbackFunction) != 'undefined') callbackFunction(false);
	};
	$(modal_box_id).dialog("option", "buttons", buttons);
}

function openPromptImage(src){modal_dialog({type:'img',href:src});}

function textToggle(id, prefix){
	if(typeof(prefix) == 'undefined') prefix = 'news';
	//show
	if($('#'+prefix+'_cut_'+id).is(':visible')){
		$('#'+prefix+'_all_'+id)
			.show()
			.height($('#'+prefix+'_cut_'+id).height());
		$('#'+prefix+'_cut_'+id).hide();
		$('#'+prefix+'_all_'+id).animate(
			{height: $('#'+prefix+'_all_'+id+' > span').height()}, 
			'normal'
		);
		$('#'+prefix+'_link_'+id).removeClass('read_link_down_arrow').addClass('read_link_up_arrow');  
	}
	//hide
	else{
		$('#'+prefix+'_cut_'+id).show();
		var height = $('#'+prefix+'_cut_'+id).height();
		$('#'+prefix+'_cut_'+id).hide();
		$('#'+prefix+'_all_'+id).animate(
			{height: height}, 
			'normal', 
			function(){
				$('#'+prefix+'_cut_'+id).show();
				$('#'+prefix+'_all_'+id).hide();
			}
		);
		$('#'+prefix+'_link_'+id).removeClass('read_link_up_arrow').addClass('read_link_down_arrow');  
	}
}

function newsToggle(id){
	//textToggle(id, 'news');
	if(typeof(prefix) == 'undefined') prefix = 'news';
	//show
	if($('#'+prefix+'_cut_'+id).is(':visible')){
		$('#'+prefix+'_all_'+id)
			.show()
			.height($('#'+prefix+'_cut_'+id).height());
		$('#'+prefix+'_cut_'+id).hide();
		$('#'+prefix+'_all_'+id).animate(
			{height: $('#'+prefix+'_all_'+id+' > span').height()}, 
			'normal'
		);
		$('#'+prefix+'_link_'+id).removeClass('read_link_down_arrow').addClass('read_link_up_arrow');  
	}
	//hide
	else{
		$('#'+prefix+'_cut_'+id).show();
		var height = $('#'+prefix+'_cut_'+id).height();
		$('#'+prefix+'_cut_'+id).hide();
		$('#'+prefix+'_all_'+id).animate(
			{height: height}, 
			'normal', 
			function(){
				$('#'+prefix+'_cut_'+id).show();
				$('#'+prefix+'_all_'+id).hide();
			}
		);
		$('#'+prefix+'_link_'+id).removeClass('read_link_up_arrow').addClass('read_link_down_arrow');  
	}
}
function productToggle(id){textToggle(id, 'product');}

function showProductTour(product_index, read_text) {
	var product_info_id = "product_info_"+product_index;
	var product_id = "product_"+product_index;
	var product_id_all = "product_"+product_index+"_all";
	var read_link_id = "read_link_"+product_index;
	if ( document.getElementById(product_id).style.display != 'none' ) {
		$('#'+product_id).slideUp(function() { 
			$('#'+product_info_id).css('height','auto');
			$('#'+product_id_all).slideDown();
			$('#'+read_link_id).removeClass('read_link_down_arrow');
			$('#'+read_link_id).addClass('read_link_up_arrow'); 
		});
	}
	if ( document.getElementById(product_id).style.display == 'none' ) {
		$('#'+product_id_all).slideUp(function() { 
			$('#'+product_info_id).css('height','149px');
			$('#'+product_id).slideDown();
			$('#'+read_link_id).removeClass('read_link_up_arrow');
			$('#'+read_link_id).addClass('read_link_down_arrow');  
		});
	}
}

function topSearchSubmit(frm){
	var val = $('#ipt_top_search').val().replace(/\s+/g, ' ');
	if(val != ' ') document.location.href = base_url+'search-'+val+'.html';
	return false;
}

function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}

//playerChangeThumbs('./xml/myPlayer2.xml')
function playerChangeThumbs(xmlFile){
	getFlashMovie("nb_header_flash").changeThumbs(xmlFile);
}

//playerPlayMovie('2');
function playerPlayMovie(clipNumber){
	getFlashMovie("nb_header_flash").playMovie(clipNumber);
}