window.addEvent("load",function(){	
	Fx.Opacity = Fx.Style.extend({initialize: function(el, options){this.now = 1;this.parent(el, 'opacity', options);},toggle: function(){return (this.now > 0) ? this.start(1, 0) : this.start(0, 1);},show: function(){return this.set(1);}}); 
	
	$$('.gk_news_mwrap').each(function(el,i){
		var ID = el.getProperty('id');
		var main = $(ID);
		var $G = $Gavick[ID];
		var settings_blocks = $ES('.gk_news_settings',main);
		var categories_blocks = $ES('.gk_news_categories',main);
		var toggle_buttons = $ES('.gk_news_section',main);
		var options_buttons = $ES('.gk_news_options',main);
		var submit_buttons = $ES('.gk_news_form_submit',main);
		var cancel_buttons = $ES('.gk_news_form_cancel',main);
		var reset_buttons = $ES('.gk_news_form_reset',main);
		var categories_fx = [];
		var settings_fx = [];
		// sorting variables
		var sorted_values = [];
		// creating structure for cookie generator
		var structure = {};
		var sections = $ES('.section_block', main);
		//
		sections.each(function(elm,j){
			structure[elm.getProperty("class").replace(" section_block","")] = {
				"toggle" : ($E('.gk_news_section',elm).hasClass('hide')) ? 1 : (($E('.gk_news_section',elm)) ? 0 : 1),
				"intro" : ($E('.gk_news_form_intro',elm)) ? $E('.gk_news_form_intro',elm).getValue() : $G['intro_news_amount'],
				"more" : ($E('.gk_news_form_more',elm)) ? $E('.gk_news_form_more',elm).getValue() : $G['more_news_amount'],
				"cats" : []	
			};
			
			if($ES('.gk_news_form_checkbox', elm).length > 0){
				$ES('.gk_news_form_checkbox', elm).each(function(elmt,k){
					if(elmt.checked) structure[elm.getProperty("class").replace(" section_block","")].cats.push(elmt.getProperty("class").replace("gk_news_form_checkbox cat",""));
				});
			}
			// filling sorted array
			sorted_values.push(elm.getProperty("class").replace(" section_block",""));
		});
		// popup with dnd for sections
		$ES('.gk_news_ordering_list',main).each(function(sortable){
			new Sortables(sortable,{
				ghosts:true,
				onComplete: function(){
					sorted_values = [];
					$ES('li', sortable).each(function(elmts,k){
						sorted_values.push(elmts.getProperty("class"));
					});
					
					$ES('.gk_news_ordering_list',main).each(function(sortlist){
						if(sortlist != sortable){
							sorted_values.each(function(item,j){
								if(j > 0) $E('.'+item,sortlist).injectAfter($E('.'+sorted_values[j-1],sortlist));
							});
						}
					});
				}
			});	
		});
		// categories blocks animation
        categories_blocks.each(function(el){
        	if(!el.hasClass('hide')){
        		el.setStyle("display","block");
        		var fx = new Fx.Slide(el,{duration:$G['animation_time'],transition:$G['animation_type']}).hide();
        		categories_fx.push(fx);
        	}else{
        		var fx = new Fx.Slide(el,{duration:$G['animation_time'],transition:$G['animation_type']});
        		categories_fx.push(fx);
        	}
        });
		// settings blocks animation
		settings_blocks.each(function(el){
			el.setStyle("display","block");
			var fx = new Fx.Slide(el,{duration:$G['animation_time'],transition:$G['animation_type']}).hide();
			settings_fx.push(fx);
		});
		// toggle buttons events
		if(main.hasClass('sliding')){
    		toggle_buttons.each(function(elmt,j){
    			elmt.addEvent("click", function(){
    				if(elmt.hasClass('hide')){
    					categories_fx[j].slideOut();
    					elmt.removeClass('hide');
    					// save settings in structure
    					structure[sections[j].getProperty("class").replace(" section_block","")].toggle = 0;
    					// save settings in cookie
    					GKNews_COOKIE(structure,sorted_values,ID);
    				}else{
    					categories_fx[j].slideIn();
    					elmt.addClass('hide');
    					// save settings in structure
    					structure[sections[j].getProperty("class").replace(" section_block","")].toggle = 1;
    					// save settings in cookie
    					GKNews_COOKIE(structure,sorted_values,ID);
    				}
    			});
    		});
		}
		// options buttons events
		options_buttons.each(function(el,j){
			el.addEvent("click", function(e){
				new Event(e).stop();
				settings_fx[j].toggle();
			});
		});
		// settings cancel buttons events
		cancel_buttons.each(function(el,j){
			el.addEvent("click", function(){
				settings_fx[j].slideOut();
			});
		});
		// settings reset buttons events
		reset_buttons.each(function(el,j){
			el.addEvent("click", function(){
				Cookie.set(ID, "", {path: '/',duration:14});
				window.location.reload();
			});
		});
		// settings submit buttons events
		submit_buttons.each(function(el,j){
			el.addEvent("click", function(){
				// validation
				$validation_cat = true;
				$validation_amount = true;
				// categories
				if($ES('.gk_news_form_checkbox', settings_blocks[j]).length > 0){
					$validation_cat = false;
					$ES('.gk_news_form_checkbox', settings_blocks[j]).each(function(elm,k){
						if(elm.checked) $validation_cat = true;
					});
				}
				// articles amount
				if($E('.gk_news_form_art_amount',settings_blocks[j])){
					if($E('.gk_news_form_intro',settings_blocks[j]).getValue() == 0 && $E('.gk_news_form_more',settings_blocks[j]).getValue() == 0){
						$validation_amount = false;
					}
				}
				// final check
				if($validation_cat && $validation_amount){
					// save settings in structure
					structure[sections[j].getProperty("class").replace(" section_block","")].intro = $E('.gk_news_form_intro',settings_blocks[j]).getValue(); 
					structure[sections[j].getProperty("class").replace(" section_block","")].more = $E('.gk_news_form_more',settings_blocks[j]).getValue();
					
					if($ES('.gk_news_form_checkbox', sections[j]).length > 0){
						structure[sections[j].getProperty("class").replace(" section_block","")].cats = [];
						$ES('.gk_news_form_checkbox', sections[j]).each(function(elmt,k){
						if(elmt.checked) structure[sections[j].getProperty("class").replace(" section_block","")].cats.push(elmt.getProperty("class").replace("gk_news_form_checkbox cat",""));
						});
					}
					// sorting sections
					sorted_values.each(function(elmts,k){
						if(k > 0) $(ID+"-"+elmts).injectAfter($(ID+'-'+sorted_values[k-1]));
					});
					// save settings in cookie
					GKNews_COOKIE(structure,sorted_values,ID);
					// send request
					var myAjax = new Ajax('index.php?option=com_gk_ajax&modid=gk_news&mod_table_id='+$G['mod_table_id']+'&tmpl=component&format=raw', {
						method: 'post',
						data: {
							"section_id":sections[j].getProperty("class").replace(" section_block","").replace("section",""),
							"cats":structure[sections[j].getProperty("class").replace(" section_block","")].cats.join('-'),
							"intro_news_amount":structure[sections[j].getProperty("class").replace(" section_block","")].intro,
							"more_news_amount":structure[sections[j].getProperty("class").replace(" section_block","")].more
						},
						onRequest:function(){
							var div = $E('.gk_news_form', settings_blocks[j])
							var o = new Fx.Opacity(div,{duration:350});
							o.start(0);
							categories_fx[j].slideOut();
						},
						onSuccess:function(){	
							var $text = this.response.text;
							(function(){
								var div = $E('.gk_news_form', settings_blocks[j]); 
								var o = new Fx.Opacity(div,{duration:350});
								o.start(1);
								$E('.gk_news_categories',sections[j]).innerHTML = $text;
								categories_fx[j].show();
								categories_fx[j].hide();
								categories_fx[j].slideIn();	
								settings_fx[j].slideOut();
							}).delay(500);	
						},
						onFailure:function(){
							$E('.gk_news_categories',sections[j]).innerHTML = "An error occured - please try again...";
						}
					}).request();
				}else{
					if(!$validation_cat) alert('You have to select at least one category');
					if(!$validation_amount) alert('You have to select at least one article to show');
				}	
			});
		});
	});
});
// function to save cookie
function GKNews_COOKIE(structure,sorted_values,id){
	data = '';
	sorted_values.each(function(el,i){
		data += el.replace("section","") + "," + structure[el].toggle + "," + structure[el].intro + "," + structure[el].more + ":";
		data += structure[el].cats.join(',') + ";";
	});

	Cookie.set(id, data, {path: '/',duration:14});
}

function GKNews_DeleteCookie(name) {
 	document.cookie = name+"=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
