function in_array(needle, haystack, argStrict) {
  // Checks if the given value exists in the array
  var key = '', strict = !!argStrict;
  if (strict) {
    for (key in haystack) {
      if (haystack[key] === needle) {
        return true;
      }
    }
  } else {
    for (key in haystack) {
      if (haystack[key] == needle) {
        return true;
      }
    }
  }
  return false;
}

function zalozka(active, deactive) {
  document.getElementById(active).style.visibility = "visible";
  if (active == 'vyrobce') {
    //document.getElementById(active).style.height = "400px";
    document.getElementById(active).style.height = "auto";
  }
  else {
    document.getElementById(active).style.height = "auto";
  }
  //document.getElementById(active+"_odkaz").style.color = "#0000FF";
  document.getElementById(active+"_odkaz").className = "active";
  document.getElementById(deactive).style.visibility = "hidden";
  document.getElementById(deactive).style.height = "0";
  document.getElementById(deactive+"_odkaz").className = "";
  //document.getElementById(deactive+"_odkaz").style.color = "#000066";
  var dny = 7;
  var denExp = new Date();
  denExp.setTime (denExp.getTime() + (86400 * 1000 * dny));
  document.cookie = "smarty_menu = " + active + "; expires=" + denExp.toGMTString() + "; path=/";
}

$.postForm = function (url, form, success) {
	return this.ajax({
		type: 'POST',
		url: url,
		data: form.serialize(),
		success: success
	});
};

function zobrazitCenuDopravy(data) {
	$('#cena-doruceni').text(data);
	return false;
}

function prepocitejKosik (data) {
  
  //alert (data);
  if (data < 0) {
    return false;
  }
  
  $("#kosik-cena").fadeOut("def");
  
  $('#kosik-cena').text(data);
  
  $("#kosik-cena").fadeIn("def");
  return false;
}

$(document).ready(function() {
  
	if ($('.js').length != 0) {
	$(".js").each(function (i) {
	  $(this).css({
	    'display': 'block'
	  });
	})
	}
	if ($('.non-js').length != 0) { 
	$(".non-js").each(function (i) {
	  $(this).css({
	    'display': 'none'
	  });
	})
	}
	
  if ($('#slider').length != 0) {
		$('#slider').slides({
			preload: true,
			preloadImage: 'img/loading.gif',
			play: 6000,
			pause: 6500,
			slideSpeed: 2000,
			hoverPause: true
		});
	}
	
	if ($('#d_kontrola').length != 0) {
	  $('#d_kontrola').val('Praha');
	}
  
	if ($('.valign').length != 0) {
		$(".valign").vAlign();
	}
	
	if ($('#fakturacni_udaje_jine').length != 0) {
		$("#fakturacni_udaje_jine").click(function () {
		  if ($(this).is(":checked")) {
		    $("#fakturacni_udaje").show("slow");
		  }
		  else {
		    $("#fakturacni_udaje").hide("slow");
		  }
		})
	}
  
  //kosik
 if ($('#form-doruceni').length != 0) {
  $("#form-doruceni").submit(function() {
    
    var vybrano_doruceni = false;
    var vybrano_platba = false;
    
    if ( $("#zpusoby_doruceni input").is(":checked") && $("#zpusoby_platby input").is(":checked") ) {
      return true;
    }
    else {
      alert ('Vyberte prosím způsob doručení i platby.');
      return false;
    }
    
  });
 }
  
  //univerzalni scroll
 if ($('.scrool').length != 0) {
  if ($('.scrool').length != 0) {
  	$('.scrool').bind('click',function(event){
  		var $anchor = $(this);
  		$('html, body').stop().animate({
  			scrollTop: $($anchor.attr('href')).offset().top
  		}, 1500,'easeInOutExpo');
  		event.preventDefault();
  	});
  };
 }
  
  //prepinani detailu ve v2
 if ($('.v2_dalsi_designy').length != 0) {
  $(".v2_dalsi_designy").click(function () {
    
    var id = $(this).attr('id'); //v2_design2_xx
    var productIDSplitter = $(this).attr('id').split("_");
		var productID         = productIDSplitter[2];
    
    $(".v2_design").hide("fast");
    $(".share").hide("fast");
    $("#v2_design" + productID).show("slow");
    $(".share").show("slow");
    
    $('.dalsi_zbozi div').removeClass('active');
    $('#' + id).addClass('active');
    
    //scroll na detail
    $('html, body').animate({
			scrollTop: $('#detail_produktu').offset().top - 20
		}, 500,'easeInOutExpo');
    
    //$('.dalsi_zbozi div input').attr('checked', false);
    //$('#' + id + '  input').attr('checked', true);
    
    //alert(id + " " + productID);
    return false;
  });
 }
  
 if ($('#product-tabs').length != 0) {
  $('#product-tabs').tabs({ fx: { opacity: 'toggle' } });
 }
	
	//simulace kliknuti na taby pro horni menu v detailu
 if ($('#detail-top-menu').length != 0) {
	$("#detail-top-menu a").click(function () {
		if (!$(this).attr('id')) {
			return;
		}
		var Splitter = $(this).attr('id').split("-");
		var param    = Splitter[1];
		$("#tabs-item-" + param).click();
	});
 }
	
if ( !($.browser.msie && parseInt($.browser.version, 10) == 6) ) {
	
  //pridani do kosiku z detailu
 if ($('.add_basket').length != 0) {
  $(".add_basket").submit(function() {
		
		var id = $(this).attr('id');
		
		var productIDSplitter = $(this).attr('id').split("_");
		var productID         = productIDSplitter[1];
		
		var speed = 1700; //rychlost, jakou leti obrazek do kosiku
		var deleno = 50; //na jakou velikost se letici obrazek zmensi
		
		var productX 	= $("#v2_img_" + productID).offset().left;
		var productY 	= $("#v2_img_" + productID).offset().top;
		//alert(1);
		var basketX 	= $("#kosik-header").offset().left;
		var basketY 	= $("#kosik-header").offset().top;
		
		var gotoX 		= basketX - productX;
		var gotoY 		= basketY - productY;
		
		var newImageWidth 	= $("#v2_img_" + productID).width() / deleno;
		var newImageHeight	= $("#v2_img_" + productID).height() / deleno;
		
		$("#v2_img_" + productID)
		.clone()
		.prependTo("#v2_div_" + productID)
		.css({'position' : 'absolute'})
		.animate({opacity: 0.8}, 50 )
		.animate({opacity: 0.5, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, speed, function() {
      $(this).remove();
		});
		
		$.postForm('../../script/kosik-pridat.php', $("#v2_" + productID), prepocitejKosik);
		
		return false;
		
	});
 }
}
	
	
	$("a[href$='.pdf/']").addClass("pdf");
	$("a[href$='.avi/']").addClass("avi");
	$("a[href$='.zip/'], a[href$='.rar/']").addClass("zip");
	$("a[href$='.gif/']").addClass("gif");
	$("a[href$='.jpg/'], a[href$='.jpeg/']").addClass("jpg");
	$("a[href$='.htm/'], a[href$='.html/']").addClass("html");
	
  if ($('.form_check').length != 0) {
    $(".form_check").each(function (i) {
      $(this).validate({
        invalidHandler: function(form, validator) {
          var errors = validator.numberOfInvalids();
          if (errors) {
            var message = 'Vyplňte prosím povinné položky';
            $("div.error").html(message);
            $("div.error").show();
          } else {
            $("div.error").hide();
          }
        }
      })
    })
  }
  
  
  //nakupni kosik
if ($('#form-doruceni').length != 0) {

$("#form-doruceni").css({
	'display': 'block'
});

$(function () {
	$("#zpusoby_doruceni input:checked").each(function (i) {
    var id = $(this).attr('id');
    $("#zpusoby_platby input").each(function (i) {
      var id2 = $(this).attr('id');
      if (in_array(id2, doruceni[id])) {
        $(this).removeAttr('disabled');
      }
      else {
        $(this).removeAttr('checked');
        $(this).attr({
	        'disabled': 'disabled'
        });
        $("#label_" + id2).css({
	        'color' : '#6F6F6F'
        });
      } 
    });
  });
  
  $("#zpusoby_platby input:checked").each(function (i) {
    var id = $(this).attr('id');
    $("#zpusoby_doruceni input").each(function (i) {
      var id2 = $(this).attr('id');
      if (in_array(id2, platby[id])) {
        $(this).removeAttr('disabled');
      }
      else {
        $(this).removeAttr('checked').attr({
	        'disabled': 'disabled'
        });
        $("#label_" + id2).css({
	        'color' : '#6F6F6F'
        });
      } 
    });
  });
  
  $("#zpristupnit-volby").click(function () {
    $("#zpusoby_platby input").each(function (i) {
      var id2 = $(this).attr('id');
      $(this).removeAttr('disabled').removeAttr('checked');
      $("#label_" + id2).css({
	      'color' : '#000066'
      });
    });
    
    $("#zpusoby_doruceni input").each(function (i) {
      var id2 = $(this).attr('id');
      $(this).removeAttr('disabled').removeAttr('checked');
      $("#label_" + id2).css({
	      'color' : '#000066'
      });
    });
    
    vybrano_doruceni = false;
    vybrano_platba = false;
    zobrazitCenuDopravy('-');
    
    return false;
  });
  
  $("#zpusoby_doruceni input:checkbox").click(function () {
    var id = $(this).attr('id');
		if ($(this).is(":checked")) {
      vybrano_doruceni = id;
      $("#zpusoby_doruceni input:checked").each(function (i) {
        var id2 = $(this).attr('id');
        if (id != id2) {
          $(this).removeAttr('checked');
        }
      });
      
      $("#zpusoby_platby input").each(function (i) {
        var id2 = $(this).attr('id');
        if (in_array(id2, doruceni[id])) {
          $(this).removeAttr('disabled');
          $("#label_" + id2).css({
	         'color' : '#000066'
          });
        }
        else {
          $(this).removeAttr('checked').attr({
	         'disabled': 'disabled'
          });
          $("#label_" + id2).css({
	         'color' : '#6F6F6F'
          });
        }
      });
      if (vybrano_platba && vybrano_doruceni) {
        $.postForm('../script/doprava.php', $("#form-doruceni"), zobrazitCenuDopravy);
      }
      
    }
    else {
      vybrano_doruceni = false;
      $("#zpusoby_platby input").each(function (i) {
        var id2 = $(this).attr('id');
        $(this).removeAttr('disabled');
        $("#label_" + id2).css({
	        'color' : '#000066'
        });
      });
      zobrazitCenuDopravy('-');
    }
	});
	
	$("#zpusoby_platby input:checkbox").click(function () {
    var id = $(this).attr('id');
		if ($(this).is(":checked")) {
      vybrano_platba = id;
      $("#zpusoby_platby input:checked").each(function (i) {
        var id2 = $(this).attr('id');
        if (id != id2) {
          $(this).removeAttr('checked');
        }
      });
      
      $("#zpusoby_doruceni input").each(function (i) {
        var id2 = $(this).attr('id');
        if (in_array(id2, platby[id])) {
          $(this).removeAttr('disabled');
          $("#label_" + id2).css({
	         'color' : '#000066'
          });
        }
        else {
          $(this).removeAttr('checked').attr({
	         'disabled': 'disabled'
          });
          $("#label_" + id2).css({
	         'color' : '#6F6F6F'
          });
        }
      });
      if (vybrano_platba && vybrano_doruceni) {
        $.postForm('../script/doprava.php', $("#form-doruceni"), zobrazitCenuDopravy);
      }
    }
    else {
      vybrano_platba = false;
      $("#zpusoby_doruceni input").each(function (i) {
        var id2 = $(this).attr('id');
        $(this).removeAttr('disabled');
        $("#label_" + id2).css({
	        'color' : '#000066'
        });
      });
      zobrazitCenuDopravy('-');
    }
	});
});
}
  //nakupni kosik end
	
});

/*
jQuery(function($){
	
	// addSizes was written by Natalie Downe 
	// http://natbat.net/2008/Aug/27/addSizes/
	
	// Copyright (c) 2008, Natalie Downe under the BSD license
	// http://www.opensource.org/licenses/bsd-license.php
	
	$('a[href$=".pdf/"], a[href$=".avi/"], a[href$=".zip/"], a[href$=".rar/"], a[href$=".gif/"], a[href$=".jpg/"], a[href$=".jpeg/"], a[href$=".htm/"], a[href$=".html/"]').each(function(){

		var link = $(this);
		var bits = this.href.split('.');
		var type = bits[bits.length -1];
		
		var url= "http://json-head.appspot.com/?url="+encodeURIComponent (this.href)+"&callback=?";
	
		// then call the json thing and insert the size back into the link text
		 $.getJSON(url, function(json){
			if(json.ok && json.headers['Content-Length']) {
				var length = parseInt(json.headers['Content-Length'], 10);
				
				// divide the length into its largest unit
				var units = [
					[1024 * 1024 * 1024, 'GB'],
					[1024 * 1024, 'MB'],
					[1024, 'KB'],
					[1, 'bytes']
				];
				
				for(var i = 0; i < units.length; i++){
					
					var unitSize = units[i][0];
					var unitText = units[i][1];
					
					if (length >= unitSize) {
						length = length / unitSize;
						// 1 decimal place
						length = Math.ceil(length * 10) / 10;
						var lengthUnits = unitText;
						break;
					}
				}
				
				// insert the text directly after the link and add a class to the link
				// note: if you want to insert the size into the link rather than after it change the following 'after' to 'append'
				link.after(' (' + type + ' ' + length + ' ' + lengthUnits + ')');
				link.addClass(type);
			}
		});
	});
});
*/


function Kontrola() { /*diskuze*/
 if ( document.form.d_jmeno.value == '' || document.form.d_nadpis.value == '' || document.form.d_text.value == '' ){
	alert('Vyplňte prosím povinné pole označené " * "');
	return false;
 }
return true;
}


