$(function() {
	$(".scrollable").scrollable();
	$(".items div img").click(function() {
		if ($(this).hasClass("active")) { return; } // see if same thumb is being clicked
		var url = $(this).attr("src").replace("small", "big"); // calclulate large image's URL based on the thumbnail URL (flickr specific)
		var title = $(this).attr("title"); // get image title
		var wrap = $("#image_wrap").fadeTo("medium", 0.5); // get handle to element that wraps the image and make it semi-transparent
		var img = new Image(); // the large image from www.flickr.com
		img.onload = function() { // call this function after it's loaded
			wrap.fadeTo("fast", 1); // make wrapper fully visible
			wrap.find("div").attr("style", "background:url('" + url + "') center center no-repeat;"); // change the image
			wrap.find("a").attr("href", url); // change the link url
			wrap.find("a").attr("title", title); // change the link title
		};
		img.src = url; // begin loading the image from www.flickr.com
		$(".items div img").removeClass("active"); // activate item
		$(this).addClass("active");
	}); // when page loads simulate a "click" on the first image
});
$(document).ready(function() {
	$("#select_sec").change(function() {
		// alert($("#select_sec option:selected").val());
		window.location = "http://www.terradarus.ru/sale"+$("#select_sec option:selected").val();
	});

	/* главная и страница товара */
	$("#single_image").fancybox({
		'titlePosition'		: 'outside',
		'overlayColor'		: '#000',
		'overlayOpacity'	: 0.9
	});
	/* Using custom settings */
	$("a#inline").fancybox({'hideOnContentClick': true});
	/* групповое, не знаю куда пока сунуть */
/* 	$("a.group").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false
	}); */
	$("a[rel='group']").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
		    return '<span id="fancybox-title-over">Image ' +  (currentIndex + 1) + ' / ' + currentArray.length + ' ' + title + '</span>';
		}
	});

	$("a#iframe").fancybox({
		'width'				: '50%',
		'height'			: '75%',
		'autoScale'     	: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});
	
	$("span.ctext1").text(declension($("span.cnumber1").text(), ["вариант", "варианта", "вариантов"]));
	$("span.ctext2").text(declension($("span.cnumber2").text(), ["вариант", "варианта", "вариантов"]));
	$("span.ctext3").text(declension($("span.cnumber3").text(), ["вариант", "варианта", "вариантов"]));
	$("span.ctext4").text(declension($("span.cnumber4").text(), ["вариант", "варианта", "вариантов"]));
	$("span.ctext5").text(declension($("span.cnumber5").text(), ["вариант", "варианта", "вариантов"]));
});

function declension(num, expressions) {
    var result;
    count = num % 100;
    if (count >= 5 && count <= 20) {
        result = expressions['2'];
    } else {
        count = count % 10;
        if (count == 1) {
            result = expressions['0'];
        } else if (count >= 2 && count <= 4) {
            result = expressions['1'];
        } else {
            result = expressions['2'];
        }
    }
    return result;
}

