/*
* (c) 2008 mirun@forcom.cz
*/

var userSet = {
  'allowMe':1,
//   'allowFilm':1,
//   'allowKniha':1,
//   'allowDrb':1,
//   'allowProblem':1,
  'logged':false
}

function getCookie(cookieName){
  var cookieValue = null;
  var posName = document.cookie.indexOf(escape(cookieName) + '=');
  if (posName != -1){
    var posValue = posName + (escape(cookieName) + '=').length;
    var endPos = document.cookie.indexOf(';', posValue);
    if (endPos != -1) {
      cookieValue = unescape(document.cookie.substring(posValue, endPos));
    }
    else {
      cookieValue = unescape(document.cookie.substring(posValue));
    }
  }
  return cookieValue;
}

function checkTest(form){
  var r = $('#testform input:checked').val();
  if(r == undefined){alert('Musíte vybrat hodnotu!'); return(false);} else {return(true);}
}

function checkQuality(){
  var r = $('#quaform input:checked').val();
  if(r == undefined){alert('Musíš dát nějaké hvězdičky!'); return(false);} else {return(true);}
}

function checkModa(form){
  var needValue = false;
  $('#moda .stars').each(function() {
    var r= $(':input:checked',this).val();
    if(r == undefined){needValue = true;}
  });
  if(needValue == true){alert('Musíte ohodnotit všechny položky!'); return(false);} else {return(true);}
}
function thisMovie(movieName) {
  if(navigator.appName.indexOf("Microsoft") != -1) {return window[movieName];} else {return document[movieName];}
}

function loadFile(obj) {
  thisMovie("rotator").sendEvent("load",obj);
  // m.sendEvent("LOAD",obj);
};

var iPravInc = 1;
var iPravMin = 50;
var iPravMax = 100;

function anmPravidla(obj,end) {
  newBot = parseInt(obj.css("bottom")) + iPravInc;
  if(newBot == iPravMax) iPravInc = -1;
  if(newBot == iPravMin) iPravInc = 1;
  obj.css("bottom", newBot);
  return (true);
}

var iImgInc = -10;
var iImgMin = 30;
var iImgMax = 603;

function anmImage(obj,end){
  newBot = parseInt(obj.css("left")) + iImgInc;
  if(newBot > iImgMax){iImgInc = -10; $('#ahzcv').show(); return(false);}
  if(newBot < iImgMin){iImgInc = 10; $('#ahzday').show(); return(false);}
  obj.css("left", newBot);
  return (true);
}

function initHP() {
  
  $('a[rel="lightbox-tour"]').colorbox();

  $('li.note[scrid]').each(function() {
    var a = '<ul class="fn"><li class="stop"><a name="' + $(this).attr('scrid') + '" href="javascript:;">nahlásit!</a></li></ul>';
    $(this).append(a);
  });

  $('li.stop a').click(function() {
    $('li.spam #spam').remove();
    var spammer = '<li id="spam">' +
    '  <h3><a class="close" href="javascript:;">x</a>Nahlásit příspěvek</h3>' +
    '  <div class="bdy"><big>Chceš tento příspěvek nahlásit moderátorům jako hloupý či obtěžující?</big>' +
    '    <p><a href="javascript:;" class="btnOk">ANO</a> <a class="close" href="javascript:;">NE</a></p>' +
    '  </div>' +
    '  <div class="msg">' +
    '    <div class="ajax"></div>' +
    '    <p><a class="close" href="javascript:;">ZAVŘÍT</a></p>' +
    '  </div>' +
    '</li>';
    $(this).parent().before(spammer);
    $('li[scrid]').removeClass('spam');
    $(this).parent().parent().parent().addClass('spam');
    var cid = $(this).attr('name');
    return(showSpam(cid));
  });
  
  $('#openComment').click(
    function() {
      $('#insertComment').show();
      $('#screamBody').focus();
      return(false);
    }
  );
  
  $('#header .new a').click(
    function() {
      $('#newsletter').show();
      return(false);
    }
  );

  $('input.star').hide();		// skryj inputy a ukaž hvězdičky
  $('#moda input').hide();
  $('span.star').show();

  $('span.star').click(function() {	// po kliknutí proveď zobrazení hvězdiček
    $(this).parent().children('span.star.set').removeClass('set');
    $(this).prevAll('span.star').addClass('set');
    $(this).addClass('set');
    $(this).children('input').attr("checked","checked");
  });
  
  $('#testform label.star').click(
    function(){
      // $(this).parent().parent().children('span.star.active').removeClass('active');
      // $(this).removeClass('active');
      $('#testform .star').removeClass('set');
      $(this).addClass('set');
      $(this).children('input').attr("checked","checked");
    }
  );
  
  // butik
  $('#ahzcv').click(function() {
    iImgInc = -10;
    createPlayer('salon2011');
    $("#imageProjectorCV").animator(10, anmImage);
    $(this).hide(); 
    $('#ahzday').show();
    return(false);
  });

  $('#ahzday').click(function() {
    createPlayer('v_kolekce2011');
    iImgInc = 10;
    $("#imageProjectorCV").animator(10, anmImage);
    $(this).hide();
    $('#ahzcv').show(); 
    return(false);
  });

  var options = {
    target:'#quadiv', 
    url:'run.php?pid=artquality&s=1&ajax=1',
    beforeSubmit: function(){return checkQuality();}
  };
  
  // toto jsem zakomentoval, protože to hlasí chybu WTF?
  // $('#quaform').ajaxForm(options);
  $('#insertComment').hide();

  $('#openComment').click(
    function() {
      $('#insertComment').show();
      $('#screamBody').focus();
      return(false);
    }
  );
  $('#header .new a').click(
    function() {
      $('#newsletter').show();
      $('#newsletter input').focus();
      return(false);
    }
  );
  
  $.ajaxSetup({async:true});
  //checkUState();
  /* kometářové limity zobraz */
  var ccount = parseInt(getCookie('commday'));
  if(ccount <= 0){
	  $('#screamInfo').html('Tento tvůj komentář již dnes <b>nebude bodován</b>!');
	  $('#screamInfo').addClass('err');
  }
  // toto jsem zakomentoval, protože to hlasí chybu WTF?
  //JQzoom();
  getUser();
}

function showSpam(id) {
  $('#spam .msg').hide();
  $('#spam .bdy').show();
  $('#spam').show();
  $('#spam .btnOk').click(function() {return(markAsSpam(id))});
  $('#spam .close').click(function() {
    $('li.spam #spam').remove();
    $('li[scrid]').removeClass('spam');
  });
  return(false);
}

function markAsSpam(id) {
  $('#spam .msg .ajax').text('Probíhá hlášení...');
  $('#spam .bdy').hide();
  $('#spam .msg .ajax').load('/run.php?pid=screams&s=markit&mark=4&id='+id);
  $('#spam .msg').show()
  return(false);
}

function removeSpam() {
  alert();
  $('#spam').remove();
  return(false);
}

function checkUState() {
  var cookieUState = getCookie('UState');
  if(cookieUState == null) {
    $.getJSON('/run.php',{pid:"allow",allowed:'all'}, function(data){initUser(data);});
  } else {
    alert(cookieUState);
  }
}

function initUser(data){
 //userSet = data;
  if(userSet.logged == true) {
    if(userSet.allowKniha == 0) {
      $('#alloMe .on').addClass('disabled');
      $('#alloMe .off').removeClass('disabled');
    }
//     if(userSet.allowKniha == 0) {
//       $('#mFormBox .on').addClass('disabled');
//       $('#mFormBox .off').removeClass('disabled');
//     }
//     if(userSet.allowDrb == 0) {
//       $('#oponaFormBox .on').addClass('disabled');
//       $('#oponaFormBox .off').removeClass('disabled');
//     }
//     if(userSet.allowProblem == 0) {
//       $('#poradnaFormBox .on').addClass('disabled');
//       $('#poradnaFormBox .off').removeClass('disabled');
//     }
  } else {
    $('.on').addClass('disabled');
    $('.unlog').removeClass('disabled');
  }
}

/*function checkUState() {
  var cookieUState = getCookie('UState');
  if(cookieUState == null) {
    $.getJSON('/run.php',{pid:"allow",allowed:'all'}, function(data){initUser(data);});
  } else {
    alert(cookieUState);
  }
}*/

function createPlayer(fileToPlay,w,h,projector){
  if(fileToPlay == undefined){fileToPlay = 'v_kolekce';}
  if(w == undefined){w = '330';}
  if(h == undefined){h = '500';}
  if(projector == undefined){projector = 'imageProjector';}
  var s1 = new SWFObject("/system/global/scripts/imagerotator.swf","rotator",w,h,"7");
  s1.addParam("allowfullscreen","false");
  s1.addVariable("file",'/users/bueno/pictures/playlists/'+fileToPlay+'.xml');
  s1.addVariable("width",w);
  s1.addVariable("height",h);
  s1.addVariable("overstretch","true");
  s1.addVariable("transition","fade");
  s1.addVariable('enablejs', 'true');
  s1.addVariable('screencolor','0xFFFFFF');
  s1.write(projector);
}

function JQzoom() {
  $('a.jqzoom').jqzoom({
    /* standard, reverse, innerzoom, drag*/
    zoomType: 'innerzoom',
    preloadImages: true,
    alwaysOn:false
  });
}

$(document).ready(function(){initHP();});
