function initialization(){
	
	var hidden_ids = [];
	var collapsed_ids = [];
	
	
	$(".pollsubmit input").button({disabled: true});
	
	$( ".stickynews img" ).addClass( "ui-corner-all" ).end();
	$( ".box_pictures a img" ).addClass( "ui-corner-all" ).end();
	
	$(".sliderportlet").css({ visibility: 'visible'});
	
	$( ".portlet" ).addClass( "ui-widget ui-helper-clearfix" ) //ui-corner-all
		.find( ".portlet-header" )
		.addClass( " move_cursor" )	//ui-corner-top
		.end()
		.find( ".portlet-content" );
		
	$( ".portlet-header a" ).append( "<span class='arrows.png'></span>")
		.end();
	
	$( ".ads_portlet" ).addClass( "ui-widget-content-ads" ).end(); //ui-corner-all
	
	//$( ".portlet-content" ).addClass( "ui-widget-content ui-helper-clearfix ui-corner-bottom" ).end();
	
	$("#generalcontainer .close").find(".portlet-header")
		.prepend( "<span style='float:left' class='ui-icon ui-icon-closethick'></span>")
		.end();
		
	$("#generalcontainer .fixed").find(".portlet-header")
		.removeClass("move_cursor")
		.end();
	
	$("#generalcontainer .collapse").find(".portlet-header")
		.prepend( "<span style='float:left' class='ui-icon ui-icon-triangle-1-s'></span>")
		.end();
	
	$("#generalcontainer .addremove strong").each(function(){
		$(this).append( "<span style='float:left' class='ui-icon ui-icon-plusthick'></span>");
		$(this).append( "<span style='float:left' class='ui-icon ui-icon-minusthick'></span>");
	});
	
		
	$( ".portlet-header .ui-icon-triangle-1-s,.ui-icon-triangle-1-s" ).click(function() {
		var timestamp = Math.round((new Date()).getTime() / 1000);
	
		$( this ).toggleClass( "ui-icon-triangle-1-s" ).toggleClass( "ui-icon-triangle-1-e" );
		$( this ).parents( ".portlet:first" ).find( ".portlet-content" ).toggle('blind');

		$(".portlet .ui-icon-triangle-1-e").each(function(){
			var id = $(this).parent().parent().attr('id');		//get the id of each collapsed box
			collapsed_ids.push(id);				//push them inside an array
	
		});
		
		if(collapsed_ids){
			var collapsed = collapsed_ids.join(",");
			
			collapsed_ids = [];
			
			$.setSubCookie("collapsed","items",collapsed); //set the cookies
		}
		
	});
	
	$(".breaking_news").find("ul li").each(function(){
		$(this).prepend( "<span class='ui-icon ui-icon-alert'></span>")
		.end();
	});
	
	$(".livenewsheader").each(function(){
		$(this).prepend( "<span class='ui-icon ui-icon-signal-diag'></span>")
		.end();
	});
	
	$(".livenews").find('h4').each(function(){
		$(this).prepend( "<span class='ui-icon ui-icon-comment'></span>")
		.end();
	});
	
	//closing the boxes
	$( ".portlet-header .ui-icon-closethick" ).click(function() {
		var id =  $(this).parent().parent().addClass('hidden'); //add hidden class to the portlet
		var timestamp = Math.round((new Date()).getTime() / 1000);
		
		$(this).parent().parent().toggle("clip",600);		//hide the box
		
		//get the portlets that has hidden classes and save them inside cookie
		$("#generalcontainer .hidden").each(function(){
			
			var id = $(this).attr('id');		//get the id of each hidden box
			
			hidden_ids.push(id);				//push them inside an array
		});
		
		if(hidden_ids){
		
			var hidden = hidden_ids.join(",");
			hidden_ids = [];
			
			$.setSubCookie("hidden","items",hidden); //set the cookies
			$.setSubCookie("hidden","hiddenstamp",timestamp);
		}
		
	});
	
	$( ".portlet-content .ui-icon-minusthick" ).click(function() {
		var last = [];
		//get the limit set by the admin
		var max = $(this).parents( ".portlet:first" ).attr('max');
		
		//get the id of portlet
		var id = $(this).parents( ".portlet:first" ).attr('id');
		
		//get the total number of nodes (li)
		var node = $(this).parents( ".portlet:first" ).find('li');
		
		var length = node.length;
		
		//total length
		if($(this).parents( ".portlet:first" ).find('ul').attr('total')){
			total = $(this).parents( ".portlet:first" ).find('ul').attr('total');
		}else{
			total = 0;
		}
		
		total = parseInt(total);
		
		length = length - total;
		
		if(length>2){
			fin = total + 1;
			
			var hidden = node.length - fin;	//get the number of hidden lines
			
			$(this).parents( ".portlet:first" ).find('ul').attr('total',fin);
			
			$(node[hidden]).hide();
			
			//get the old cookie value
			
			$('.portlet').each(function(){
				portalid = $(this).attr('id');
				
				portalid = parseInt(portalid);
				
				portalnode  = $(this).find('li');
				
				portallength = portalnode.length;
				
				if($(this).find('ul').attr('total')){
					portaltotal = $(this).find('ul').attr('total');
				}else{
					portaltotal = 0;
				}
				
				if(portalid == id){
					last.push("#"+id+","+fin);
					
				}else{
				
					if(portaltotal>0){
						last.push("#"+portalid+","+portaltotal);
					}
				}
			});
			
			values = last.join("|");
			
			$.setSubCookie("less","items",values);
		}
	});
	
	$( ".portlet-content .ui-icon-plusthick" ).click(function() {
		var last = [];
		//get the limit set by the admin
		var max = $(this).parents( ".portlet:first" ).attr('max');
		
		//get the id of portlet
		var id = $(this).parents( ".portlet:first" ).attr('id');
		
		//get the total number of nodes (li)
		var node = $(this).parents( ".portlet:first" ).find('li');
		
		var length = node.length;
		
		//total hidden number
		if($(this).parents( ".portlet:first" ).find('ul').attr('total')){
			total = $(this).parents( ".portlet:first" ).find('ul').attr('total');
		}else{
			total = 0;
		}
		
		
		if(total>0){
			fin = total -1;
			if(fin <1){
				fin = 0;
			}
			var ind = length - total;
			
			$(this).parents( ".portlet:first" ).find('ul').attr('total',fin);
			
			$(node[ind]).show();
			
			var hidden = node.length - fin;	//get the number of hidden lines
			
			//get the old cookie value
			
			$('.portlet').each(function(){
				portalid = $(this).attr('id');
				
				portalid = parseInt(portalid);
				
				portalnode  = $(this).find('li');
				
				portallength = portalnode.length;
				
				if($(this).find('ul').attr('total')){
					portaltotal = $(this).find('ul').attr('total');
				}else{
					portaltotal = 0;
				}
				
				if(portalid == id){
					last.push("#"+id+","+fin);
					
				}else{
				
					if(portaltotal>0){
						last.push("#"+portalid+","+portaltotal);
					}
				}
			});
			
			values = last.join("|");
			
			$.setSubCookie("less","items",values);
			
		}
	});
	
	$(".ui-icon").each(function(){
		$(this).hover(function(){
			$(this).css({cursor: 'pointer'});
		});
	});

}

function sortingLeft(){

	var ids = [];
		
	$( "#leftcategoriescontainer" ).sortable({
		
		connectWith: ".left,.right",
		revert: true, 
		cancel : '.fixed', //keep
		handle: "h3",
		helper: 'clone',
		opacity: 0.6,//keep
		forcePlaceholderSize: true,
		dropOnEmpty: false,
			
		update: function (event,ui){
			var timestamp = Math.round((new Date()).getTime() / 1000);
			
			$(".left .portlet").each(function(k,v){
				
				var id = $(this).attr('id');	//the id of the box
					
				if($(this).hasClass('fixed')){	//check if the box is fixed
				
					var priority = $(this).attr('priority');	//get the box priority
						
					var left = $(".left .portlet");		// the left container
						
					priority = parseInt(priority);		//convert it into integer
					
					i = priority - 1;
						
					var current  = $(left[i]).attr('id');	//get the current box id
					
					var zero = $(left[0]).attr('id');		// get the 1st box id
					
					if(current && current!=id){				// avoid that the current fix box disappear
						
						if(zero !=id){				// disabled moving the first box downside the fixed box
							$('#'+current).before($('#'+id)); //id before current
						}else{
							$('#'+current).after($('#'+id));
						}
					}
				}	
			});
				
			$("#leftcategoriescontainer .portlet").each(function(k,v){		// sort the ids inside an array
				var id = $(this).attr('id');
				if(id){
					ids.push(id);
				}
			});
			
				
			var fin = ids.join(',');
			ids = [];
			
			$.setSubCookie("order","left",fin); //set the cookies
			$.setSubCookie("order","left_stamp",timestamp);
			
			var opposite = [];
			
			$("#rightcategoriescontainer .portlet").each(function(k,v){		// sort the ids inside an array
				var id = $(this).attr('id');
				if(id){
					opposite.push(id);
				}
			});
			var arr = opposite.join(',');
			
			opposite = [];
			
			$.setSubCookie("order","right",arr); //set the cookies
			$.setSubCookie("order","right_stamp",timestamp);
		}
	});
}

function sortingRight(){

	var ids = [];
		
	$( "#rightcategoriescontainer" ).sortable({
		
		connectWith: ".left,.right",
		revert: true, 
		cancel : '.fixed', //keep
		handle: "h3",
		helper: 'clone',
		opacity: 0.6,//keep
		forcePlaceholderSize: true,
		dropOnEmpty: false,
			
		stop: function (event,ui){
			var timestamp = Math.round((new Date()).getTime() / 1000);
			
			$(".right .portlet").each(function(k,v){
					
				var id = $(this).attr('id');	//the id of the box
					
				if($(this).hasClass('fixed')){	//check if the box is fixed
						
					var priority = $(this).attr('priority');	//get the box priority
						
					var right = $(".right .portlet");		// the right container
						
					priority = parseInt(priority);		//convert it into integer
					
					i = priority - 1;					
						
					var current  = $(right[i]).attr('id');	//get the current box id
						
					var zero = $(right[0]).attr('id');		// get the 1st box id
					
					if(current!=id){				// avoid that the current fix box disappear
						
						if(zero !=id){				// disabled moving the first box downside the fixed box
							$('#'+current).before($('#'+id)); //id before current
						}else{
							$('#'+current).after($('#'+id));
						}
					}
				}	
			});
				
			$("#rightcategoriescontainer .portlet").each(function(k,v){		// sort the ids inside an array
				var id = $(this).attr('id');
				
				if(id){
					ids.push(id);
				}
			});
			
				
			var fin = ids.join(',');
			ids = [];
			
			$.setSubCookie("order","right",fin); //set the right cookies
			$.setSubCookie("order","right_stamp",timestamp);
			
			var opposite = [];
			
			$("#leftcategoriescontainer .portlet").each(function(k,v){		// sort the ids inside an array
				var id = $(this).attr('id');
				if(id){
					opposite.push(id);
				}
			});
			var arr = opposite.join(',');
			
			opposite = [];
			
			$.setSubCookie("order","left",arr); //set the cookies
			$.setSubCookie("order","left_stamp",timestamp);
		}
	});
}


function sortingCenter(){

	var ids = [];
		
	$( "#centercategoriescontainer" ).sortable({
		
		connectWith: ".center",
		revert: true, 
		cancel : '.fixed', //keep
		handle: "h3",
		helper: 'clone',
		opacity: 0.6,//keep
		forcePlaceholderSize: true,
		dropOnEmpty: false,
			
		stop: function (event,ui){
				var timestamp = Math.round((new Date()).getTime() / 1000);
				
			$(".center .portlet").each(function(k,v){
					
				var id = $(this).attr('id');	//the id of the box
					
				if($(this).hasClass('fixed')){	//check if the box is fixed
					
					var priority = $(this).attr('priority');	//get the box priority
					
					var center = $(".center .portlet");		// the center container
						
					priority = parseInt(priority);		//convert it into integer
					
					i = priority - 1;					
						
					var current  = $(center[i]).attr('id');	//get the current box id
						
					var zero = $(center[0]).attr('id');		// get the 1st box id
					
					if(current!=id){				// avoid that the current fix box disappear
						
						if(zero !=id){				// disabled moving the first box downside the fixed box
							$('#'+current).before($('#'+id)); //id before current
						}else{
							$('#'+current).after($('#'+id));
						}
					}
				}	
			});
				
			$("#centercategoriescontainer .portlet").each(function(k,v){		// sort the ids inside an array
				var id = $(this).attr('id');
				if(id){
					ids.push(id);
				}
			});
			
				
			var fin = ids.join(',');
			
			ids = [];
			
			$.setSubCookie("order","center",fin); //set the cookies
			$.setSubCookie("order","center_stamp",timestamp); //set the cookies
		}
	});
}

function vote(){
	
	$('.pollradio').change(function (){
		$(".pollsubmit input").button({disabled: false});
	});
	
	$( ".progressbar" ).each(function(){
		var percentage = $(this).attr('perc');
			perc = parseInt(percentage);
		$(this).progressbar({value : perc});
	});
	
	$('.pollsubmit input').click(function(){
		
		$('.pollradio').each(function (){
		
			if($(this).is(":checked")){
				
				var vote = $(this).val();
				var pollid = $(this).parents("#pollform").find('.hiddenvote').attr('pollid');
				
				$(".pollsubmit input").button({disabled: true});
				
				var url = "poll.php?do=vote";
				
				$.ajax({
					type: 'POST',
					url: url,
					data: 	{	'vote'	: vote,
								'pollid': pollid
							},
					success: function(msg){ 
						$("#pollform").html(msg); 
						
						$( ".progressbar" ).each(function(){
							var percentage = $(this).attr('perc');
								perc = parseInt(percentage);
							$(this).progressbar({value : perc});
							$(".ui-progressbar-value").each(function(){
								var perc = $(this).parent().attr('perc');
								$(this).css({background:'#cc'+perc+'11'});
							});
						});
										
					}
				
				});
			}
		
		});
	
	});
	
	$(".ui-progressbar-value").each(function(){
		var perc = $(this).parent().attr('perc');
		var id = $(this).parent().attr('id');
		var c = 254- perc;
		var p = c - perc;
		
		var d = 'rgb('+id+perc+','+p+','+c+')';
	
		$(this).css({background: d});
	});

}


(function($) {
	$.fn.capslide = function(options) {
		var opts = $.extend({}, $.fn.capslide.defaults, options);
		return this.each(function() {
			$this = $(this);
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			
			if(!o.showcaption)	$this.find('.ic_caption').css('display','none');
			else $this.find('.ic_text').css('display','none');
				
			var _img = $this.find('img:first');
			var w = _img.css('width');
			var h = _img.css('height');
			$('.ic_caption',$this).css({'color':o.caption_color,'background-color':o.caption_bgcolor,'bottom':'0px','width':w});
			$('.overlay',$this).css('background-color',o.overlay_bgcolor);
			$this.css({'width':w , 'height':h, 'border':o.border});
			$this.hover(
				function () {
					if((navigator.appVersion).indexOf('MSIE 7.0') > 0)
					$('.overlay',$(this)).show();
					else
					$('.overlay',$(this)).fadeIn();
					if(!o.showcaption)
						$(this).find('.ic_caption').slideDown(500);
					else
						$('.ic_text',$(this)).slideDown(500);	
				},
				function () {
					if((navigator.appVersion).indexOf('MSIE 7.0') > 0)
					$('.overlay',$(this)).hide();
					else
					$('.overlay',$(this)).fadeOut();
					if(!o.showcaption)
						$(this).find('.ic_caption').slideUp(200);
					else
						$('.ic_text',$(this)).slideUp(200);
				}
			);
		});
	};
	$.fn.capslide.defaults = {
		caption_color	: 'white',
		caption_bgcolor	: 'black',
		overlay_bgcolor : 'blue',
		border			: '1px solid #fff',
		showcaption	    : true
	};
})(jQuery);


function gallery(){
	$("#photo_gallery").capslide({
		caption_color	: '#CC0000',
		caption_bgcolor	: 'white',
		overlay_bgcolor : '#F0B2B2',
		border			: '19px solid white',
		showcaption	    : true
	});
	
	$("#video_gallery").capslide({
		caption_color	: '#CC0000',
		caption_bgcolor	: 'white',
		overlay_bgcolor : '#F0B2B2',
		border			: '19px solid white',
		showcaption	    : true
	});
	
	$("#audio_gallery").capslide({
		caption_color	: '#CC0000',
		caption_bgcolor	: 'white',
		overlay_bgcolor : '#F0B2B2',
		border			: '19px solid #FCFAFA',
		showcaption	    : true
	});
	
}

function newsSlider(){
	$('#newsslider').accessNews({
		speed : "slow",
		slideBy : 5,
		slideShowInterval: 10000,
		slideShowDelay: 10000
	});
}

function anim(){
	$( ".breaking_news h3" ).animate({
			backgroundColor: "#FF1919",
			color: "#ffffff"
	}, 1400 );
	
	$( ".breaking_news h3" ).animate({
			backgroundColor: "#CC0000",
			color: "#F4F2F2"
	}, 500 );
}

function accord(){
	$( "#accordion" ).accordion({
		clearStyle: true
	});
	
	$("#accordion ul li").css({marginRight: '-25px'});
}

function pageDesign(){
	
	$(".page_button").click(function(){
		$( "#page_design" ).dialog( "destroy" );
		$( "#page_design" ).html('');
		
		$('.portlet').each(function(){
			var id = $(this).attr('id');
			
			var title = $(this).find(".portlet-header").text();
			if($(this).hasClass('close')){
				var htm = $( "#page_design" ).html();
				if($(this).css("display") !=='none'){
					var checkbox = "<input type='checkbox' class='sections' value='"+id+"' checked='checked' />";
				}else{
					var checkbox = "<input type='checkbox' class='sections' value='"+id+"' />";
				}
				var div	="<div><label>"+title+"</label>"+checkbox+"</div>";
				$( "#page_design" ).html(htm+div);
			}
			
		});
		
		var dialog_title= $("#page_design").attr('title');
		var dialog_save	= $("#page_design").attr('save');
		var dialog_reset= $("#page_design").attr('res');
		
		
		$( "#page_design" ).dialog({
				resizable: false,
				title: dialog_title,
				minHeight:190,
				width: 400,
				modal: true,
				draggable: false,
				buttons: [
							{
								text: dialog_reset,
								click: function() { 
										if($.cookie("order")){
											$.removeCookie("order");
										}
											
										if($.subCookie("hidden","items")){
											$.removeSubCookie("hidden","items");
										}
										
										if($.subCookie("collapsed","items")){
											$.removeSubCookie("collapsed","items");
										}
										
										if($.subCookie("less","items")){
											$.removeSubCookie("less","items");
										}
										
										$(this).dialog("close");
										
										window.location= $(location).attr('href');
								}
							},
							{
								text: dialog_save,
								click: function() { 
									hidden_ids = [];
									$('#page_design input.sections').each(function(){
										if($(this).is(':checked')){
											
										}else{
											var id = $(this).val();
											var timestamp = Math.round((new Date()).getTime() / 1000);
											hidden_ids.push(id);				//push them inside an array
										}
									});
									
									if(hidden_ids.length>0){
										var hidden = hidden_ids.join(",");
											hidden_ids = [];
													
											$.setSubCookie("hidden","items",hidden); //set the cookies
											//$.setSubCookie("hidden","hiddenstamp",timestamp);
									}else{
										if($.subCookie("hidden","items")){
											$.removeSubCookie("hidden","items");
										}
									}
									$(this).dialog("close");
									
									window.location= $(location).attr('href');
									
								}
							}
						]
				
		});
		
	});
}

function misc(){
	$("#history").click(function(){
		var url = $("#history div.text strong a").attr('href');
		window.location = url;
	});
	
	$("#charbelkhalil").click(function(){
		var url = $("#charbelkhalil div.text a").attr('href');
		window.location = url;
	});
}

function youlouk(){
	var headID = document.getElementsByTagName("head")[0];         
	var newScript = document.createElement('script');
	newScript.type = 'text/javascript';
	newScript.src = 'http://youlouk.com/ads/d/13';
	headID.appendChild(newScript);
}

function tooltip(){

	var leftcookies		= $.subCookie("order","left");
	var rightcookies	= $.subCookie("order","right");
	var centercookies	= $.subCookie("order","center");
		
	if(!centercookies && !rightcookies && !leftcookies){
		
		$('.portlet').each(function(k,v){
			if($(this).hasClass('fixed')){
					
			}else{
				$(this).find("h3").tipTip();
			}
			
		});
	}
	
	$('.page_button').tipTip();
}

function tabs(){
	
	if($(".bbdisabled").attr('title')=='empty'){
		$("#basketballtabs").tabs({ disabled : [0], selected: 1});
	}else{
		$("#basketballtabs").tabs();
	}
	
	if($(".disabled").attr('title')=='empty'){
		$("#sportstabs").tabs({ disabled : [0], selected: 1});
	}else{
		$("#sportstabs").tabs();
	}
	
	$("#generaltabs").tabs();

	
	if($(".disabled").attr('title')=='empty'){
		if($(".bbdisabled").attr('title')=='empty'){
			$("#sportstabs").tabs({ disabled : [0], selected: 1});
		}else{
			$("#generaltabs").tabs({ selected: 1});
			$("#basketballtabs").tabs({ disabled : [0], selected: 1});
		}
	}
}


$(document).ready(function(){
	newsSlider();
	initialization();
	sortingLeft();
	sortingRight();
	sortingCenter();
	//accessCookies();
	tabs();
	vote();
	accord();
	gallery();
	youlouk();
	//setInterval('anim()',1900);
	pageDesign();
	misc();
	tooltip();
});

