(function($){
	$.fn.scaleImage = function(options) {
	 
		var defaults = {
			maxwidth: 200,
            maxheight: 200,
			linkclass:'',
			icon:true,
			thickbox:true
		};

		var options = $.extend(defaults, options);

		return this.each(function() {

			obj = $(this);
			
			obj.removeAttr("width").removeAttr("height").css({ width: "", height: "" });
			 

			var width = obj.width();
			var height = obj.height();
			
			//alert("W:"+width+" H:"+height);
							
			if((width > options.maxwidth) || (height > options.maxheight)) {
				
				var scaleH = options.maxheight / height;
				var scaleW = options.maxwidth / width;
				
				//alert('H: '+scaleH+' W: '+scaleW);

				if(scaleH < scaleW) {
					new_height = options.maxheight;
					new_width = Math.round(width * scaleH);
				} else {
					new_height = Math.round(height * scaleW);
					new_width = options.maxwidth;
				}

				//if (width > options.maxwidth) {
				//Set variables for manipulation
				//var ratio = (height / width );
				//var new_width = options.maxwidth;
				//var new_height = (new_width * ratio);
				var classes = options.linkclass+' scaleImage';
				 
				//thickbox
				if (options.thickbox == true) {
					var img_full_link = obj.attr('src');
					obj.wrap('<a class="thickbox" title="'+obj.attr('alt')+'" href="'+img_full_link+'"></a>');
					tb_init(obj.parent('a'));
				}
				 
				//Shrink the image and add link to full-sized image
				obj.height(new_height).width(new_width);
				obj.addClass(classes);
				 
				//zoom icon
				if (options.icon == true) {
					obj.after('<div class="thumb-zoom"> </div>');
					obj.hover(function(){
						$(this).next('.thumb-zoom').addClass("hover");
					},function(){
						$(this).next('.thumb-zoom').removeClass("hover");
					});
				}
			 
			}
		});
	};
})(jQuery);

/*$(window).load(function() {
	$("#big-image").scaleImage({maxwidth: 268, maxheight: 246, icon: false, thickbox: false});
	$("img", ".product-thumb").scaleImage({maxwidth: 90, maxheight: 60, icon: false, thickbox: false});
    $("img", ".product-image").scaleImage({maxwidth: 178, maxheight: 120, icon: false, thickbox: false});
	$("img", ".cart-product-img").scaleImage({maxwidth: 90, maxheight: 88, icon: false, thickbox: false});
	$("img", ".cart-block-image").scaleImage({maxwidth: 63, maxheight: 63, icon: false, thickbox: false});
});*/

function changeImage(src) {
	var img = document.getElementById('big-image');
  //var link = document.getElementById( 'big-image-link' );
	img.setAttribute('src', src);
  //link.setAttribute( 'href', src );
	//$("#big-image").scaleImage({maxwidth: 268, maxheight: 246, icon: false, thickbox: false});
}

function checkExtraField(id, msg) {
	if($('#'+id).val() == '') {
		alert(msg);
		return false;
	}
	return true;
}

function jqFormValidate() {
	var isValid = true;
	$(".req input").each(function(i, v) {
		if($(this).val() == '')
		{
			isValid = false;
		}
	});
	if(!isValid) {
		alert("Visi laukai pažymėti '*' yra privalomi.");
	}
	return isValid;
}

function showHideBlock(id) {
	if($('#'+id).css('display') == 'none') {
		$('#'+id).css('display', 'block');
	} else {
		$('#'+id).css('display', 'none');
	}
}


/** autocomplete */

$(function() {
	
	var cache = {},
		lastXhr;
	$( "#search .search-input" ).autocomplete({
		minLength: 3,
		source: function( request, response ) {
			var term = request.term;
			if ( term in cache ) {
				response( cache[ term ] );
				return;
			}
            
			lastXhr = $.getJSON( "autosearch?showtemplate=false", request, function( data, status, xhr ) {
				cache[ term ] = data;
				if ( xhr === lastXhr ) {
					response( data );
				}
			});
		},
		html: true,
		position: {
	        my: "left top",
	        at: "left bottom",
	        offset: "-160 11"
	    },
		select: function( event, ui ) {
			/*$( "#project" ).val( ui.item.label );
			$( "#project-id" ).val( ui.item.value );
			$( "#project-description" ).html( ui.item.desc );
			$( "#project-icon" ).attr( "src", "images/" + ui.item.icon );*/
			location.href = ui.item.value;
			return false;
		}
	});
	
});

/***** colorbox ****/
jQuery( function( $ ){
  var $gallery = $( 'a.lthumbs' ).colorbox({
    rel: 'group1',
    maxWidth: 800,
    maxHeight: 500,
    current: "paveiksliukas {current} iš {total}"
  });
  
  $( '#big-image-link' ).click( function( e ){
    e.preventDefault();
    //$gallery.eq(3).click();
    $gallery.eq( $( this ).attr( 'href' ) ).click();
  });
  
  $( '.product-thumb' ).mouseover( function( e ){
    $( '#big-image-link' ).attr( 'href', $( this ).index( '.product-thumb' ) );
  });
});
