/*
	description:script file for Playback.pl
	resources:Prototype, script.aculo.us
	version:0.2
*/

// ---------------------------

/*
extension for Prototype to use cookies
written by Carlos Reche
*/
var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};

// ---------------------------

/*
show or hide menu block with links
	id - id of menu block
*/
var switchMenu = function(id) {
  if($('links_'+id).style.display != 'none') {
    Effect.BlindUp('links_'+id, {duration: 0.3});
   $('arrow_'+id).className = 'arrowDown';
	} else {
	  Effect.BlindDown('links_'+id, {duration: 0.3});
	  $('arrow_'+id).className = 'arrowUp';
 }
}

/*
hide inactive menu blocks
*/
var hideMenu = function() {
  // begin from... 
  var begin = 0;
  var classname = 'arrowDown';
  
  while($('links_'+begin)) {
		if($('arrow_'+begin).className == classname) {
    	$('links_'+begin).style.display = 'none';
	 	}
   	begin++;
	}
}

Event.observe(window, 'load', hideMenu, false);

// ---------------------------

var textStatus = 0; // 0 - with columns, 1 - without columns

var switchText = function() {
	if(textStatus == 0) {
		$('leftColumn').className = "noColumn";
		$('rightColumn').className = "noColumn";
		$('textSwitcher').className = "toColumn";
		textStatus = 1;
	} else {
		$('leftColumn').className = "left";
		$('rightColumn').className = "right";
		$('textSwitcher').className = "toNormal";
		textStatus = 0;
	}
	
	Cookie.set('cTextStatus', textStatus, 2);
}

var setText = function() {
  if(Cookie.get('cTextStatus') == 1) {
		switchText();
	}
}
Event.observe(window, 'load', setText, false);

// ---------------------------

var cmntUserInfo;

var cmntInit = function() {
  new Ajax.Request('/?m=zin&mode=islogged',
  {onSuccess:cmntCheck, onFailure:errFunc});
}

// alias
var cmntBox = function() {
  cmntInit();
}

var showWindow = function(id) {
  var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var winTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
	var winLeft = 250;
	$(id).style.top = winTop+"px";
	$(id).style.left = winLeft+"px";
	Effect.Appear(id);
}

var cmntCheck = function(t) {
  var cmntUserInfo = t.responseText.evalJSON(true);
  if(cmntUserInfo.status == "ok") {
		showWindow('comment');
	} else {
		showWindow('loginbox');
	}
}

var cmntSend = function() {
  $('zinContent').readonly = 'readonly';
  $('cmntButton').disabled = 'disabled';
  new Ajax.Request('/?m=zin&mode=cmnt&json=true', {
	parameters: $('cmntForm').serialize(true), onSuccess:infoWindow, onFailure:errFunc});
}

var infoWindow = function(t) {
  Effect.Fade('comment');
	var result = t.responseText.evalJSON();
	if(result.status == 'ok') {
		$('msgtext').innerHTML = "Komentarz został dodany.<br /><a href=\"#\" onclick=\"Effect.Fade('msgbox');return false;\">Zamknij</a><span>|</span><a href=\""+result.url+"\">Przejdź do tematu</a>";
	} else {
		$('msgtext').innerHTML = "Wystąpił błąd podczas dodawania komentarza.<br /><a href=\"#\" onclick=\"Effect.Fade('msgbox');return false;\">Zamknij</a>";
	}
	showWindow('msgbox');
}

var errFunc = function(t) {
    alert('Error ' + t.status + ' -- ' + t.statusText);
}

var logIn = function() {
	$('loginField').readonly = 'readonly';
	$('passField').readonly = 'readonly';
  $('loginButton').disabled = 'disabled';
  new Ajax.Request('/?m=login&json=true', {
	parameters: $('loginForm').serialize(true), onSuccess:logInResult, onFailure:errFunc});
}

var logInResult = function(t) {
  var result = t.responseText.evalJSON();
  if(result.status == "ok") {
		Effect.Fade('loginbox');
		showWindow('comment');
	} else {
		Effect.Fade('loginbox');
		$('loginField').readonly = false;
		$('passField').readonly = false;
  	$('loginButton').disabled = false;
		$('msgtext').innerHTML = "Nieprawidłowy login lub hasło.<br /><br /><a href=\"#\" onclick=\"Effect.Fade('msgbox');showWindow('loginbox');return false;\">Spróbuj ponownie</a><span>|</span><a href=\"#\" onclick=\"Effect.Fade('msgbox');return false;\">Zamknij</a>"
		showWindow('msgbox');
	}
}

// ---------------------------

//document.writeln('<'+'scr'+'ipt type="text/javascript" src="http://s2.hit.stat24.com/_'+(new Date()).getTime()+'/script.js?//id=bPo1lIdrneBuDpvGEKpNwrPp374yiu8QuN38Tcnw.zn.F7/l=11"></'+'scr'+'ipt>');

// ---------------------------

var createAdsBlock = function() {
  // create box
  var adbox = document.createElement('div');
  Element.extend(adbox);
  adbox.addClassName('rightBlock');
  
  // title - top
  var adtitletop = document.createElement('div');
  Element.extend(adtitletop);
  adtitletop.addClassName('blockTitle');
  
  // title - text
  var adtitletext = document.createElement('h4');
  Element.extend(adtitletext);
  adtitletext.innerHTML = "Reklama";
  
  // content
  var adcontent = document.createElement('div');
  Element.extend(adcontent);
  adcontent.addClassName('adsBlockContent');
  
  // content - script - it doesn't work...
  //var adscript = document.createElement('script');
  //adscript.setAttribute("type", "text/javascript");
  //adscript.setAttribute("src", "http://pagead2.googlesyndication.com/pagead/show_ads.js");
  
  // content - object
  //var adobject = document.createElement('object');
  //adobject.setAttribute("data", "test.html");
  //adobject.setAttribute("type", "text/html");
  //adobject.setAttribute("width", "120");
  //adobject.setAttribute("height", "240");
  /*adobject.innerHTML = "Twoja przeglądarka nie akceptuje osadzania obiektów!";*/
  // content - iframe
  var adiframe = document.createElement('iframe');
  adiframe.setAttribute("src", "http://www.playback.pl/googleads.php5");
  adiframe.setAttribute("frameborder", "0");
  adiframe.setAttribute("scrolling", "no");
  adiframe.setAttribute("marginheight", "0");
  adiframe.setAttribute("marginwidth", "0");
	adiframe.setAttribute("allowtransparency", "allowtransparency");
	adiframe.setAttribute("hspace", "0");
	adiframe.setAttribute("vspace", "0");
  // object param
  //var adparam = document.createElement('param');
  //adparam.setAttribute("src", "test.html");
  
  // just connect
  //adobject.appendChild(adparam);
  adtitletop.appendChild(adtitletext);
  //adcontent.appendChild(adobject);
  adcontent.appendChild(adiframe);
  adbox.appendChild(adtitletop);
  adbox.appendChild(adcontent);
  document.getElementsByClassName("third")[0].appendChild(adbox);
}

Event.observe(window, 'load', createAdsBlock, false);
