dojo.require("dijit.Dialog");
dojo.require("dojo.fx");
dojo.require("dijit.TitlePane");

function openDialog( url, dialogId, params)
{
	if(params === undefined)
	{
		var params = new Array();
	}
	
	params['href'] = url;
	params['id'] = dialogId;
	
	var d = dijit.byId(dialogId); 
	
	if(d)
	{
		d.hide();
		d.destroy();
		delete d;
	}
	
	var dlg = new dijit.Dialog(params);
	
	dlg.show();
}

function ArtwerkPage()
{
	this._current = 'main';
	this._lines_anims = new Array();
	this._pids = ['artists','equipment','contact','contact_en','about','about_en','events','links','news','coming_soon'];
	
	this.throwLine = function(id)
	{
		//	podswietlanie obrazkow
		this._turnOnMenuItem(id);
		
		//	linie wypuszczaj tylko na stronie glownej
		if (this._current != 'main') {
			return false;
/*			var curtain = dojo.byId('curtain');
			if (curtain.style.opacity==undefined || curtain.style.opacity==0) {
				fadeIn(curtain,500);
			}
			
			dojo.style(curtain,'zIndex',9);*/
		}
		
		if (this._lines_anims[id] && this._lines_anims[id].status()=='playing') {
			this._lines_anims[id].pause();
		}
		
		var node = dojo.byId('line_'+id);
		var nodeImg = dojo.byId('lineImg_'+id);
		nodeImg.style.marginLeft = "-538px";
		
		//	wszystkie obrazki maja stala szerokosc, chociaz linie sa krotsze
		var linesWidths = {'contact':156,'about':282,'artists':270,'events':436,'news':374};
		var lineWidth = (linesWidths[id] === undefined) ? 538 : linesWidths[id]; 
		
		//onsole.log('id='+id+', lineWidth='+lineWidth);
		this._lines_anims[id] = new LineMarginAdjuster(nodeImg, node, 538, lineWidth);
		this._lines_anims[id].play();
	};

	this.pullLine = function(id)
	{
		//	podswietlanie obrazkow
		if (this._current != 'main' && this._current != id) {
			this._turnOffMenuItem(id);
		}

		if (this._lines_anims[id] && this._lines_anims[id].status()=='playing') {
			this._lines_anims[id].pause();
		}
	
		var node = dojo.byId('line_'+id);
		var nodeImg = dojo.byId('lineImg_'+id);
		
		if (nodeImg && node) {
			var that = this;
			this._lines_anims[id] = new LineMarginAdjuster(nodeImg, node, 538, 0);
			this._lines_anims[id].play();
		}
	};
	
	this.show = function(href, refresh)
	{
		var pid = gup('pid', href);
		//onsole.log('showPage('+(this._current)+' => '+pid+')');
		this._current = pid;
		this.pullLine(pid);
		href = href.replace('index.php','viewhelper.php');
		var containerName = pid+'Container';

		//	sciagnij tresc jesli jeszcze jej nie ma
		
		var that = this;
		var nod = dojo.byId('mainWindow');
		var cont = dojo.byId('container');

		fadeOut(nod,500, function(){cont.className = pid;});

		if ( !dojo.byId(pid+'Container') || (dojo.byId(pid+'Container') && dojo.byId(pid+'Container').innerHTML=='') || refresh) {
			var _handleAfterLoadEnd = function()
			{
				that.buildScrollPane();
				that.activateLightBoxes();
				that.buildTabPane();
				$('#loadingDataNotifier').hide();
				fadeIn(nod,600);
			};
			$('#loadingDataNotifier').show();
			retrieveUrlFade(href,'content',_handleAfterLoadEnd);
		//	fadeOut na glownym oknie, podmiana tla,
		} else {
		//	fadeOut na glownym oknie, podmiana tla,
			setTimeout(function(){ fadeIn(nod,600); }, 500);
			setTimeout( function(){ that.buildScrollPane(); }, 800);
			setTimeout( function(){ that.activateLightBoxes(); }, 800);
			setTimeout( function(){ that.buildTabPane(); }, 800);
		}
		
		//	pokaz/ukryj link do strony glownej
		if (this._current == 'main') {
			this.hideHomePageLink();
		} else {
			this.showHomePageLink();
		}
		
		//	zapal wszystkie elementy menu dla HP
		if (pid=='main') {
			var i;
			for (i=0;i<this._pids.length;i++) {
				setTimeout("AP._turnOnMenuItem(AP._pids["+i+"])",i*300);
			}
		} else {
		//	zgas pozostale elementy
			this._turnOffInactiveItems();
		}
		
		//	pokaz/schowaj zakladki
		if (pid=='events') {
			$('div#tabsContainer').show();
			$('div#itemsLinksContainer').show();
		} else {
			$('div#tabsContainer').hide();
			$('div#itemsLinksContainer').hide();
		}
	};
	
	this.showHomePageLink = function()
	{//onsole.log('showHPlink');
		var hpl = dojo.byId('mainMenu');
		
		if (hpl.style.opacity == undefined || hpl.style.opacity==0) {
			dojo.style(hpl,'opacity',0);
			dojo.style(hpl,'display','block');
			fadeIn(hpl,800);
		}
	};

	this.hideHomePageLink = function()
	{//onsole.log('hideHPlink');
		var hpl = dojo.byId('mainMenu');
		
		if (hpl.style.display!='none' || (hpl.style.opacity && hpl.style.opacity!=0)) { //onsole.log('TRUE');
			fadeOut(hpl,1000);
		}
	};
	
	this._timers = [];
	
	this._turnOnMenuItem = function(id)
	{
		//	wyczysc timery
		if (this._timers['menuImg'+id] !== undefined) {
			var i=0;
			while (this._timers['menuImg'+id][i] !== undefined) {
				clearTimeout(this._timers['menuImg'+id][i]);
				i++;
			}
		}
		
		var im = dojo.byId('menuImg_'+id);

		if (!im || !im.src) {
			return false;
		}

		im.src = im.src.replace('Inactive.gif','.gif');
	};

	this._turnOffMenuItem = function(id)
	{
		var im = dojo.byId('menuImg_'+id);
		
		if (!im || !im.src || im.src.indexOf('Inactive')>-1) {
			return false;
		}
		
		var that = this;
		
		var zgas = function()
		{
			if (im.src.indexOf('Inactive')==-1) {
				im.src = im.src.replace('.gif','Inactive.gif'); 
			}
		};
		
		var zapal = function()
		{
			im.src = im.src.replace('Inactive.gif','.gif');
		};
		
		this._timers['menuImg'+id] = [setTimeout(zgas,1300),
		 setTimeout(zapal,1400), setTimeout(zgas,1450),
		 setTimeout(zapal,1600), setTimeout(zgas,1650),
		 setTimeout(zapal,2000), setTimeout(zgas,2050)];
	};
	
	this._turnOffInactiveItems = function()
	{
		var i;
		for (i=0;i<this._pids.length;i++) {
			if (this._pids[i] !== this._current) {
				//this._turnOffMenuItem(this._pids[i]);
				setTimeout("AP._turnOffMenuItem(AP._pids["+i+"])",i*300);
			} else if (this._timers['menuImg'+this._current]!==undefined && this._timers['menuImg'+this._current][0]!==undefined) {
				//	wyczysc timer zwiazany z aktywnym elementem
				var j=0;
				while (this._timers['menuImg'+this._current][j] !== undefined) {
					clearTimeout(this._timers['menuImg'+this._current][j++]);
				}
				
				this._turnOnMenuItem(this._current);
			}
		}
	}
	
	this.buildScrollPane = function()
	{
		setTimeout(function(){
			//TODO zrob cos madrego ze scrollem
			$("div#content.scroll-pane").jScrollPane({showArrows:true,arrowSize:16, scrollbarWidth:15, scrollbarMargin:2,maintainPosition: false});//
		}, 1000);
		
	};
	
	this.activateLightBoxes = function()
	{//onsole.log('activateLightBoxes');
		var tmpItems = $('.itemsContainer .item');
		var i=0;
		for (i=0;i<tmpItems.length;i++) {
			$('#' + tmpItems[i].id + ' .lightboxPlease').lightBox();
		}
	};
	
	this.buildTabPane = function()
	{
		$('div#tabsContainer').html($('div#tabsProvider').html());
		$('div#itemsLinksContainer').html($('div#itemsLinksProvider').html());
		$('ul.sf-menu').superfish();
		$('div#nextItemLinkContainer').html($('div#nextItemLinkProvider').length>0 ? $('div#nextItemLinkProvider').html() : '');
		$('div#prevItemLinkContainer').html($('div#prevItemLinkProvider').length>0 ? $('div#prevItemLinkProvider').html() : '');
	};
	
	this.Newsletter = {
		subscribe: function() { $('form#formNewsletter input#wybierzPolecenie').val('subscribe'); $('form#formNewsletter')[0].onsubmit(); },
		unsubscribe: function() { $('form#formNewsletter input#wybierzPolecenie').val('unsubscribe'); $('form#formNewsletter')[0].onsubmit(); }
	};
}

//	form
var tempInpArr = new Array();

function hideValue(inp)
{
	if ($(inp).hasClass('inputEmpty')) {
		tempInpArr[inp.id] = inp.value;
		inp.value = '';
		$(inp).removeClass('inputEmpty');
	}
}

function checkValue(inp)
{
	$(inp).removeClass('inputFocused');
	if (inp.value=='') {
		$(inp).addClass('inputEmpty');
		inp.value=tempInpArr[inp.id];
	}
}

function submitContactForm()
{
	var f = $('#contactForm');
	if (f.length > 0) {
		f[0].onsubmit();
	}
}

function _handleBeforeSubmitContactForm(f)
{
	if ($('#contactMessage.inputEmpty').length==1 || $('#contactMessage')[0].value=='') {
		$('#contactError').html('Wpisz treść wiadomości');
		$('#contactError').show('slow');
		return false;
	}

	//	wylacz przycisk submit
	$('#buttonSubmitContact').hide();
	
	//	pokaz progress
	$('#contactError').html('');
	$('#contactError').addClass('in-progress');
	$('#contactError').show();

	return true;
};

function _handleAfterSubmitContactForm(resp)
{
	resp = processJsonResponse(resp);
	$('#contactError').hide();
	var responseString = "<div class=\"msg "+resp.status+"\">"+resp.msg+"</div>";
	//onsole.log(responseString);
	//	jesli ok to zastap formularz odpowiedzia
	if (resp.status && resp.status == 'ok') {
		var responseNode = document.createElement('div');
		responseNode.setAttribute('id','contactFormResponse');
		responseNode.innerHTML = responseString;
		$('#contactForm')[0].parentNode.appendChild(responseNode);
		$('#contactForm')[0].parentNode.removeChild($('#contactForm')[0]);
	} else {
	//	wyswietl odpowiedz pod formularzem
		$('#buttonSubmitContact').show();
		$('#contactError').removeClass('in-progress');
		$('#contactError').html(responseString);
		$('#contactError').show('slow');
	}
};

function handleBeforeSubmitNewsletter()
{
	if ($('#email').val().match('^[0-9a-z]+([\._\-][0-9a-z]+)*\@[0-9a-z]+([\.\-][0-9a-z]+)*\.(([a-z]{2,4})|(travel)|(museum))') === null) {
		output = "<div class=\"msg error\">Wpisz poprawny adres e-mail</div>";
		displayResponseInElement(output, 'message', 8000);
		$('#email').focus();
		return false;
	}
}
