var work = false; // interval
var li = null; // items
var lil = 0; // items.length
var sp = (sp?sp:40); // speed
var mycarousel_itemList = new Array();
var AMR = 0;
var tm = 0;

$(window).load(function() {
	initTopCarusel();
	changeFontSize($(document).width());
	ie6Width();
	
	catalog2hack();
});

$(window).resize(function() {
	initTopCarusel();
	changeFontSize($(document).width());
	ie6Width();
});

function ie6Width() {
	if ($.browser.msie) {
		if (parseInt($(document).width()) <= 1010) {
			$('div.body').width('1010px');
		}
		else {
			$('div.body').width('100%');
		}
	}
}

function changeFontSize(dw) {
	var dw = parseInt(dw);
	if (dw > 1200 && dw < 1400) {
		$('body').css('font-size', '14px');
	}
	else {
		if (dw < 1200) {
			if (dw > 1100) { 
				$('body').css('font-size', '12px');
			}
			else {
				if (dw > 1030) {
					$('body').css('font-size', '11px');
				}
				else {
					$('body').css('font-size', '10px');
				}
			}
		}
		else {
			if (dw > 1400 && dw < 1600) {
				$('body').css('font-size', '15px');
			}
			else {
				$('body').css('font-size', '16px');
			}
		}
	}
}

function catalog2hack() {
	var items = $('.catalog2 .row2 .item2');
	var itemsLength = items.length;
	for (var i = 0; i < itemsLength; i++) {
		var j = i + 1;
		if (j != itemsLength) {
			h1 = parseInt($(items[i]).height());
			h2 = parseInt($(items[j]).height());
			if (h1 > h2) {
				$(items[j]).height(h1);
			}
			else {
				$(items[i]).height(h2);
			}
		}
		i++;
	}
}

function initTopCarusel() {
	work = false;
	
	li = $('.headerList .item');
	lil = li.length;
	var allElsWidth = getAllWidth(0);
	var workWidth = parseInt($('.header').width());
	if (allElsWidth > workWidth) {
		li.each(function(i) {
			$(this).css('width',parseInt($(this).width()));
			AMR = parseInt($(this).css('margin-right'));
			$(this).css('position','absolute');
			$(this).css('top',0);
			$(this).css('left', getX(i));
			$(this).mouseover(function() {
				work = false;
				clearTimeout(tm);
			});
			$(this).mouseout(function() {
				if (!work) {
					tm = setTimeout(m, sp);
					work = true;
				}
			});
		});
		if (!work) {
			tm = setTimeout(m, sp);
			work = true;
		}
	}
}

function getX(i) {
	var el = $(li.get(i));
	var x = 0;
	for (j = 0; j < i; j++) {
		x += parseInt($(li.get(j)).width()) + parseInt($(li.get(j)).css('margin-right'));
	}
	return x;
}

function getAllWidth(i) {
	var wi = 0;
	if (i < lil && (wi = parseInt($(li.get(i)).width())) && (mr = parseInt($(li.get(i)).css('margin-right')))) {
		return wi + mr + getAllWidth(++i);
	}
	return wi;
}

function m() {
	li.each(function(i){
		var lp = parseInt(this.style.left) - 1;
		this.style.left = lp + 'px';
		var pos = parseInt(this.style.width) + parseInt(this.style.left);
		if (pos <= 0) { 
			var n = (i == 0 ? lil - 1 : i - 1); 
			var lp2 = parseInt((li.get(n)).style.left) + parseInt((li.get(n)).style.width) + AMR;
			this.style.left = lp2 + 'px';
		}
	});
	if (work) {
		tm = setTimeout(m, sp);
	}
	else {
		clearTimeout(tm);
	}
}














$(window).load(function() {
	$('#vprev').click(function () {variantGo('prev');return false;});
	$('#vnext').click(function () {variantGo('next');return false;});
});

var InnovationObject = null;
var emptyImage = "/templates/css/x.gif";


var VariantsObject = null;

var ENABLE_UPLOAD_MIDDLE_AND_SMALL_IMGS = true;
var IMGS_ROW_LENGTH = 4;

function initVariants(list) {
	var len = list.length; if ($.browser.msie) len--;
	
	if (ENABLE_UPLOAD_MIDDLE_AND_SMALL_IMGS) {
		for (i = 0; i < len; i++) {
			list[i].imgSrcSmall = imgSrcSmall[i].src;
			list[i].imgSrcLarge = imgSrcLarge[i].src;
		}
	}
	
	if (len <= IMGS_ROW_LENGTH) {
		$('#vnext').parent().css('visibility','hidden');
		$('#vprev').parent().css('visibility','hidden');
		var currentValue = -1;
	}
	else {
		$('#vprev').parent().css('visibility','hidden');
		var currentValue = - 1;
	}
	VariantsObject = {
		current:currentValue,
		items:list,
		length:function() {return this.items.length;},
		item:function() {return this.items[this.current]},
		next:function() {if (this.current + 1 < this.length()) this.current++; return this.item(); },
		prev:function() {if (this.current - 1 >= 0) this.current--; return this.item(); },
		selected:-1,
		select:function(id) {this.selected = id; return this.items[id];},
		getSelected:function() {return this.items[this.selected];}
	};
	
	for (i = 1; i <= IMGS_ROW_LENGTH && i <= VariantsObject.length(); i++) {
		$('#variants .ov' + i ).click(function() { 
			var id = $(this).attr('idInList'); 
			var el = VariantsObject.items[id]; 
			VariantsObject.select(id);
			$('.mainImg img').fadeOut('normal', function() {
				$('.mainImg img').attr('src',emptyImage); 
				$('.mainImg img').attr('src',VariantsObject.getSelected().imgSrcLarge); 
				$('.mainImg a').attr('href',VariantsObject.getSelected().imgSrcOriginal); 
				$('.mainImg img').fadeIn('normal');
			});
			return false; 
		});
	}
	
	$('.mainImg img').attr('src',VariantsObject.select(currentValue + 1).imgSrcLarge);
	$('.mainImg a').attr('href',VariantsObject.getSelected().imgSrcOriginal); 
	
	variantGo('next');
}

function variantGo(direction) {
	if (VariantsObject == null)
		return;
		
	var el = VariantsObject.item();
	
	if (direction == "next") {
		VariantsObject.next();
	}
	else {
		VariantsObject.prev();
	}
	
	for (i = 1; i <= IMGS_ROW_LENGTH && i <= VariantsObject.length(); i++) {
		$('#variants .ov' + i ).attr('src',''); // this line for IE6
		$('#variants .ov' + i).attr('src',VariantsObject.items[VariantsObject.current + i - 1].imgSrcSmall);
		$('#variants .ov' + i).attr('idInList', VariantsObject.current + i - 1);
	}
	
	if (VariantsObject.current == 0) {
		$('#vprev').parent().css('visibility','hidden');
	}
	else {
		$('#vprev').parent().css('visibility','visible');
	}
	
	if (VariantsObject.current == VariantsObject.length() - IMGS_ROW_LENGTH) {
		$('#vnext').parent().css('visibility','hidden');
	}
	else {
		$('#vnext').parent().css('visibility','visible');
	}
	
	if (VariantsObject.length() <= IMGS_ROW_LENGTH) {
		$('#vnext').parent().css('visibility','hidden');
		$('#vprev').parent().css('visibility','hidden');
	}
}
