$(document).ready( function() {
  $("#tabs").tabs();
  $('#switcher').themeswitcher({loadTheme:'Mint Choc'});
	/*---background tool--------------------------*/
	$('select#bkgopc').selectmenu({  
	     style:'dropdown', // popup  //change:function(){alert($(this).html()); }, 
   	   width:200, 
	 menuWidth:200 });
	$('select#bkgopc').change(function() {
			 $('#bgimg').fadeOut('300').attr('src','images/bkg/'+$(this).val()+'.jpg').fadeIn('500');
 			 var date = new Date();
 			 date.setTime(date.getTime() + (120 * 24 * 60 * 60 * 1000)); // 2 months
			 $.cookie('ocobyte_tv_bkg_img',$(this).val(),{ path: '/', expires: date });
  });
	var bkg = $.cookie('ocobyte_tv_bkg_img');
	if (bkg==null)  bkg = '005_auroraborealis';
	$('#bgimg').attr('src','images/bkg/'+bkg+'.jpg');
	$('#bg').fadeIn('2000');
	/* ----end backgroud tool ------------------- */
	/* ----dialog window ------------------------ */
	$('#change_theme').click(function() {
		$('#dialog').dialog('open');})
		.hover(function(){$(this).addClass("ui-state-hover");},function(){$(this).removeClass("ui-state-hover");})
		.mousedown(function(){$(this).addClass("ui-state-active");})
		.mouseup(function(){$(this).removeClass("ui-state-active");});
  /* --- end dialog w ------------------------- */
	/* --- dialog window ------------------------ */
   $("#dialog").dialog({
			bgiframe: true,
			resizable:false,
			autoOpen: false,
			height:   200,
			modal:    true,
			overlay: {
				backgroundColor: '#000',
			  opacity: 0.6
			},
			buttons: {
				'Close': function() {	$(this).dialog('close');},
			}
		});	
	/* -- end dialog w --------------------------- */
	/*--- checkbox Cat Update ------------------------- */
	$("form#ui input").filter(":checkbox,:radio").checkbox();
	$(".chk_label_cat input[type='checkbox']").click(function(){
			/* aqui va el post */
			var lbl = $(this).next();
       $.post("tv.categoria.php", {
			    c:$(this).attr('value'),          // category
			    v:($(this).attr('checked')?1:0),  // value to set
					i:$('#cat_can').attr('cid'),      // channel id
					o:'set'
			 }, function(data){ });
	});
	/*--- eliminar canal --------------------------*/
	$("a[opt='disablech']").live( 'click', function() {
			$.post("tv.usraction.php", { u:user, i:$(this).attr('cid'), o:'disch' },
         function(rdata){ alert(rdata); });
	});
	
	/*--- guardar canal ---------------------------*/
	$("a[opt='savech']").live( 'click', function() {
			$.post("tv.usraction.php", { u:user, i:$(this).attr('cid'), o:'savech' },
         function(rdata){ alert(rdata); });
	});
  /*--- categorize ------------------------------*/
	$("a[opt='categorize']").live( 'click', function() {
		// 1 recover the values for this channel
		$('#lang_select').val($(this).attr('lang')); // set ch lang 
		$('#cat_can').attr('cid' ,$(this).attr('cid'));
		 $.post("tv.categoria.php", {
				i:$('#cat_can').attr('cid'),      // channel id
				o:'get'
		 },
			 function(rdata){ 
			 $("form#ui input").filter(":checkbox").each(function() {  $(this).checkbox('uncheck'); });
			  var arrcat = rdata.split(' ');
				for ( var i in arrcat ){	$("input[value='"+arrcat[i]+"']").checkbox('check'); }
			 });
    // 2 open the window whit the new values
    $('#categorizar').dialog('open');
		});
	   $("#categorizar").dialog({
			bgiframe: true,
			resizable: true,
			autoOpen: false,
			height:280,
			width:450,
			modal: true,
			overlay: {
		  backgroundColor: '#000',
			opacity: 0.6
			},
			buttons: {
				'Close': function() {	$(this).dialog('close');},
			}
		});	
		 $('#lang_select').change(function(){
				 $.post("tv.categoria.php", {
				i:$('#cat_can').attr('cid'),      // channel id
				l:$('#lang_select').val(),
				o:'lang'
		 },function(rdata){ })
		});
	/*--- search toggle ---------------------------*/
	/* $('#search_box').filter(":checkbox,:radio").checkbox(); */
	$('#search_toggle').click(function(){
			if ($('#search_box').is(':hidden')){
				$(this).removeClass('ui-state-default').addClass('ui-state-active');
				$('#search_box').slideDown();
			}else{
				$(this).removeClass('ui-state-active').addClass('ui-state-default');
				$('#search_box').slideUp();
			}
	});
	$('#search_results').click(function (e) {
      e.preventDefault();
    	load_channels();
	});
	/*--- create/edit user ----------------------- */
	$('#user_form').click(function(){
			if ($('#edit_usr').is(':hidden')){
				if ($('#rec_user').is(':hidden')){}else{
				  $('#recover_form').removeClass('ui-state-active').addClass('ui-state-default'); $('#rec_user').slideUp();
				}
				$(this).removeClass('ui-state-default').addClass('ui-state-active');
				$('#edit_usr').slideDown();
			}else{
				$(this).removeClass('ui-state-active').addClass('ui-state-default');
				$('#edit_usr').slideUp();
			}
	});
  $('#recover_form').click(function(){
			if ($('#rec_user').is(':hidden')){
				if ($('#edit_usr').is(':hidden')){}else{ 
					 $('#user_form').removeClass('ui-state-active').addClass('ui-state-default');	$('#edit_usr').slideUp();
				}
				$(this).removeClass('ui-state-default').addClass('ui-state-active');
				$('#rec_user').slideDown();
			}else{
				$(this).removeClass('ui-state-active').addClass('ui-state-default');
				$('#rec_user').slideUp();
			}
	});
	$("#save_user").live( 'click', function() {
		 $.post("user.admin.php", {
		  i:$(this).attr('idc'),
  	  n:$("input[name='name']").val(),
		  e:$("input[name='email']").val(),
			o:'upd'
   },
	 function(rdata){ alert(rdata); });

	});
	/*--- Canales Guardados ---------------------------*/
	load_channels();
	 $("a[href=#tabs-2]").click(function(){
	 $.post("tv.load.php", {
		 i:'',
  	 u:user,
		 o:'recent'
   },
	 function(rdata){ $('#recently').html(rdata); });
	 });
	 /*--- Canales Favoritos --------------------- */
	 $("a[href=#tabs-3]").click(function(){
	 $.post("tv.load.php", {
		 i:'',
  	 u:user,
		 o:'saved'
   },
	 function(rdata){ $('#chsaved').html(rdata); });
	 });
	 /*--- Canales sin Categoria --------------------- */
	 $("a[href=#tabs-6]").click(function(){
	 $.post("tv.load.php", {
		 i:'',
  	 u:user,
		 o:'onnocat'
   },
	 function(rdata){ $('#onnocat').html(rdata); });
	 });
	 
	 /*---------------------------------------------*/
	 $("a.img_text").live( 'mouseover', function() { $(this).find("b").css('visibility','visible'); })
	                .live(  'mouseout', function() { $(this).find("b").css('visibility','hidden');  });
	/*--- the end ---------------------------------*/
});
/* ########################################################################## */
function load_channels(){
   var searchf = loadCats();
	 $.post("tv.load.php", {
		 i:'',
  	 s:searchf, // cat options
		 o:'main'
   },
	 function(rdata){ $('#channels').html(rdata); });
}
/* ########################################################################## */
function load_recently(){

}
/* ########################################################################## */
function loadCats(){
	var ret= '';
	if ( $.browser.msie ) {
		$("#search_box div span").each(function(){
				var data = $(this).html(); 
				if (data !=''){
					if (data.indexOf('aria-checked=true')>0){
							ret += " "+ data.substring(data.indexOf('value')+6,data.indexOf('value')+10); 
					}
				}
		});
		return ret;
	}	
	$("#search_box").find(":checkbox").each(function(){
			if ($(this).attr('checked')) ret += " "+$(this).attr('value');
	});
	return ret;
}
/* ########################################################################## */
function openWin(mypage,myname,w,h,options){
if ((!options)||(options==null)) alert('no options ERR:01138 ['+mypage+']');
  var help     = (options.toUpperCase().indexOf("HELP")      != -1)?'1':'0';
  var fullscr  = (options.toUpperCase().indexOf("FULLSCREEN")!= -1)?'yes':'0';
  var center   = (options.toUpperCase().indexOf("CENTER")    != -1)?'1':'0';
  var scroll   = (options.toUpperCase().indexOf("SCROLL")    != -1)?'1':'0';
  var toolbar  = (options.toUpperCase().indexOf("TOOLBAR")   != -1)?'1':'0';
  var location = (options.toUpperCase().indexOf("LOCATION")  != -1)?'1':'0';
  var status   = (options.toUpperCase().indexOf("STATUS")    != -1)?'1':'0';
  var menubar  = (options.toUpperCase().indexOf("MENUBAR")   != -1)?'1':'0';
  var resizable= (options.toUpperCase().indexOf("RESIZABLE") != -1)?'1':'0';
  var notitlebar=(options.toUpperCase().indexOf("NOTITLEBAR")!= -1)?'1':'0';//? no aplica a todos los browwsers, ie es <titlebar>
  var borde    = (options.toUpperCase().indexOf("BORDE")     != -1)?'Raised':'Sunken';
  var left     = 0;
  var top      = 0;
  var day      = new Date();
  var now      = day.getTime();
	if (fullscr=='yes'){w    = screen.width;	h = screen.height;}
	if (center)        {left = (screen.width  - w)/2; top = (screen.height - h)/2;	}
  settings = 'width='+w+',height='+h+',left='+left+',top='+top+',scrollbars='+scroll+', status='+status+',resizable='+resizable+',menubar='+menubar+',toolbar='+toolbar+',location='+location+',fullscreen='+fullscr;
  eval("window.page" + now + "=window.open(mypage,'"+myname+"','"+settings+"');"); // tiene que ser con eval!
  eval("window.page" + now + ".focus();"); // focus en la nueva pagina creada!!
}
