$(document).ready(function() {
    // Search input text handling on focus
		var $searchq = $("#search-q").attr("value");
	    $('#search-q.text').css('color', '#999');
		$('#search-q').focus(function(){
			if ( $(this).attr('value') == $searchq) {
				$(this).css('color', '#555');
				$(this).attr('value', '');
			}
		});
		$('#search-q').blur(function(){
			if ( $(this).attr('value') == '' ) {
				$(this).attr('value', $searchq);
				$(this).css('color', '#999');
			}
		});
	// Switch categories
		$('#h-wrap').hover(function(){
				$(this).toggleClass('active');
				$("#h-wrap ul").css('display', 'block');
			}, function(){
				$(this).toggleClass('active');
				$("#h-wrap ul").css('display', 'none');
		});
	// Handling with tables (adding first and last classes for borders and adding alternate bgs)
		$('tbody tr:even').addClass('even');
		$('table.grid tbody tr:last-child').addClass('last');
		$('tr th:first-child, tr td:first-child').addClass('first');
		$('tr th:last-child, tr td:last-child').addClass('last');
		$('form.fields fieldset:last-child').addClass('last');
	// Handling with lists (alternate bgs)
		$('ul.simple li:even').addClass('even');
	// Handling with grid views (adding first and last classes for borders and adding alternate bgs)
		$('.grid .line:even').addClass('even');
		$('.grid .line:first-child').addClass('firstline');
		$('.grid .line:last-child').addClass('lastline');
	// Tabs switching
		$('#box1 .content#box1-grid').hide(); // hide content related to inactive tab by default
		$('#box1 .header ul a').click(function(){
			$('#box1 .header ul a').removeClass('active');
			$(this).addClass('active'); // make clicked tab active
			$('#box1 .content').hide(); // hide all content
			$('#box1').find('#' + $(this).attr('rel')).show(); // and show content related to clicked tab
			return false;
		});
		
		$(function() {
			$(".column").sortable({
				connectWith: '.column',
				tolerance: 'pointer',
				stop: function() {
				  var submiturl = $("#widgetssubmiturl").text();
				  var i = 0;
				  $(".column").each(function(){
					  var widgets = $(this).sortable("toArray");
            var encoded = $.toJSON(widgets);
            
            jQuery.post(submiturl+"?column="+i+"&data="+encoded, function(data) {
            });
            i++;
          });
				}
			});

			$(".portlet").addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
				.find(".portlet-header")
					.addClass("ui-widget-header ui-corner-all")
					.prepend('<span class="button ui-icon ui-icon-minusthick">-</span>')
					.prepend('<span class="button ui-icon-xthick">x</span>')
					.end()
				.find(".portlet-content");

			$(".portlet-header .ui-icon").click(function() {
				$(this).toggleClass("ui-icon-minusthick").toggleClass("ui-icon-plusthick");
				$(this).parents(".portlet:first").find(".portlet-content").toggle();
			});

			$(".column").disableSelection();
		});
		
		$(".portlet-header .ui-icon-xthick").click(function()
		{
			$(this).parents(".portlet:first").remove();
			
			$(this).parents(".column:first").sortable("refresh");
		});
		
		$("#addwidgetbutton").click(function()
		{
		  var url = $("#widgetaddurl").text();
		  var widget_id = $("#widget").val();
		  
		  jQuery.get(url+"?widgetid="+widget_id, function(data) {
          jQuery.facebox(data)
      })
		});
});


