var cookies = false;

$(document).ready(function(){

	if(typeof $.cookie !== 'undefined'){
	cookies = true;	
	}

	$('.panel form').submit(function(){

	var id = $(this).parents('.panel').attr('id');
	
		var ajax_options = { 
			type: 			"POST",
			url: 			'ajax_callback.php',
			data: 			$(this).serialize(),
			success: 		function(data, textStatus){
							update_panel(data, textStatus, id);
							}	
		};
			
		$.ajax(ajax_options);

		return false;
	
	});

	attach_number_of_feeds_event();
	
	/* fix IE bug - add active class with js */
	
	$('.top-panel-links').find('a:first').addClass('active');
	
		
	$('.top-panel-links a').mouseover(function(){

		if($(this).prev('img:hidden').length){

			var panel_id = $(this).parents('div.panel').attr('id'); 
		
			$('#'+panel_id+' a.active').removeClass('active');
			$(this).addClass('active');
		
			$('#'+panel_id+' .top-panel-links img:visible').hide();
			$(this).prev('img').show();
		
		}

	});
		
	
	$('.edit-panel').click(function(){
	
		$(this).parents('.panel').find('div.panel-main').slideDown('fast');
		$(this).parents('.panel').find('form').slideToggle('fast');
		return false;
		
	});
	
	/* expand panel */
	
	$('.toggle-panel').click(function(){
	
		var img = $(this).children('img');
	
		$(this).parents('.panel').find('div.panel-main').slideToggle('fast', function(){
			
			if(cookies){
			
			//remember in a cookie
			var parent_id = $(this).parents('.panel').attr('id');
			
			var collapsed_cookie = read_cookie($.cookie('collapsed'));
			
				switch($('#'+parent_id+' div.panel-main').css('display')){
					
					case 'block':
			
					collapsed_cookie[parent_id] = 0;
					img.attr('src', 'images/open_panel.gif');
					img.attr('height', 9);
					img.attr('width', 13);
					
					break;
					
					case 'none':
			
					collapsed_cookie[parent_id] = 1;
					img.attr('src', 'images/closed_panel.gif')
					img.attr('height', 13);
					img.attr('width', 9);			
					break;
					
				}	

			write_cookie('collapsed', collapsed_cookie);
			
			}		
				
		});

		return false;
		
	});	

	$("div.column").sortable(

	    { 
			revert: true, 
			scroll: true,
			connectWith: ['.column'],
			placeholder: 'ui-sortable-placeholder',
			handle: $('h2 span'),
			stop: update_positions,
			zIndex: 200,
			start: start_drag,
			appendTo: $('#container')
	    });

	$('#customize').click(function(){

		$('#top-form form').slideToggle('fast');
		return false;

	});

	$('#top-form form #cancel').click(function(){

		$('#top-form form').slideUp('fast');

	});

	$('#choose-colours label').click(function(){

		$('#choose-colours li.active').removeClass('active');
		$(this).parents('li').addClass('active');

	})
		
	$("#features > ul").tabs();
	
	if($.browser.msie){
	//$('#header img').ifixpng(); 
	$('#header img').css('display', 'block');
	}

	if(typeof $.fn.jclock !== 'undefined'){
		$('#jclock').jclock({
			timeNotation: '12h',
			am_pm: true
		});
	}






});

function start_drag(e, ui){
/*
if($.browser.msie){
	var margin = $(document).scrollTop()+20;
	ui.helper.css('margin-top', margin+'px');	
}*/
	
}

function update_positions(e, ui){
	
	if(!cookies){
		return true;
	}
	
	var panel_positions = read_cookie($.cookie('panel_positions'));
	
	var moved_panel_id = ui.item.attr('id');

	var moved_to_column_id = ui.item.parents('.column').attr('id');
	
	var order_in_column = 0;
	
	$('#'+moved_to_column_id+' .panel:not(.ui-sortable-helper)').each(function(){
		
		var panel_id = $(this).attr('id');
			
		panel_positions[panel_id] = moved_to_column_id+':'+order_in_column;
		
	order_in_column++;	
		
	})
	
//	write_cookie('panel_positions', panel_positions);
	position_modules();
	
}

function read_cookie(cookie){
	
	var cookie_array = new Array();
	
	if(cookie){
	
	var cookie_elements = cookie.split(';');
	
		for(i=0;i<=cookie_elements.length;i++){
	
			if(cookie_elements[i]){
		
				cookie_data = cookie_elements[i].split('=');
			
				var key = cookie_data[0];

				cookie_array[key] = cookie_data[1];
			
			}
		
		}
	
	}
	
	return cookie_array;
	
}

function write_cookie(name, cookie_array){
	
	var cookie_str = '';
	
	for (key in cookie_array){
		
		cookie_str += key+'='+cookie_array[key]+';';
		
	}
	
	$.cookie(name, cookie_str, { expires: 10000 });

}

function attach_number_of_feeds_event(){
	
	$('div.feed-top a').unbind('click');
	$('div.feed-top a').click(function(){
	
	if(cookies){
	feed_limits_cookie = read_cookie($.cookie('feed_limits'));	
	}

	var id = $(this).parents('.feed').attr('id');
	
	var current_limit = $('#'+id+' li:visible').length;
	
	switch($(this).attr('class')){
		
		case 'add':
	
		$('#'+id+' li:hidden:first').show();
		current_limit ++;
		
		break;
		
		case 'remove':
		
		$('#'+id+' li:visible:last').hide();
		current_limit --;
		
		break;
			
	}
	
// feed_limits_cookie[id] = current_limit;
	
//	if(cookies){
//	write_cookie('feed_limits', feed_limits_cookie);	
//	}

	return false;
		
	});
	
	
}

function update_panel(data, textStatus, id){
		
	$('#'+id+' .feed-output').html(data);

	$('#'+id+' form.edit-panel-form').slideUp();

	//reattach the events
	attach_number_of_feeds_event();
	
}



/* Custom Function for positioning the modules */
function position_modules(){

// DEFINETELY NEEDS OPTIMIZATION

//pare ta 3 div-columns
var columnArray = ['dnn_column1','dnn_column2','dnn_column3'];

var cookie_str = '';

//gia ka8e div-column
for ( var i=0, len=columnArray.length; i<len; ++i )
{
    
    cookie_str = cookie_str + columnArray[i]+':';

    var div = document.getElementById(columnArray[i]);
    var elms = div.getElementsByTagName("div");
      
    for(var j = 0, maxI = elms.length; j < maxI; j++) 
    {
   
       var elm = elms[j];

        if (elm.id.startsWith('panel-'))
		{//alert(elm.id);				
			if(jQuery.inArray(elm.id.substr(-4), panelIdArray ) > -1)
			{
      			 
			//	alert(cookie_str);
				
				var pos=cookie_str.indexOf(elm.id)
				   
				 cookie_str =cookie_str + elm.id+',';                                                
		   
			}     
		}
      
     }
     
     var lastchar=cookie_str.charAt(cookie_str.length-1);
     //alert(lastchar);
     if (lastchar!=':')
    {
     cookie_str = cookie_str.substring(0, cookie_str.length-1);
     
     
     }
      cookie_str =cookie_str + '&';
      
}//end for
    
      cookie_str = cookie_str.substring(0, cookie_str.length-1);
  

//edw grafw to cookie
 $.cookie('position_cookie', cookie_str,{ expires: 1000000 })
 var asxetolinks = $.cookie('position_cookie')
//alert(asxetolinks);

}



function readPanelPositions()
{
//$.cookie("position_cookie", null);

	// Read the cookie with the positions (in the form of: "dnn_column1:panel1,panel2,...,paneln&dnn_column2:panel5,panel6,...,paneln&dnn_column3:panel3,panel4,...,paneln")
	var position_string = $.cookie('position_cookie');
	//alert(position_string);
	if(position_string != null)
	{
	// Set column_string to an array with the columns (e.g. 0:dnn_column1, 1:dnn_column2, 3:dnn_column3)
		var column_string = position_string.split("&");
		
		// loop through all the columns
		for(i=0;i<column_string.length;i++)
		{
		
		
			//if(column_string[i] == 'undefined')
		   // {
				   //alert(i);
			  //  alert(column_string[i]);
			
		   // }
			//cookies = true;	
			//}
			////if(column_string[i])//p.x. dnn_column1:panel1,panel2,...,paneln
			
		   // //{
				// set panel_string to the i-th column's order (e.g. dnn_column1:panel1,panel2,panel6)
				var panel_string=column_string[i];
				// set panel_array to 0:column name(e.g. dnn_column1) and 1:panels (e.g. panel1,panel2,panel3)
				var panel_array = panel_string.split(":");
				// get the column anem (e.g. dnn_column1)
				var column = panel_array[0]; //dnn_column1
				// if there are panels in the string	
				if(panel_array[1] != "")
				{	//set panel to each panel's name
					var panel=panel_array[1].split(",");
					//for each panel apply the order
					for(j=0;j<panel.length;j++)//panel1
					{
						$("#"+column).append($("#"+panel[j]));
					   // alert(panel[j])											   				 	
					}
				}
			}
		}
	
}
