/* cookies funcs */
var Cookies = {
	set: function(cookieName, cookieContent, cookieExpireTime){
		if (cookieExpireTime>0){
			var expDate=new Date();
			expDate.setTime(expDate.getTime()+cookieExpireTime*1000*60*60);
			var expires=expDate.toGMTString();
			document.cookie=cookieName+"="+escape(cookieContent)+"; path="+escape('/')+"; expires="+expires
		}else{
			document.cookie=cookieName+"="+escape(cookieContent)+"; path="+escape('/')+"";
		}
	},
	get: function(cookieName){
		var ourCookie=document.cookie;
		if (!ourCookie || ourCookie=="")return "";
		ourCookie=ourCookie.split(";");
		var i=0;
		var Cookie;
		while (i<ourCookie.length){
			Cookie=ourCookie[i].split("=")[0];
			if (Cookie.charAt(0)==" ") Cookie=Cookie.substring(1);
			if (Cookie==cookieName) return unescape(ourCookie[i].split("=")[1]);
			i++;
		}
		return "";
	},
	erase: function(cookieName){
		var cookie = Cookies.get(cookieName) || true;
		Cookies.set(cookieName, '', -1);
		return cookie;
	},
	accept: function(){
		if (typeof navigator.cookieEnabled == 'boolean') return navigator.cookieEnabled;
		Cookies.set('_test', '1');
		return (Cookies.erase('_test') = '1');
	}
}

/*
 * Randomly interchange elements
 * if param 'b' is set to 'true', it will shuffle sub- and multisubarrays
 * in their own right, the default value is 'false'
 */
Array.prototype.shuffle = function(b){
	var i = this.length, j, t;
	while(i){
		j = Math.floor((i--) * Math.random());
		t = b && typeof this[i].shuffle !== 'undefined' ? this[i].shuffle() : this[i];
		this[i] = this[j];
		this[j] = t;
	}
	return this;
};

/*
 * trim funcs
 */
Object.extend(String.prototype, {
	LTrim: function(){
		return this.replace(/\s*((\S+\s*)*)/, "$1");
	},
	RTrim: function(){
		return this.replace(/((\s*\S+)*)\s*/, "$1");
	},
	trim: function(){
		return this.LTrim(this.RTrim());
	}
});

/*
 * we need win1251, not that utf stuff
 */
window.encodeURIComponent = function(str){
	var trans = [];
	for (var i = 0x410; i <= 0x44F; i++) trans[i] = i - 0x350; // À-ßà-ÿ
	trans[0x401] = 0xA8; // ¨
	trans[0x451] = 0xB8; // ¸
	var ret = [];
	for (var i = 0; i < str.length; i++){
		var n = str.charCodeAt(i);
		if (typeof trans[n] != 'undefined') n = trans[n];
		if (n <= 0xFF) ret.push(n);
	}
	return escape(String.fromCharCode.apply(null, ret)).replace(/\+/g, '%2B'); // +
}
window.decodeURIComponent = function(str){
	return unescape(str);
}

Element.addMethods({
	swapWith: function(element, other) {
		element = $(element);
		other = $(other);
		if (element !== other) {
			var stub = document.createElement('div');
			other = Element.replace(other, stub);
			element = Element.replace(element, other);
			stub = Element.replace(stub, element);
			stub = null; // prevent possible leaks
		}
		return element;
	}
}); 

function cut_text(text, len, fin) {
	if ( text.length <= len ) {
		return text;
	}

	text = text.substr(0, len);
	text = text.replace(/\s[\S]*?$/gmi, '');

	return text + fin;
}

var newsImages = new Array();

function changeNewsImage(value, id) {
	var images = newsImages[id];
	var number = parseInt($('curr_img_' + id).innerHTML);
		
	if ( ( value == 1 ) && ( number == images.length ) ) {
		number = 0;
	}
	if ( ( value == -1 ) && ( number == 1 ) ) {
		number = images.length + 1;
	}
	
	number = number + value;
	
	$('curr_img_' + id).innerHTML = number;
	$('img_' + id).src = '/images/' + images[number-1][1];
	$('img_' + id).alt = images[number-1][2];
	$('img_descr_' + id).innerHTML = images[number-1][0];
	
	var el = $('img_name_' + id);
	if ( el != null ) {
		el.innerHTML = images[number-1][2];
	}
}

var galleryFirstElementPos = new Array();
var gallery = new Array();

function scrollGallery(cont_id, dir, limit) {
	var gallery = $$('#photos_' + cont_id + ' .rotator ul li');
	
	var pos = galleryFirstElementPos[cont_id];
	
	if ( dir == 'right' ) {
		if ( pos + limit == gallery.length  ) {
			return;
		}
		
		gallery[pos].addClassName('hidden');
		gallery[pos+limit].removeClassName('hidden');
		
		if ( pos == 0 ) {
			$$('#photos_' + cont_id + ' .arrow.left-arrow')[0].removeClassName('s-no-scrollable');
		}
		
		if ( pos + limit + 1 == gallery.length ) {
			$$('#photos_' + cont_id + ' .arrow.right-arrow')[0].addClassName('s-no-scrollable');
		}
		
		galleryFirstElementPos[cont_id]++;
	}
	else {
		if ( pos == 0 ) {
			return;
		}
		
		gallery[pos-1].removeClassName('hidden');
		gallery[pos+limit-1].addClassName('hidden');
		
		if ( pos == 1 ) {
			$$('#photos_' + cont_id + ' .arrow.left-arrow')[0].addClassName('s-no-scrollable');
		}
		
		if ( pos + limit - 1 < gallery.length ) {
			$$('#photos_' + cont_id + ' .arrow.right-arrow')[0].removeClassName('s-no-scrollable');
		}
		
		galleryFirstElementPos[cont_id]--;
	}
}

function scrollRingedGallery(cont_id, dir, limit) {
	var gallery = $$('#photos_' + cont_id + ' .rotator ul li');
	
	var pos = galleryFirstElementPos[cont_id];
	
	if ( dir == 'right' ) {
		if ( pos + limit == gallery.length ) {
			gallery[ galleryFirstElementPos[cont_id] ].addClassName('hidden');
			
			gallery[ gallery.length - 1 ].insert( { after: gallery[0] } );
			
			gallery[0].removeClassName('hidden');
		}
		else {
			gallery[pos].addClassName('hidden');
			gallery[pos+limit].removeClassName('hidden');
			
			galleryFirstElementPos[cont_id]++;
		}
	}
	else {
		if ( pos == 0 ) {
			gallery[ limit - 1 ].addClassName('hidden');
			
			gallery[0].insert( { before: gallery[ gallery.length - 1 ] } );
			
			gallery[ gallery.length - 1 ].removeClassName('hidden');
		}
		else {
			gallery[pos-1].removeClassName('hidden');
			gallery[pos+limit-1].addClassName('hidden');
			
			galleryFirstElementPos[cont_id]--;
		}
	}
}

var daysRotatorOffset = new Array();
var daysRotatorMaxOffset = new Array();

function scrollDaysRotator(cont_id, dir) {
	if ( dir == 'left' ) {
		if ( daysRotatorOffset[cont_id] == 0 ) {
			return;
		}
		daysRotatorOffset[cont_id] += 25;
	}
	else {
		if ( daysRotatorOffset[cont_id] + daysRotatorMaxOffset[cont_id] == 0 ) {
			return;
		}
		daysRotatorOffset[cont_id] -= 25;
	}
	
	$(cont_id).setStyle( { marginLeft:daysRotatorOffset[cont_id] + 'px' } );
}

var lettersRotatorOffset = new Array();
var lettersRotatorMaxOffset = new Array();

function scrollLettersRotator(cont_id, dir) {
	if ( dir == 'left' ) {
		if ( lettersRotatorOffset[cont_id] == 0 ) {
			return;
		}
		lettersRotatorOffset[cont_id] += 25;
	}
	else {
		if ( lettersRotatorOffset[cont_id] + lettersRotatorMaxOffset[cont_id] == 0 ) {
			return;
		}
		lettersRotatorOffset[cont_id] -= 25;
	}
	
	$(cont_id).setStyle( { marginLeft:lettersRotatorOffset[cont_id] + 'px' } );
}

function updateByLettersLocator(id, category_id, letter) {
	new Ajax.Updater(id + '_locator', '/ajax/locator/' + id + '.html', {
		parameters: { category_id:category_id, letter:letter },
		method: 'get',
		evalScripts: true
	});
}

function updatePodiumsLocator(designer_id, year, month, day) {
	new Ajax.Updater('podiums_locator_' + designer_id, '/ajax/locator/podiums.html', {
		parameters: { designer_id:designer_id, year:year, month:month, day:day },
		method: 'get',
		evalScripts: true
	});
}

function updateNewsLocator(sect_id, year, month, day) {
	new Ajax.Updater('news_locator', '/ajax/locator/news.html', {
		parameters: { sect_id:sect_id, year:year, month:month, day:day },
		method: 'get',
		evalScripts: true
	});
}

function updatePostsLocator(sect_id, year, month, day) {
	new Ajax.Updater('posts_locator', '/ajax/locator/posts.html', {
		parameters: { sect_id:sect_id, year:year, month:month, day:day },
		method: 'get',
		evalScripts: true
	});
}

function updateGalleriesLocator(sect_id, number, year, month, day) {
	new Ajax.Updater('galleries_locator_' + number, '/ajax/locator/galleries.html', { 
		parameters: { sect_id:sect_id, number:number, year:year, month:month, day:day },
		method: 'get',
		evalScripts: true
	});
}

function updateThingsLocator(catalog_id, brand_id, p, doc_dtime) {
	new Ajax.Updater('things_locator', '/ajax/locator/things.html', {
		parameters: { catalog_id:catalog_id, brand_id:brand_id, p:p, doc_dtime:doc_dtime },
		method: 'get',
		evalScripts: true
	});
}

function updateTrendsLocator(sect_id, p) {
	new Ajax.Updater('trends_locator', '/ajax/locator/trends.html', {
		parameters: { sect_id:sect_id, p:p },
		method: 'get',
		evalScripts: true
	});
}

function submitOnEnter(element, event) {
	var keycode;
	
	if (window.event) keycode = window.event.keyCode;
	else if (event) keycode = event.which;
	else return true;

	if ( keycode == 13 ) {
		element.form.submit();
		return false;
	}
	else
		return true;
}

function postToFriendfeed(url, title) {
	var e = document.createElement('script');
	e.setAttribute('type','text/javascript');
	e.setAttribute('src','http://friendfeed.com/share/bookmarklet/javascript?url=' + url + '&title=' + title);
	document.body.appendChild(e);
}

function toggleBasketButton(size) {
	if ( size == '' ) {
		$('add-to-basket').removeClassName('active');
	}
	else {
		$('add-to-basket').addClassName('active');
	}
}

function addToBasket(id, size) {
	if ( size == '' ) {
		return;
	}
	var cookie = Cookies.get('basket');
	
	if ( cookie == '' ) {
		cookie = id + ',' + size;
	}
	else {
		cookie = cookie + ';' + id + ',' + size;
	}
	
	Cookies.set('basket', cookie, '+1d');
	
	$('order-process').addClassName('active');
	$('add-to-basket').removeClassName('active');
	$('thing-size').value = '';
	
	$$('#header .menu .basket').invoke('removeClassName', 'hidden');
	
	alert('Äîáàâëåíî â êîðçèíó');
}

function updateDeliveryCost(city_id) {
	new Ajax.Updater('delivery-cost', '/ajax/delivery_cost.html', {
		parameters: { city_id:city_id },
	});
}