(function( $ ){
 	$.fn.SubmitForms = function(options) {
		var settings = {
		};
		if ( options ) { 
			$.extend( settings, options );
		};
		return this.each(function() {
			$("button.Submit").live("click", function() {
				var action = $(this).val();
				var form = $(this).parents("form");
				$(form).prepend('<input type="hidden" name="action" value="'+action+'"/>');
				$(form).submit();
			})
		});
	};	
})( jQuery );

$(document).ready(function() {
	$(".header-search").whisperer({
		search_input : '.search-input',
		position_left: 0,
		position_top: 35
	});
	// form submit
	$("button.Submit").SubmitForms();
	$(".ax-cancel a").live("click", function() {
		ShowLoading('hide');
		return false;
	})
  
  // menu select
  sortList('limitedlevels');

  $('#detail .leva .dalsi-img a').attr('onclick','');
  $('#detail .leva .obr a').attr('rel','fancy');
  $('#detail .leva .dalsi-img a').attr('rel','fancy');
  $("#detail .leva .obr a").fancybox();
  $("#detail .leva .dalsi-img a").fancybox();
  $("#doprava area").fancybox();

  $("#detail .detail-sklad a,#detail .detail-nesklad a").fancybox({
    'autoScale'     	: true,
    'transitionIn'		: 'none',
    'transitionOut'		: 'none',
    'type'				: 'iframe'
  });

  $('#detail .alternativa tr.line').hover(function() {
    var img = $(this).attr('rel');
    var append = '<img class="product-preview" src="'+img+'" />';
    $(this).children('td.nazev').append(append);
  }, function() {
    $('img.product-preview').remove();
  });
  
  $('a.link-fulltext').click(function() {
    var search = $(this).attr('title');  
    $('#searchphrase').val(search);
    $('#ctl01_simplesearch').submit();
    return false;
  });
});

function display(myimage) {
return false;
html = "<HTML><HEAD>" 
+ "<style type=text/css>body{margin:0;padding:0;}</style>"
+ "</HEAD><BODY> <CENTER>" 
+ "<a href='javascript:window.close();'><IMG SRC='" + myimage + "' BORDER=0 NAME=image " 
+ "onload='window.resizeTo(document.image.width+50,document.image.height+70)' /></a>"
+ "</CENTER>" 
+ "</BODY></HTML>";
popup=
window.open
('','image','toolbar=0,location=0,directories=0,menuBar=0,scrollbars=0,resizable=1');
popup.document.open();
popup.document.write(html);
popup.document.close()
};

/* puvodni JS z MN.cz */
var zobrazena="data";
function zalozky(polozka, obj ){
  if( polozka != zobrazena ){
    if(document.getElementById(zobrazena)){
      document.getElementById(zobrazena).style.display= "none";
      document.getElementById(zobrazena+"_1").className="";
    }
    document.getElementById(polozka).style.display= "block";
    obj.className="active";
    zobrazena=polozka;
  }
}


function mena( mena ){
  var id;
  if(mena == 'eur') {
    // EURO
    id = 7;
  } else {
    // CZK
    id = 1;
  }
  location.href= '/inshop/scripts/shop.aspx?action=DoChangeCurrency&CurrID=' + id;
}


function openWindow( src ){
  window.open(src ,"pict",'toolbar=0,location=0,scrollbars=0,width=500,height=450,resizable=0,top=20,left=20');
}


function menu( iDdmenu ){
  var blok = '';
  if( document.getElementById( 'lvlID_' + iDdmenu ) ){
    blok = document.getElementById( 'lvlID_' + iDdmenu );
  }
  if( document.getElementById( iDdmenu ) ){
    blok = document.getElementById( iDdmenu );
  }
  if( blok  != '' ){
    if( blok.style.display == "none" ){
        blok.style.display = "block";
    } else {
        blok.style.display = "none";
    }
    return false;
  }
}

function zmensit( Id ){
  var blok = document.getElementById( Id );
  if( document.getElementById( Id ) ){
    if( blok.style.visibility == "hidden" ){
        blok.style.visibility = "visible";
        blok.style.height = "135px";
    } else {
        blok.style.visibility = "hidden";
        blok.style.height = "0";
    }
    return false;
  }
}


function otevrit5(id) {
  if(document.getElementById(id)){
    document.getElementById(id).style.visibility = "visible";
    if(document.getElementById('youtube1')){
      document.getElementById('youtube1').style.visibility = "hidden";
    }
  }
}
function zavrit5(id) {
  if(document.getElementById(id)){
    document.getElementById(id).style.visibility = "hidden";
    if(document.getElementById('youtube1')){
      document.getElementById('youtube1').style.visibility = "visible";
    }
  }
}

function powerPartnerImg(par) {
  nodkaz=par.href;
  nobraz='/img/powerpartner-'+par.name+'.jpg';
  document.getElementById('vodkaz').href=nodkaz;
  document.getElementById('vobraz').src=nobraz;
  document.location='http://powerpartner.moderni-naradi.cz/#'+par.name;
  return false;
}

var _chars = ' aáäaaâbcćčdďeéěëeefghhiíiiîjklĺľmnńňnoóôooöőpqrŕřsśštťuúuůüuuvwxyýzźž0123456789'.toUpperCase();
function czechSort(arr) {
    var map = {};
    var index;

    for (var i = 0, item; i < arr.length; i++) { //pro každý prvek
        index = []; //reset indexu
        item = arr[i];
        item.replace("/\s+/g", " ");
        for (var j = 0, ch, pos; j < item.length; j++) {
            ch = item.substr(j, 1).toUpperCase();
            //test na ch
            if (j + 1 < item.length && ch == "C" && item.substr(j + 1, 1).toUpperCase() == "H") {
                pos = 22; //ch je na pozici 22
                j++;
            } else { //zjistíme správnou pozici písmene
                pos = _chars.indexOf(ch);
            }

            index.push(pos < 10? "0" + pos : pos); //potřebujeme 0 prefix
        }
        map[item] = index.join("");
    }
    return arr.sort(function(a, b) {
        var a2 = map[a];
        var b2 = map[b];
        if (a2 > b2) { return 1;}
        if (a2 < b2) { return -1;}
        return 0;
    });
}
function sortList(id) {
  var objParent = document.getElementById(id);
  var lis = objParent.getElementsByTagName("option");
  var vals = [];
  for(var i = 0, l = lis.length; i < l; i++) {
    txt=lis[i].innerHTML+'||'+lis[i].value+'||'+lis[i].getAttribute('selected');
    vals.push(txt);
  }
  vals=czechSort(vals);
  for(var i = 0, l = lis.length; i < l; i++) {
    txt=vals[i];
    pos=txt.indexOf('||');
    value=txt.substr(pos+2);
    txt=txt.substr(0,pos);

    pos=value.indexOf('||');
    sel=value.substr(pos+2);
    value=value.substr(0,pos);
    
    lis[i].value=value;
    if (sel=="selected") {
      lis[i].setAttribute("selected","selected");
    } else {
      lis[i].removeAttribute("selected");
    }
    lis[i].innerHTML = txt;
  }
}
