//kalendar v historie objednavok
$(function() {	
	// zadefinujeme si slovensku lokalizaciu pre datepicker
	$.datepicker.regional['sk'] = {
		closeText: 'Zavrieť',
		prevText: '&#x3c; Predchádzajúci',
		nextText: 'Nasledujúci &#x3e;',
		currentText: 'Dnes',
		monthNames: ['Január','Február','Marec','Apríl','Máj','Jún',
		'Júl','August','September','Október','November','December'],
		monthNamesShort: ['Jan','Feb','Mar','Apr','Máj','Jún',
		'Júl','Aug','Sep','Okt','Nov','Dec'],
		dayNames: ['Nedel\'a','Pondelok','Utorok','Streda','Štvrtok','Piatok','Sobota'],
		dayNamesShort: ['Ned','Pon','Uto','Str','Štv','Pia','Sob'],
		dayNamesMin: ['Ne','Po','Ut','St','Št','Pia','So'],
		weekHeader: 'Ty',
		dateFormat: 'dd.mm.yy',
		firstDay: 0,
		isRTL: false,
		showMonthAfterYear: false,
//			showButtonPanel: true,
		yearSuffix: ''};
		//setneme sk nastavenia ako defaultne
	$.datepicker.setDefaults($.datepicker.regional['sk']);
	
	$(".datepicker").datepicker();

	//LIGHTBOX
	$('a[rel*=lightbox]').lightBox({
		txtImage: 'Obrázok',
		txtOf: '/'
	});	 
	
	//change form for signup or signout
	$('#newsletter_link_signoff').click(function() {									 	
		$('#sign_on').fadeOut(function() {
			$('#sign_off').fadeIn();							   
		});
	});
	
	$('#newsletter_link_signon').click(function() {									 	
		$('#sign_off').fadeOut(function() {
			$('#sign_on').fadeIn();							   
		});
	});
});

function image_title() {
	// hmm... asi bude treba urlencoding natvrdo :(
	
	//document.getElementById('green2').innerHTML = '<img src="includes/imageheader.php?text=' + document.getElementById('green2').innerHTML + '" />'
}

function show_accessories(idcko) {
	sect = document.getElementById('odp_mob_' + idcko);
	
	if(sect.style.display == "none")
		sect.style.display = "block";
	else
		sect.style.display = "none";
}

function starover(k){							
	for(i = 1; i <= k; i++)
		document.getElementById('star'+i).src = document.getElementById('star'+i).src.replace('emptystar.jpg', 'fullstar.jpg');
}
function starout(){							
	for(i = 1; i <= 5; i++)
		document.getElementById('star'+i).src = document.getElementById('star'+i).src.replace('fullstar.jpg', 'emptystar.jpg');
}

function hlasuj(k) {
	document.location.href = '?hlasovanie=' + k;
}

function popupWindowFitImage(url) {
  window.open(url,'popupWindow',"resizable=yes, location=no");
}

// otvorenie nahladu na tlac
function print_preview(id) {
  window.open('nahlad-na-tlac-' + id, 'preview', 'toolbar=no,menubar=no,location=no,directories=no,scrollbars=yes,resizable=no,status=no,height=600,width=625');
}

// otvorenie obrazku v novom okne - picture_show.php
function show_picture(source, path, comment) {
  path = (path ? path : "");
  window.open(path + "picture_show.php?source=" + source + "&comment=" + ((comment == null) ? "" : comment), 'picture', "toolbar=no,menubar=no,location=no,directories=no,scrollbars=no,resizable=no,status=no");
}

// otvorenie okna na porovnanie produktov
function compare_product(prod_id) {
  window.open('compare.php?prod0=' + prod_id, 'compare', 'toolbar=no,menubar=no,location=no,directories=no,scrollbars=yes,resizable=no,status=no,height=600,width=750');
}

// otvorenie okna na odoslanie emailu
function open_email_form(typ, param) {
    window.open('mail-' + typ + '-' + param, 'email', 'toolbar=no,menubar=no,location=no,directories=no,scrollbars=no,resizable=no,status=no,height=470,width=530');
}

// funkcia na submitnutie enterom
function submitenter(myfield, e) {
  var keycode;
  if(window.event) 
    keycode = window.event.keyCode;
  else {
    if(e) 
      keycode = e.which;
  }

  if(keycode == 13)
    myfield.form.submit();
}

// bookmark page
function bookmark_page(title, linka) {
  // Mozilla Firefox Bookmark
  if(window.sidebar)
    window.sidebar.addPanel(title, linka,"");
  
  // IE Favorite
  else if(window.external)
    window.external.AddFavorite(linka, title);
  
  // Opera Bookmark
  else if(window.opera && window.print) {
    var obm = document.createElement('a');
    obm.setAttribute("title", title);
	obm.setAttribute("href", linka);
	obm.setAttribute("rel", "sidebar");    
    obm.click();
  }
}

//         <- ONMOUSEOVER PRE BUTTONY ->

function over(obj) {
  //var thisimg = document.getElementById(imgID);
  if(obj.src.substring(obj.src.length-9, obj.src.length) == "blank.gif") obj.style.filter = obj.style.filter.replace('.png','-hover.png');
  else obj.src = obj.src.replace(/(\.[a-z0-9]+)$/i,'-hover$1');
  obj.style.cursor= "pointer";
}
function out(obj) {
  //var thisimg = document.getElementById(imgID);
  //thisimg.src = thisimg.src.replace(/-hover(\.[a-z0-9]+)$/i,'$1');
 if(obj.src.substring(obj.src.length-9, obj.src.length) == "blank.gif")
  	obj.style.filter = obj.style.filter.replace('-hover.png','.png');
  else
  	obj.src = obj.src.replace(/-hover(\.[a-z0-9]+)$/i,'$1');
}

// SELECT BOX
(function( $ ) {
	$.widget( "ui.combobox", {
		_create: function() {
			var self = this,
				select = this.element.hide(),
				selected = select.children( ":selected" ),
				value = selected.val() ? selected.text() : "";
			var input = this.input = $( "<input>" )
				.insertAfter( select )
				.val( value )
				.autocomplete({
					delay: 0,
					minLength: 0,
					source: function( request, response ) {
						var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
						response( select.children( "option" ).map(function() {
							var text = $( this ).text();
							if ( this.value && ( !request.term || matcher.test(text) ) )
								return {
									label: text.replace(
										new RegExp(
											"(?![^&;]+;)(?!<[^<>]*)(" +
											$.ui.autocomplete.escapeRegex(request.term) +
											")(?![^<>]*>)(?![^&;]+;)", "gi"
										), "<strong>$1</strong>" ),
									value: text,
									option: this
								};
						}) );
					},
					select: function( event, ui ) {
						ui.item.option.selected = true;
						self._trigger( "selected", event, {
							item: ui.item.option
						});
					},
					change: function( event, ui ) {
						if ( !ui.item ) {
							var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
								valid = false;
							select.children( "option" ).each(function() {
								if ( $( this ).text().match( matcher ) ) {
									this.selected = valid = true;
									return false;
								}
							});
							if ( !valid ) {
								// remove invalid value, as it didn't match anything
								$( this ).val( "" );
								select.val( "" );
								input.data( "autocomplete" ).term = "";
								return false;
							}
						}
					}
				})
				.addClass( "ui-widget ui-widget-content ui-corner-left nohighlight" );

			input.data( "autocomplete" )._renderItem = function( ul, item ) {
				return $( "<li></li>" )
					.data( "item.autocomplete", item )
					.append( "<a>" + item.label + "</a>" )
					.appendTo( ul );
			};

			this.button = $( "<button type='button'>&nbsp;</button>" )
				.attr( "tabIndex", -1 )
				.attr( "title", "Show All Items" )
				.insertAfter( input )
				.button({
					icons: {
						primary: "ui-icon-triangle-1-s"
					},
					text: false
				})
				.removeClass( "ui-corner-all" )
				.addClass( "ui-corner-right ui-button-icon " )
				.click(function() {
					// close if already visible
					if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
						input.autocomplete( "close" );
						return;
					}

					// pass empty string as value to search for, displaying all results
					input.autocomplete( "search", "" );
					input.focus();
				});
		},

		destroy: function() {
			this.input.remove();
			this.button.remove();
			this.element.show();
			$.Widget.prototype.destroy.call( this );
		}
	});
})( jQuery );

$(function() {
	$( ".combobox" ).combobox();
	$( "#toggle" ).click(function() {
		$( ".combobox" ).toggle();
	});
});

