dojo.require("dojo.NodeList-fx");
dojo.require("dojox.widget.AutoRotator");
dojo.require("dojox.widget.rotator.Controller");
dojo.require("dojox.widget.rotator.Fade");


var galleryObjects=[];

function connectGalleries(){
	dojo.query('.gallerytoggle .toggler-nav').forEach(function(el, index){
		var galleryElementId = el.id.replace(/controler-rotator-/gi, "");
		galleryObjects["autorotator_"+galleryElementId] = new dojox.widget.AutoRotator({
				transition: "dojox.widget.rotator.fade", duration: 4000, pauseOnManualChange: true, suspendOnHover: true
			}, dojo.byId("rotator-"+galleryElementId));
		galleryObjects["autorotator_"+galleryElementId+"_controller"] = new dojox.widget.rotator.Controller({
				rotator: galleryObjects["autorotator_"+galleryElementId], commands:'#'
			}, dojo.byId("controler-rotator-"+galleryElementId));
	});
}

dojo.addOnLoad(connectGalleries);

dojo.addOnLoad(function(){

	function reduce (/*Array*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
		// summary: repeatedly applies a binary function to an array from left
		//	to right; returns the final value.
		a = typeof a == "string" ? a.split("") : a; o = o || window;
		var z = a[0];
		for(var i = 1, s = a.length; i < s; z = f.call(o, z, a[i++]));
		return z;	// Object
	}

	function max_height (aNode, bNode) {
		return dojo.position(aNode).h > dojo.position(bNode).h ? aNode : bNode;
	}

	var defaultConfig = {
		module_selector : '.module',
		child_selector : '.item'
	};

	function equalizer(userConfig) {
		var config = dojo.mixin({}, defaultConfig, userConfig || {});
		dojo.query(config.module_selector).forEach(function(node){
			var items = dojo.query('.item', node),
				max_height_item = reduce(items, max_height),
				height = dojo.style(max_height_item, 'height').toString();
			items.forEach(function (node) {
				dojo.style(node, 'height', height + 'px');
			});
		});
	}

	equalizer({
		module_selector: ".md_wideo"
	});

});


(function(){

	dojo.require("dojo.fx");
	dojo.require("dojo.NodeList-fx");

	var axTimeout=33000,
		strWait='<img src="http://s.v3.tvp.pl/files/portal/gfx/wait.gif" alt="wait" class="mid" /> Prosz\u0119 czeka\u0107... ';

	function loadContent(obj, params, url) {
		var url=url||'/pub/stat/listing';
		var div=(typeof obj == 'string')?dojo.byId(obj):obj;
		if(!div) {
			console.error('Unrecognized div element');
			console.error(obj);
			return false;
		}
		div.innerHTML=''+strWait;
		var bindArgs={
			content:params,
			timeout:axTimeout,
			url:url,
			handle:function(response,ioArgs) {
				div.innerHTML='';
				div.innerHTML=response;
			},
			error:function(response,ioArgs){
				console.error('HTTP status code: ',ioArgs.xhr.status);
				return response;
			}
		};
		dojo.xhrGet(bindArgs);
		return false;
	}


	window.loadContent = loadContent;
}())
