function ToggleBlock(tel, fid) {
	con = 'LikeUrl';
	cof = 'LikeUrlOff';
	if($(tel).hasClass(con)){
		$('#' + fid).show();
		$(tel).removeClass(con);
		$(tel).addClass(cof);
	} else {
		$('#' + fid).hide();
		$(tel).removeClass(cof);
		$(tel).addClass(con);
	}
	
	$("#MailFormOkText").css('display', 'none');
	$("#MailFormWaitText").css('display', 'none');
	$("#MailFormErrorText").css('display', 'none');
}

function trim(str) {
	var value = str || '';
	while(value.charAt(0)==' ' || value.charCodeAt(0)==10 || value.charCodeAt(0)==9) {
		value = value.substr(1);
	}
	while(value.charAt(value.length - 1)==' ' || value.charCodeAt(value.length - 1)==10 || value.charCodeAt(value.length - 1)==9) {
		value = value.substr(0,value.length - 1);
	}
	return value;
}

function checkUrl(url) {
	var ind = url.indexOf('/',7);
	var thisurl = url.substr(ind+1);
	if(thisurl=='')
		$('#bone').attr('class','main');
	else
		$('#bone').attr('class','inner');
}

function trim_main_link() {
	var objgroup = $('.main_link');
	var i = 0;
	while(objgroup.get(i) || null != null) {
		var obj = objgroup.get(i);
		var id = obj.id;
		obj = $('#' + id); // Переопределяем объект, иначе не работает
		var html = trim(obj.html());
		obj.html(html);
		i++;
	}
}

var tm;
function resize_height() {
	var objgroup = $('div.BgRamka-big input:hidden');
	var i = 0;
	var url = '';
	var inpid = -1;
	while(objgroup.get(i) || null != null && inpid==-1) {
		var obj = objgroup.get(i);
		inpid = obj.id;
		obj = $('#' + inpid); // Переопределяем объект, иначе не работает
		url = obj.val();
		i++;
	}
	if(inpid==-1)
		return false;
	var divid = 'Div' + inpid.substr(3);
	var divobj = $('#' + divid);
	
	var imgid = 'Img' + inpid.substr(3);
	var imgobj = $('#' + imgid);
	
	var img = new Image();
	img.src = '/i/goods/' + url;
	
	var h = img.height;
	var w = img.width;
	
	if(h==0 || w==0) {
		tm = setTimeout('resize_height()', 100);
		return false;
	}
	
	var divobj = $('#' + divid);
	var imgobj = $('#' + imgid);
	var hgt = 0;
	imgobj.attr('src', '/i/goods/' + url);
	if(w>281) {
		hgt = Math.round( h*281/w );
		divobj.css( 'width', 281 );
		divobj.css( 'height', hgt );
		imgobj.css( 'width', 281 );
		imgobj.css( 'height', hgt );
	}else {
		hgt = h;
		divobj.css( 'width', w );
		divobj.css( 'height', hgt );
		imgobj.css( 'width', w );
		imgobj.css( 'height', hgt );
	}
}

$(window).resize(function() {
	bone_for_ie();
	$('#pictues').css('left', $('#bone').offset().left + 210 + 'px');
	$('#pictues').css('top', $('#bone').offset().top + 385 + 'px');
});

function start_animation() {
	for(var i=1; i<8; i++) {
		if((i != prev_count_animation) && (i != count_animation))
			$('#animation' + i).attr('style',
							'opacity: 1.0; ' +
							'-moz-opacity: 1.0; ' +
							'-khtml-opacity: 1.0; ' +
							'filter:progid:DXImageTransform.Microsoft.Alpha(opacity=100); ' +
							'position: relative; display: none; top: 0px; left: 0px'
		);
	}
	
	var incr = Math.round( new Number($('#animation' + count_animation).css('opacity')) * 100 );
	if(incr<max_opacity)
		incr = incr + 10;
	else
		incr = max_opacity;
	var decr = Math.round( new Number($('#animation' + prev_count_animation).css('opacity')) * 100 );
	if(decr>0)
		decr = decr - 10;
	else
		decr = 0;
	
	$('#animation' + count_animation).attr('style',
					'opacity: ' + incr/100 + '; ' +
					'-moz-opacity: ' + incr/100 + '; ' +
					'-khtml-opacity: ' + incr/100 + '; ' +
					'filter:progid:DXImageTransform.Microsoft.Alpha(opacity=' + incr + '); ' +
					'position: relative; display: inline; top: 0px; left: ' + (count_animation > prev_count_animation ? '-65px' : '0px')
	)
	
	$('#animation' + prev_count_animation).attr('style',
					'opacity: ' + decr/100 + '; ' +
					'-moz-opacity: ' + decr/100 + '; ' +
					'-khtml-opacity: ' + decr/100 + '; ' +
					'filter:progid:DXImageTransform.Microsoft.Alpha(opacity=' + decr + ');' +
					'position: relative; display: inline; top: 0px; left: ' + (count_animation > prev_count_animation ? '0px' : '-65px')
	)
	
	if(incr==max_opacity) {
		if(count_animation == 7)
			count_animation = 1;
		else
			count_animation++;
		
		if(count_animation == 1)
			prev_count_animation = 7;
		else
			prev_count_animation = count_animation - 1;
	}
	full_count++;
	switch (full_count) {
		case(20) : max_opacity = 20; break;
		case(40) : max_opacity = 30; break;
		case(60) : max_opacity = 40; break;
		case(80) : max_opacity = 50; break;
		case(100) : max_opacity = 60; break;
		case(120) : max_opacity = 70; break;
		case(140) : max_opacity = 80; break;
		case(160) : max_opacity = 90; break;
		case(180) : max_opacity = 100; break;
		case(200) : max_opacity = 90; break;
		case(220) : max_opacity = 80; break;
		case(240) : max_opacity = 70; break;
		case(260) : max_opacity = 60; break;
		case(280) : max_opacity = 50; break;
		case(300) : max_opacity = 40; break;
		case(320) : max_opacity = 30; break;
		case(340) : max_opacity = 20; break;
		case(360) : max_opacity = 10; break;
		case(380) : max_opacity = 0; break;
	}
	
	if(max_opacity==0)
		setDefault();
}

function setDefault() {
	for(var i=0; i<8; i++) {
	$('#animation' + i).attr('style',
					'opacity: 0.0; ' +
					'-moz-opacity: 0.0; ' +
					'-khtml-opacity: 0.0; ' +
					'filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0); ' +
					'position: relative; display: none; top: 0px; left: 0px'
		);
	}
	clearInterval(timer_animation);
	max_opacity = 10;
	timer_animation = null;
	count_animation = 1;
	prev_count_animation = 7;
	full_count = 0;
	$('.drova').css('cursor','pointer');
}

function func_obj_selector() {
	var html = new String($('#obj_selector').html());
	var html_lower = html.toLowerCase();
	var start_index = html_lower.indexOf('start</div>') + (new String('start</div>')).length;
	var end_index = html_lower.indexOf('end</div>') + 9;
	var new_html = html.substring(start_index, end_index);
	$('#obj_selector').html(new_html);
}

function bone_for_ie() {
	var brauz = (new String(window.navigator.appName)).toLowerCase();
	if(brauz.indexOf('explorer')!=-1) {
		ie_timer = setTimeout('timer_for_ie();', 100);
	}
}

function timer_for_ie() {
	if($('#bone').width()>1180)
		$('#bone').width(1180);
	if($('#bone').width()<1000)
		$('#bone').width(1000);
	
	$('.BgRamka').css('margin', '0px');
	$('.BgRamka').css('padding', '0px');
	$('.BgRamka').css('width', '150px');
	$('.BgRamka').css('height', '150px');
	
	$('.ramka_img').css('z-index', 60);
	
	$('#pictues').css('left', $('#bone').offset().left + 210 + 'px');
	$('#pictues').css('top', $('#bone').offset().top + 385 + 'px');
}

var max_opacity = 10;
var timer_animation = null;
var count_animation = 1;
var prev_count_animation = 7;
var full_count = 0;
var ie_timer;

function url_analysis() {
	var wind = new String(window.location);
	if(wind.indexOf('error_404')!=-1) {
		$('.menu').css('display', 'none');
		$('.phone').css('display', 'none');
		$('.map').css('display', 'none');
		$('.copyright').css('display', 'none');
		$('.developers').css('display', 'none');
		return true;
	}
	var ind = wind.indexOf('/', 8);
	wind = wind.substr(ind);
	$.post("/SEARCH_URL.php", {wind: wind, random: Math.random()},
		function(data) {
			sdata = data.toLowerCase();
			if(sdata == 'ok') {
				return true;
			}else {
				window.location = '/error_404/';
				return false;
			}
		}
	);
}

$(document).ready(function(){
	url_analysis();
	bone_for_ie();
	$('#pictues').css('left', $('#bone').offset().left + 210 + 'px');
	$('#pictues').css('top', $('#bone').offset().top + 385 + 'px');
	
	func_obj_selector();
	
/// Onload functions
	// Костыль для IE
	/*
	var brauz = (new String(window.navigator.appName)).toLowerCase();
	if(brauz.indexOf('explorer')!=-1)
		$('#pictues').css('left', '207px');
	else
		$('#pictues').css('left', '249px');
	*/
	// **************
	$('#sender_mail').click(function() {
		$('#MailFormWaitText').show();
		$('#MailForm').hide();
		$.post('/ajax.php', $('#MailForm').serialize(), function(text) {
			$('#MailFormWaitText').hide();
			if(text == 'OK'){
				$('#MailFormOkText').show();
			} else {
				$('#MailFormErrorText').show();
			}
			$('#mail_span').attr('class', 'LikeUrl');
	    }, 'text');
	})
	$('.drova').click(function() {
		if(timer_animation == null)
			timer_animation = setInterval('start_animation();', 100);
		$(this).css('cursor','text');
	})
	$('.Gallery a').lightBox();
	$('.Thumb a').lightBox();
	$('.Thumb-big a').lightBox();

	var appName = (new String(navigator.appName)).toLowerCase();
	if(appName=='microsoft internet explorer') {
		$('.light-box').css('background', 'url(/i/backgrounds/light.gif) no-repeat 50% 0');
		$('#logo_id').attr('src', '/i/logo.gif');
	}
	var thishref = new String(window.location.href);
	checkUrl(thishref);
	trim_main_link();
	resize_height();
});
