
// todo



function showxImage(hrf, dsc, wdh, hgt) {
  if (hrf!=null)
  {
    $('image').update();
    $('desc').update();
    var tabHeight = document.viewport.getDimensions().height;
		var	yScroll = document.viewport.getScrollOffsets().top;
  	var detailScroll;
  		if (window.innerHeight && window.scrollMaxY) {	
  			detailScroll = window.innerHeight + window.scrollMaxY;
  		} else if (document.body.scrollHeight > document.body.clientHeight){
  			detailScroll = document.body.scrollHeight;
  		} else {
  			detailScroll = document.body.clientHeight;
  		}
      $('foto').setStyle({height: detailScroll+'px'});
      $('tab').setStyle({top: yScroll+'px'});
      $('tab').setStyle({height: tabHeight+'px'});
      $('fotoWrap').setStyle({visibility:'visible'});


      var img = document.createElement("img");
      img.setAttribute("src", hrf);
      img.setAttribute("alt", dsc);  
  


      var text = document.createTextNode(dsc);
      $('prev').setStyle({display: 'none'});
      $('next').setStyle({display: 'none'});
      $('image').appendChild(img);  
      $('desc').appendChild(text);
      return false;
  }
}


  
//

document.observe('dom:loaded', function() {

  if($('menu')){
  	Event.observe($('menu'),'mouseover',function(event){
  		var elt = $(Event.element(event));
  		if (elt && elt != document && elt.nodeName=='A' && elt.descendantOf($('menu'))){
  			elt=elt.up('LI');
  			Event.stop(event);
  
  			var its=$$('.miActive').findAll(function(e){return !elt.descendantOf(e) && !e.descendantOf(elt) && e!=elt});
  			elt.addClassName('miActive');							
  
  			its.each(function(e){e.select('UL').invoke('hide')});
  			its.invoke('removeClassName','miActive');
  
  			var ul=elt.childElements().find(function(e){return e.nodeName=='UL';});
  			if (ul) {
  				ul.setStyle({display:'block'});					
  			}	
  		}		
  	});		  
  	Event.observe(document,'mouseover',function(event){
  		var elt = $(Event.element(event));
  		if (!elt.descendantOf($('menu')) && $$('.miActive').length>0) {
  			$('menu').select('ul').invoke('hide');
  			$$('.miActive').invoke('removeClassName','miActive');
  		}		
  	});		 
  }
    
  if($('search')){
    Event.observe($('srch'),'focus',function(event){
      el = Event.element(event);
  
      if (!el.used) {
        el.value='';
        el.used=1;
      }
    });	
    Event.observe($('search'),'submit', function(event){
      el = Event.element(event);
      new Ajax.Updater('left', '/search/ajax.php', {asynchronous:true, evalScripts:true, onComplete:function(request){ if($('subsrch')){ $('subsrch').setStyle({backgroundPosition:'0px 0px'});}}, onLoading:function(request){ if($('subsrch')){ $('subsrch').setStyle({backgroundPosition:'-25px 0px'});}}, parameters:el.serialize()});
      Event.stop(event);	
    }, false);
  }
  
  if($('pass')){  
    var focusi = [$('name'), $('pass')];
    for (var i = 0; i < focusi.length; i++) {
      if(focusi[i]){
        Event.observe(focusi[i],'focus',function(event){
          Event.element(event).setStyle({background:'#acadb5', color:'#000000'});
        });	
        Event.observe(focusi[i],'blur',function(event){
          Event.element(event).setStyle({background:'#292b36', color:'#ff8f00'});
        });
      }
    }
  }

  var regform = $('registration_form');
  if(regform){
  
    var inputs = [{ input_name : "name", input_notice : "Jméno musíte vyplnit!", input_exp : /\D{2,}/},
                 { input_name : "surname", input_notice : "Příjmení musíte vyplnit!", input_exp : /\D{2,}/},
                 { input_name : "street", input_notice : "Ulici musíte vyplnit!", input_exp : /.{3,} ?\d{1,}/},
                 { input_name : "city", input_notice : "Město musíte vyplnit!", input_exp : /\D{2,}/},
                 { input_name : "code", input_notice : "PSČ musíte vyplnit!", input_exp : /\d{3} ?\d{2}/},
             //    { input_name : "phone", input_notice : "Telefon musíte vyplnit!", input_exp : /^[+]?[()/0-9. -]{9,}$/},
                 { input_name : "email", input_notice : "E-mail musíte vyplnit!", input_exp : /[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/},
              //   { input_name : "currency", input_notice : "Vyberte měnu pro platby!"},
                 { input_name : "login", input_notice : "Přihlašovací jméno musíte vyplnit!", input_exp : /\D{2,}/},
                 { input_name : "regpassword", input_notice : "Heslo musíte vyplnit!", input_exp : /.{3,}/},
                 { input_name : "confirm", input_notice : "Heslo musíte potvrdit!", input_exp : /.{3,}/}              
            ];


    function validateItem(ipt, exp) {
    itm = regform[ipt];
    	if (exp.test($F(itm))) {
    		itm.style.color = "#000000";
    		return true;
    	} else {
    		itm.style.color = "#B51E05";
    		return false;
    	}
    }

    for (i = 0; i < inputs.length; i++) { 
      ipt = inputs[i].input_name;
      exp = inputs[i].input_exp;
      if (regform[ipt] && exp) {
        validateItem(ipt,exp);
        regform[ipt].observe('keyup',validateItem.curry(ipt,exp));
      }
    }


    function validateRegform(event){
      el = Event.element(event);
      for (i = 0; i < inputs.length; i++) { 
        itm = el[inputs[i].input_name];
        if (itm) {
          ivalue = $F(itm);
          notice = inputs[i].input_notice;
            if (ivalue < 1) {alert(notice); Event.stop(event); return false;}
        }
      }
      if (el.regpassword.value != el.confirm.value) {alert("Zadaná hesla se neshodují!"); Event.stop(event); return false;}
    }

    regform.observe('submit',validateRegform);
    regform.action='/login.php'; 
 
  }

  if($('order') && cartids){ 
    
    $('order').reset();  
    for (var i = 1; i < cartids; i++) {
      
      if($('delete_'+i)){
        Event.observe($('delete_'+i),'click',function(event){
          el = Event.element(event);
          k = el.name.substring(7, el.name.length);
        $('purpose_'+k).value = 'none';
      
        $('irow_'+k).setStyle({display:'none'});
        });	
      }
      
      if($('purpose_'+i)){
        Event.observe($('purpose_'+i),'change',function(event){
          el = Event.element(event);
          p = el.value.substring(0, 2);
          k = el.value.substring(3, el.value.length);
      
          if(p == 're'){      
            $('tprice_'+k).innerHTML = currency;      
            $('ppformat_'+k).setStyle({display:'none'});
            $('pnformat_'+k).setStyle({display:'none'});
            $('price_'+k).setStyle({display:'inline'});
            $('format_'+k).setStyle({display:'inline'});
          }
          
          if(p == 'de'){      
            $('tprice_'+k).innerHTML = decorative+currency;      
            $('ppformat_'+k).setStyle({display:'none'});
            $('pnformat_'+k).setStyle({display:'none'});
            $('price_'+k).setStyle({display:'none'});
            $('format_'+k).setStyle({display:'inline'});
          }
          
          if(p == 'pp'){      
              if ($('ppformat_'+k).value) { val = $('ppformat_'+k).value; } 
              else { val = $('ppformat_'+k).options[$('ppformat_'+k).selectedIndex].value; }
              if (val=='none') { val = ''; valx = '';} else { valx = val+currency;}      
            $('tprice_'+k).innerHTML = valx;     
            $('format_'+k).setStyle({display:'none'});
            $('pnformat_'+k).setStyle({display:'none'});
            $('price_'+k).setStyle({display:'none'});
            $('ppformat_'+k).setStyle({display:'inline'});          
          }
    
          if(p == 'pn'){      
              if ($('pnformat_'+k).value) { val = $('pnformat_'+k).value; } 
              else { val = $('pnformat_'+k).options[$('pnformat_'+k).selectedIndex].value; }
              if (val=='none') { val = ''; valx = '';} else { valx = val+currency;}      
            $('tprice_'+k).innerHTML = valx;     
            $('format_'+k).setStyle({display:'none'});
            $('ppformat_'+k).setStyle({display:'none'});
            $('price_'+k).setStyle({display:'none'});
            $('pnformat_'+k).setStyle({display:'inline'});          
          }

        });	
      }

/*

*/  


/*
      if($('rradio_'+i)){
        Event.observe($('rradio_'+i),'click',function(event){
          el = Event.element(event);
          k = el.name.substring(6, el.name.length);
      
        $('price_'+k).value = el.value; $('tprice_'+k).innerHTML = el.value;
      
        $('pformat_'+k).setStyle({display:'none'});
        $('dformat_'+k).setStyle({display:'none'});
        $('rformat_'+k).setStyle({display:'inline'});
        });	
      }
      
      if($('dradio_'+i)){
        Event.observe($('dradio_'+i),'click',function(event){
          el = Event.element(event);
          k = el.name.substring(6, el.name.length);
      
        $('price_'+k).value = el.value; $('tprice_'+k).innerHTML = el.value+currency;
      
        $('pformat_'+k).setStyle({display:'none'});
        $('rformat_'+k).setStyle({display:'none'});
        $('dformat_'+k).setStyle({display:'inline'});
      
        });	
      }
      
      if($('pradio_'+i)){
        Event.observe($('pradio_'+i),'click',function(event){
          el = Event.element(event);
          k = el.name.substring(6, el.name.length);
      
          if ($('pformat_'+k).value) { val = $('pformat_'+k).value; } 
          else { val = $('pformat_'+k).options[$('pformat_'+k).selectedIndex].value; }
          if (val=='none') { val = ''; valx = '';} else { valx = val+currency;}
      
        $('price_'+k).value = val; $('tprice_'+k).innerHTML = valx;
      
      
        $('rformat_'+k).setStyle({display:'none'});
        $('dformat_'+k).setStyle({display:'none'});
        $('pformat_'+k).setStyle({display:'inline'});
      
        });	
      }
*/      
      if($('ppformat_'+i)){
      
        Event.observe($('ppformat_'+i),'change',function(event){
          el = Event.element(event);
          k = el.name.substring(9, el.name.length);
          if (el.value) { val = el.value; } 
          else { val = el.options[el.selectedIndex].value; }
          if (val=='none') { val = ' '; valx = ' ';} else { valx = val+currency;}
      
      
      if (val) {$('tprice_'+k).innerHTML = valx;}
      
        });	
      }
      
      if($('pnformat_'+i)){
      
        Event.observe($('pnformat_'+i),'change',function(event){
          el = Event.element(event);
          k = el.name.substring(9, el.name.length);
          if (el.value) { val = el.value; } 
          else { val = el.options[el.selectedIndex].value; }
          if (val=='none') { val = ' '; valx = ' ';} else { valx = val+currency;}
      
      
      if (val) {$('tprice_'+k).innerHTML = valx;}
      
        });	
      }
    
    }

    function validateOrderform(event){
      el = Event.element(event);
      for (var i = 1; i < cartids; i++) { 
        if($('irow_'+i) && $('irow_'+i).getStyle('display')!='none'){
          notice = "vyber účel";
          notice2 = "vyplň formát";
          notice3 = "vyplň dohodnutou cenu";
          notice4 = "vyber formát";
          val = $('purpose_'+i).value.substring(0, 2);
          if (val == 'no') {
            alert(notice); $('purpose_'+i).focus(); Event.stop(event); return false;
          } else if (val == 'de') {
            if ($('format_'+i).value < 1) {alert(notice2); $('format_'+i).focus(); Event.stop(event); return false;}
          } else if (val == 're') {
            if ($('format_'+i).value < 2) {alert(notice2); $('format_'+i).focus(); Event.stop(event); return false;}
            if ($('price_'+i).value < 3) {alert(notice3); $('price_'+i).focus(); Event.stop(event); return false;}
          } else if (val == 'pp') {
            if ($('ppformat_'+i).value == 'none') {alert(notice4); $('ppformat_'+i).focus(); Event.stop(event); return false;}
          } else if (val == 'pn') {
            if ($('pnformat_'+i).value == 'none') {alert(notice4); $('pnformat_'+i).focus(); Event.stop(event); return false;}
          }
        }
      }
      radio = 0;
      for (i = 0; i <=el.delivery.length-1; i++) {
        if(el.delivery[i].checked){
          radio = 1;
        }
      }      
      if (radio < 1) {alert('vyber způsob předání objednaných fotografií'); Event.stop(event); return false;}
      if (el.accepted.checked != true) {alert('musíte souhlasit s licenční smlouvou'); Event.stop(event); return false;}

    }
    $('order').observe('submit',validateOrderform);
  }

  if($('image')){
    function showImage(seq, source, event) {
      count = source.length;
      if (seq!=null)
      {
        $('image').update();
        $('desc').update();
        tabHeight = document.viewport.getDimensions().height;
    		yScroll = document.viewport.getScrollOffsets().top;
    		if (window.innerHeight && window.scrollMaxY) {	
    			detailScroll = window.innerHeight + window.scrollMaxY;
    		} else if (document.body.scrollHeight > document.body.clientHeight){
    			detailScroll = document.body.scrollHeight;
    		} else {
    			detailScroll = document.body.clientHeight;
    		}
        $('foto').setStyle({height: detailScroll+'px'});
        $('tab').setStyle({top: yScroll+'px'});
        $('tab').setStyle({height: tabHeight+'px'});
        $('fotoWrap').setStyle({visibility:'visible'});
        img = new Element('div');
        img.setStyle('background:url(' + source[seq].href + ') center center no-repeat;');
        img.setStyle('width:' + source[seq].width  + 'px;');
        img.setStyle('height:' + source[seq].height  + 'px;');
        if (seq != 0) {
          $('prev').setStyle({display: 'inline'});
          Event.stopObserving($('prev'),'click');
          Event.observe($('prev'),'click',function(event){
            seq--; 
            showImage(seq, source);
            Event.stop(event);
          }, false);
        } else {
          $('prev').setStyle({display: 'none'});
          Event.stopObserving($('prev'),'click');
        }
        if (seq < count-1) {
          $('next').setStyle({display: 'inline'});
          Event.stopObserving($('next'),'click');
          Event.observe($('next'),'click',function(event){
            seq++; 
            showImage(seq, source);
            Event.stop(event);
          }, false);
        } else {
          $('next').setStyle({display: 'none'});
          Event.stopObserving($('next'),'click');
        }
        $('image').appendChild(img);  
        $('desc').update(source[seq].desc);
        if(event) {
          event.stop();
        } else {
          return false;
        }
      }
    }

    function hideSlider() {
      Event.stopObserving($('prev'),'click');
      Event.stopObserving($('next'),'click');
      $('image').update();
      $('desc').update();
      $('fotoWrap').setStyle({visibility:'hidden'});
      return false;
    }

    Event.observe($('image'),'click',function(event){
      hideSlider();
      Event.stop(event); 	
  	}, false);		 
  }

  if($('latest_photos')){
    for (var i = 1; i <= 20; i++) {
      if($('latest_'+i)){
        Event.observe($('latest_'+i),'click',showImage.curry(i-1,latest_holder), false);
      }      
    }

  //  var change_seq = ['3','8','2','5','10','4','9','6','1','7','4','8','3','6','9','5','2','7','1','10']; 
      var change_seq = ['3','18','12','5','20','14','9','16','1','11','17','8','13','6','19','2','15','4','7','10']; 
    
    function img_change(element, current, source) {
      el_id = element+'_'+change_seq[seq_pos];
      count = source.length;      
      if (count <= 1)
      	return true;
    
      new Effect.Fade($(el_id).firstDescendant(),
        { queue: {scope: 'changeimg', position: 'end' },
        afterFinish: function() { 
          a = new Element('a', { href: source[current].href  });
          img = new Element('img', { src: source[current].url, alt: source[current].alt });
          Event.stopObserving($(el_id).firstDescendant(),'click');
          $(el_id).update(); 
          a.setStyle({display:'none'});
          img.setStyle({marginTop: source[current].mng});
          a.appendChild(img);
          $(el_id).appendChild(a);
          Event.observe($(el_id).firstDescendant(),'click',showImage.curry(current,source), false);
          new Effect.Appear($(el_id).firstDescendant(),
           { queue: {scope: 'changeimg', position: 'end', delay: 3 }
          });   
        }
      });    
    	current++;
      seq_pos++;
    	if (current>=count) current = 0;
    	if (seq_pos>=change_seq.length) seq_pos = 0;	
    	return current;    
    }

		latest_holder.each(function(el) {
			tmp = new Image();
			tmp.src = el.url;
		});	 
	  new PeriodicalExecuter( function() { current = img_change('latest', current, latest_holder); },6 );
  }

  if($('thumbnails')){
    for (var i = 0; i < images.length; i++) {
      if($('image_'+i)){
        Event.observe($('image_'+i),'click',showImage.curry(i,images), false);
      }      
    }
  }


});
