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";
  }
  else {
    document.getElementById(active).style.height = "auto";
  }
  document.getElementById(active+"_odkaz").style.color = "#0000FF";
  document.getElementById(deactive).style.visibility = "hidden";
  document.getElementById(deactive).style.height = "0";
  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() {
  
  $(".js").each(function (i) {
    $(this).css({
	    'display': 'block'
    });
  })
  
  $(".non-js").each(function (i) {
    $(this).css({
	    'display': 'none'
    });
  })
  
  $("#fakturacni_udaje_jine").click(function () {
    
    if ($(this).is(":checked")) {
      $("#fakturacni_udaje").show("slow");
    }
    else {
      $("#fakturacni_udaje").hide("slow");
    }
    
  })
  
  //kosik
  $("#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;
    }
    
    
  });
  
  
  
  
  $('#product-tabs').tabs({ fx: { opacity: 'toggle' } });


  //pridani do kosiku z detailu
  
  $(".add_basket").submit(function() {
		
		var id = $(this).attr('id');
		
		var productIDSplitter = $(this).attr('id').split("_");
		var productID         = productIDSplitter[1];
		
		var design = productID.slice(-2);
		var nasobek = Math.ceil(design / 3);
		var speed = 1000 + nasobek * 700;
		
		var productX 	= $("#img_" + productID).offset().left;
		var productY 	= $("#img_" + productID).offset().top;
		
		var basketX 	= $("#img-kosik").offset().left;
		var basketY 	= $("#img-kosik").offset().top;	
		
		var gotoX 		= basketX - productX;
		var gotoY 		= basketY - productY;
		
		var newImageWidth 	= $("#img_" + productID).width() / 3;
		var newImageHeight	= $("#img_" + productID).height() / 3;
		
		$("#img_" + productID)
		.clone()
		.prependTo("#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', $("#a_" + 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");

	
});

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;
}

