jQuery(function($)
{
	$('#im').Draggable(
	{
		opacity:0.5,onChange:function()
		{
			$('#im').show()
		}
		,onDrag:function(nx,ny)
		{
			cx=nx;
			cy=ny
		}
	}
	);
	$('input').attr('autocomplete','off');
	$('input[@type=text]').css('border','1px solid #AFAFAF');
	$('#panel_menu > div').click(function()
	{
		eval('switchTab('+this.id.substr(3)+')')
	}
	);
	$('#panel_menu2 > div').click(function()
	{
		eval('switchTab2('+this.id.substr(3)+')')
	}
	);
	$('#clear_bg').click(function()
	{
		result=null;
		image_src='';
		actual_width=actual_height=image_width=image_height=0;
		$('#im').attr('src','').hide();
		$('#wrapper').css('backgroundImage','');
		$('#upload_text').val('')
	}
	);
	$('#tables_caption').mouseover(function()
	{
		if(hover)
		{
			clearInterval(hover)
		}
		else $('#hover').fadeIn('slow')
	}
	).mouseout(function()
	{
		hover=setInterval("$('#hover').fadeOut('slow');clearInterval(hover);hover=null;",500)
	}
	);
	$('#premade_button').click(function()
	{
		var w=500;
		var h=screen.height-70;
		var l=screen.width-w-20;
		var t=Math.min(5,screen.height-h);
		window.open('/backgrounds-para-generadores/categorias','bglist','top='+t+',left='+l+',width='+w+',height='+h+',statusbar=0,resizable=0,scrollbars=1')
	}
	);
	if($.browser.mozilla)$('#fantom').css('left','351px');
	else if($.browser.msie)$('#fantom').css('left','375px');
	else if($.browser.opera)$('#fantom').css('left','370px');
	$('#fantom').change(function()
	{
		var res=f_re.test(this.value);
		if(!res)
		{
			alert('Bad file');
			return
		}
		loading_flag=1;
		$.blockUI('<img src="http://www.daleonda.com/plugins/hi5-contact-tables/images/waiting.gif"><br><br><img src="http://www.daleonda.com/plugins/hi5-contact-tables/images/stop_loading.gif" style="cursor:pointer" onclick="$.unblockUI()">');
		$('#upload_text').val(this.value);
		testupload()
	}
	);
	$(apply_img).load(function()
	{
		$('#im').css(
		{
			left:'0',top:'0'
		}
		).attr(
		{
			src:this.src,width:this.width,height:this.height
		}
		).show();
		cx=cy=0;
		image_src=this.src;
		image_width=actual_width=this.width;
		image_height=actual_height=this.height;
		wh=image_width/image_height;
		$('#a1').attr('checked','1');
		$('#wrapper').css(
		{
			backgroundImage:''
		}
		);
		if(waiting_flag)
		{
			$.unblockUI();
			waiting_flag=0
		}
		if(loading_flag)loading_flag=0
	}
	);
	$('#font_slider').Slider(
	{
		accept:'.slider_handle',fractions:10,onSlide:function(px,py,x,y)
		{
			font_size=10+Math.round((px/100)*10)+'px';
			$('#font_slider_label').html(font_size)
		}
		,values:[[100,0]]
	}
	);
	$('#effect_slider').Slider(
	{
		accept:'.slider_handle',fractions:9,onSlide:function(px,py,x,y)
		{
			effect_size=1+Math.round((px/100)*9);
			$('#effect_slider_label').html(effect_size+'px')
		}
		,values:[[0,0]]
	}
	);
	$('#border_slider').Slider(
	{
		accept:'.slider_handle',fractions:9,onSlide:function(px,py,x,y)
		{
			border_size=1+Math.round((px/100)*9)+'px';
			$('#border_slider_label').html(border_size);
			$('#tbl').css('borderWidth',border_size)
		}
		,values:[[0,0]]
	}
	);
	$('#font_color').click(function()
	{
		showColorPicker(this,document.getElementById('font_color_value'),false,false,false)
	}
	);
	$('#effect_color').click(function()
	{
		showColorPicker(this,document.getElementById('effect_color_value'),false,false,false)
	}
	);
	$('#border_color').click(function()
	{
		showColorPicker(this,document.getElementById('border_color_value'),document.getElementById('tbl'),'borderColor',false)
	}
	);
	$('#bg_color').click(function()
	{
		showColorPicker(this,document.getElementById('bg_color_value'),document.getElementById('bg'),'backgroundColor',false)
	}
	);
	$('input[@name=border]').click(function()
	{
		$('#tbl').css('borderStyle',this.value);
		border_style=this.value
	}
	);
	$("input[@name=text_align]").click(function()
	{
		text_align=this.value
	}
	);
	$("input[@name=effect]").click(function()
	{
		effect_type=this.value
	}
	);
	$("input[@name=size]").click(function()
	{
		if(!image_src)return;
		$('#wrapper').css(
		{
			backgroundImage:''
		}
		);
		switch(this.value)
		{
			case'actual':image_width=actual_width;
			image_height=actual_height;
			break;
			case'f_width':image_width=181;
			image_height=parseInt(181/wh,10);
			break;
			case'f_height':image_width=parseInt(185*wh,10);
			image_height=185;
			break;
			case'f_size':image_width=Math.min(181,parseInt(185*wh,10));
			image_height=Math.min(185,parseInt(181/wh,10));
			break;
			case'tiled':image_width=actual_width;
			image_height=actual_height;
			$('#im').hide();
			$('#wrapper').css(
			{
				backgroundImage:'url('+image_src+')'
			}
			);
			break;
			case's_width':image_width=181;
			image_height=actual_height;
			break;
			case's_height':image_width=actual_width;
			image_height=185;
			break;
			case's_size':image_width=181;
			image_height=185;
			break
		}
		if(this.value!='tiled')
		{
			$('#im').attr(
			{
				width:image_width,height:image_height
			}
			).css(
			{
				left:'0',top:'0'
			}
			).show();
			cx=cy=0
		}
		else $('#im').hide();
		image_size=this.value
	}
	);
	$('#delete_temp_button').click(function() 
	{
		del("borrar_temp");
	}
	);
	$('#delete_upload_button').click(function() 
	{
		del("borrar_upload");
	}
	);
	$('#add_button').click(function() 
	{
		add($('#add_text').val());
	}
	);
	$('#choose_font').click(function()
	{
		var w=650;
		var h=600;
		var l=screen.width-w-20;
		var t=Math.min(20,screen.height-h);
		window.open(root+'fonts/1.html','fontlist','top='+t+',left='+l+',width='+w+',height='+h+',statusbar=0,resizable=1,scrollbars=1')
	}
	);
	$('#w1,#w2,#w3,#w4,#w5,#w6,#w7,#w8').focus(function()
	{
		words_buffer=this.value;
		$(this).css('borderColor','#CF0606');
		return true
	}
	).blur(function()
	{
		$(this).css('borderColor','#AFAFAF');
		return true
	}
	);
	$('#upload_text,#friendid').focus(function()
	{
		$(this).css('borderColor','#CF0606');
		return true
	}
	).blur(function()
	{
		$(this).css('borderColor','#AFAFAF');
		return true
	}
	);
	$("input[@name=align]").click(function()
	{
		if(!image_src)return;
		switch(this.value)
		{
			case'left':cx=0;
			break;
			case'top':cy=0;
			break;
			case'right':cx=181-image_width;
			break;
			case'bottom':cy=185-image_height;
			break;
			case'center':cx=185-parseInt(image_width/2,10);
			cy=75-parseInt(image_height/2,10);
			break
		}
		$('#im').css(
		{
			left:cx+'px',top:cy+'px'
		}
		).show()
	}
	);
	$('#view').click(function()
	{
		generate(0)
	}
	);
	$('#live0,#live1,#live2').load(function()
	{
		if(top_image>=0)return;
		flag[this.id.substr(4,1)]=1;
		if(flag[0]&&flag[1]&&flag[2])showLive()
	}
	).click(function()
	{
		getCode(this.src)
	}
	).mouseover(function()
	{
		$(this).css('borderColor','#C80000')
	}
	).mouseout(function()
	{
		$(this).css('borderColor','#FFFFFF')
	}
	);
	tmp_image.onload=function()
	{
		showLive()
	};
	liveUpdate();
	setInterval('liveUpdate()',10000)
}
);
var root=$('base').attr('href');
var f_re=/^.+\.(gif|jpg|png|jpeg)$/i;
var u_re=/^http:\/\/.+\.(gif|jpg|png|jpeg)$/i;
var activeTab=1;
var apply_img=new Image();
var words_buffer='';
var waiting_flag=0;
var loading_flag=0;
var hover=null;
var flag=[0,0,0];
var images=[];
var images_len=0;
var images_curr=0;
var top_image=-1;
var transaction=0;
var tmp_image=new Image;
var bg_color='#DFDFDF';
var border_size='1px';
var border_style="none";
var font_size='16px';
var image_src='';
var actual_width=0;
var actual_height=0;
var image_width=0;
var image_height=0;
var font=1;
var cx=0;
var cy=0;
var wh=0;
var viejo='';
var image_size='actual';
var text_align='left';
var effect_type='shadow';
var effect_size='1px';
var result=null;
var preload=['waiting.gif','stop_loading.gif','hover.gif'];
var len=preload.length;
var images=new Array(len);
for(var i=0;i<len;i++)
{
	images[i]=new Image();
	images[i].src=root+'images/'+preload[i]
}
function switchTab2(n)
{
	if(activeTab==n)return;
	$('#panead'+activeTab).css('visibility','hidden');
	$('#tab'+activeTab).removeClass('selected');
	$('#panead'+n).css('visibility','visible');
	$('#tab'+n).addClass('selected');
	switch(n)
	{
		case 1:$('#panead1').css('height','178px').show('slow');
		break;
		case 2:$('#panead2').css('height','178px').show('slow');
		break;
	}
	activeTab=n
};
function switchTab(n)
{
	if(activeTab==n)return;
	$('#pane'+activeTab).css('visibility','hidden');
	$('#tab'+activeTab).removeClass('selected');
	$('#pane'+n).css('visibility','visible');
	$('#tab'+n).addClass('selected');
	switch(n)
	{
		case 1:$('#panel').css('height','271px').show('slow');
		break;
		case 2:$('#panel').css('height','291px').show('slow');
		break;
		case 3:$('#panel').css('height','161px').show('slow');
		break;
		case 4:$('#panel').css('height','120px').show('slow');
		break
	}
	activeTab=n
};
function fontCallback(n)
{
	font=n.substr(1);
	$('#font_sample').attr(
	{
		src:root+'images/fonts/'+font+'.gif'
	}
	)
}
function bgCallback(imsrc)
{
	apply_img.src=imsrc;
	$('#upload_text').val(imsrc)
}
function add(file)
{
	$.post("funciones.php",
	{
		tipo:"agregar",file:file
	}
	,function(msg)
	{
		result=eval('('+msg+')');
		alert(result.message)
	}
	)
}	
function del(type)
{
	$.post("funciones.php",
	{
		tipo:type
	}
	,function(msg)
	{
		result=eval('('+msg+')');
		alert(result.message)
	}
	)
}	
function update()
{
	$.post("http://www.daleonda.com/plugins/hi5-contact-tables/generate.php",
	{
		viejo:viejo,bg_color:$('#bg_color_value').val(),image_src:image_src,image_size:image_size,image_width:image_width,image_height:image_height,actual_width:actual_width,actual_height:actual_height,x:cx,y:cy,font_family:font,font_size:font_size,font_color:$('#font_color_value').val(),text_align:text_align,effect_type:effect_type,effect_size:effect_size,effect_color:$('#effect_color_value').val(),w1:$('#w1').val(),w2:$('#w2').val(),w3:$('#w3').val(),w4:$('#w4').val(),w5:$('#w5').val(),w6:$('#w6').val(),w7:$('#w7').val(),w8:$('#w8').val()
	}
	,function(msg)
	{
		result=eval('('+msg+')');
		if(result.success)
		{
			$('#tbl').html('<img src="'+root+result.message+'">');
			viejo=result.message
			image_src=result.image_src
		}
		else alert(result.message)
	}
	)
}
function generate(mode)
{
	if(!result||!result.success)return;
	var form=document.f_code;
	form.image.value=result.message;
	form.style.value=border_style;
	form.size.value=border_size;
	form.color.value=$('#border_color_value').val();
	if(mode==0)
	{
		form.target='_blank';
		form.action='http://www.daleonda.com/plugins/hi5-contact-tables/livepreview.php'
	}
	else
	{
		form.target='_self';
		form.action='http://www.daleonda.com/hi5-contact-tables/code.php'
	}
	form.submit()
}
function getCode(img)
{
	document.fcode.src.value=img;
	document.fcode.submit()
}
function testupload()
{
	$.ajaxUpload(
	{
		uploadform:document.f,url:root+'upload.php',secureuri:false,dataType:'json',success:function(msg)
		{
			if(!msg.success)
			{
				alert(msg.message);
				return
			}
			$('#im').css(
			{
				left:'0',top:'0'
			}
			).attr(
			{
				src:msg.image,width:msg.width,height:msg.height
			}
			).show();
			cx=cy=0;
			image_src=msg.image;
			image_width=actual_width=msg.width;
			image_height=actual_height=msg.height;
			image_size='actual';
			wh=image_width/image_height;
			$('#a1').attr('checked','1');
			$('#wrapper').css(
			{
				backgroundImage:''
			}
			);
			$.unblockUI();
			loading_flag=0
		}
	}
	)
}
function liveUpdate()
{
	if(transaction)return;
	transaction=1;
	if(!images_len)
	{
		$.get(root+"images.html",function(msg)
		{
			images=msg.split("|");
			images_len=images.length;
			images_curr=Math.round(Math.random()*(images_len-1));
			$('#live0').attr('src',root+'images/tables/'+images[images_curr%images_len]);
			$('#live1').attr('src',root+'images/tables/'+images[(images_curr+1)%images_len]);
			$('#live2').attr('src',root+'images/tables/'+images[(images_curr+2)%images_len]);
			images_curr++
		}
		)
	}
	else
	{
		top_image++;
		tmp_image.src=root+'images/tables/'+images[(images_curr+2)%images_len];
		images_curr++
	}
}
function showLive()
{
	if(top_image<0)
	{
		$('#live0').css('visibility','visible').fadeIn('slow',function()
		{
			$('#live1').css('visibility','visible').fadeIn('slow',function()
			{
				$('#live2').css('visibility','visible').fadeIn('slow',function()
				{
					transaction=0
				}
				)
			}
			)
		}
		)
	}
	else
	{
		$('#live'+(top_image%3)).hide('normal',function()
		{
			$('#live'+((parseInt(top_image)+1)%3)).animate(
			{
				top:8
			}
			,'slow',function()
			{
				$('#live'+((parseInt(top_image)+2)%3)).animate(
				{
					top:174
				}
				,'slow',function()
				{
					$('#live'+(top_image%3)).attr('src',tmp_image.src).css('top','340px').show('normal',function()
					{
						transaction=0
					}
					)
				}
				)
			}
			)
		}
		)
	}
}