var isIE = navigator.userAgent.toLowerCase().indexOf("msie") > -1;
var isIE56=(/MSIE (5\.5|6).+Win/.test(navigator.userAgent));
var isMoz = document.implementation && document.implementation.createDocument;
var isOpera = window.opera && opera.buildNumber;

//Работа с прозрачностью
function RollOpacity(id,value) {
	var elem=$(id), op=(isIE)?parseInt(value*100):value, shag=(isIE)?5:0.05;
	RollOpacity.clear(id);
	if(elem) {
	    var cur_op=(isIE)?elem.filters["DXImageTransform.Microsoft.alpha"]:elem.style, cof=parseFloat(cur_op.opacity);
		if(cof==op) {
			Events.fire('roll_end');
			return false;
		}
		if(cof<op) cur_op.opacity=cof+shag;
		if(cof>op) cur_op.opacity=cof-shag;

		RollOpacity.rop[id]=setTimeout("RollOpacity('"+id+"',"+value+")",50);
	}
}
RollOpacity.rop={};
RollOpacity.clear=function(id) {if(RollOpacity.rop[id]) clearTimeout(RollOpacity.rop[id]);}
RollOpacity.clear_all=function(block) {
	var bl=block.length;
	for(var i in RollOpacity.rop) {
		if(i.substring(0,bl)==block) RollOpacity.clear(i);
	}
}

function SetOpacity(elem,value) {
	var s=elem.style;
	if(isIE) {
        if(elem.filters["DXImageTransform.Microsoft.alpha"]) elem.filters["DXImageTransform.Microsoft.alpha"].opacity=value*100;
        else s.filter="progid:DXImageTransform.Microsoft.Alpha(opacity="+value*100+")";
     }
	else s.opacity=value;
}

//Обработка событий
var Events={
	list: {},
	fire: function(name, params) {
		if(this.list[name]) {
			for(var i in this.list[name]) this.list[name][i](params);
		}
	},
	add: function(name, func_name, callback) {
		if(!this.list[name]) this.list[name]={};
		this.list[name][func_name]=callback;
	},
	del: function(name, func_name) {
		if(func_name) delete this.list[name][func_name];
		else delete this.list[name];
	}
}

function $(id) {return document.getElementById(id);}

var cur_big_pic='';

var Slides={
	cur_big_pic: '',
	cur_num: 0,
	fisrt_num: 0,
	second_num: 1,
	type: null,
	
	init: function(type) {
		this.type=type;
		Shadowbox.init();
		this.cur_big_pic=pics[0]['big'];

		var cont=$('slides');
		
        /*
		if(type=='manual') {
			cont.onclick=function(){Shadowbox.open({player: 'img', content: Slides.cur_big_pic});}
			cont.style.cursor='pointer';
			//cont.title='Нажмите, чтобы увеличить изображение';
		}
		else {
			//$('slides_title').style.display='none';
			//$('slides_title_bg').style.display='none';
		}
        */
		
		for(var i=0; i<2; i++) {
			var img=document.createElement('IMG');
			img.id='slide_'+i;
			cont.appendChild(img);
		}
		
		$('slide_0').src=pics[this.cur_num]['path'];
		$('slide_0').style.zIndex=20;
		$('slide_1').style.zIndex=10;
		if(pics[this.cur_num+1]) $('slide_1').src=pics[this.cur_num+1]['path'];

		this.cur_num=1;
		
		Events.add('roll_end', 'change_pics', function() {
			$('slide_'+Slides.second_num).style.zIndex=20;
			$('slide_'+Slides.fisrt_num).style.zIndex=10;
			if(Slides.type=='auto') $('slide_'+Slides.fisrt_num).src=pics[Slides.cur_num]['path'];
		});
		
		if(type=='auto') this.play();
	},
	
	change: function(num) {
		if($('slide_0').style.zIndex==10) {
			this.fisrt_num=1;
			this.second_num=0;
		}
		else {
			this.fisrt_num=0;
			this.second_num=1;
		}
		this.cur_num=num+1;
		if(this.cur_num>pics.length-1) {
			this.cur_num=0;
		}
		
		SetOpacity($('slide_'+this.second_num), 1);
		if(Slides.type=='manual') $('slide_'+this.second_num).src=pics[num]['path'];
		this.cur_big_pic=pics[num]['big'];
		
		SetOpacity($('slide_'+this.fisrt_num), 1);
		RollOpacity('slide_'+this.fisrt_num, 0);

		return false;
	},
	
	simple_change: function(num) {
		$('slide_0').src=pics[num]['path'];
		this.cur_big_pic=pics[num]['big'];
		return false;
	},
		
	play: function() {
		if(pics.length>1) this.inter=setInterval("Slides.change(Slides.cur_num)", 3000);
	},
    
    clear: function() {
        if(this.inter) clearInterval(this.inter);
    }
	
}

//Обработка выпадающего меню для IE
function FixDropMenu(menu) {
	if(!isIE56) return true;
	
	var fix=function(e) {
		var ev = e || window.event, target = ev.target || ev.srcElement;
		while(target.id!=menu.id) {
			if(target.className.match(/menu_div/)) doClass(target,'hover_menu',((ev.type=='mouseover') ? 'add' : 'remove'));			
			target=target.parentNode;
		}
	}
	menu.onmouseover=fix;
	menu.onmouseout=fix;
}

//Добавление/удаление css класса
function doClass(el,name,type) {
	if(type=='add') {
		if(!el.className.match(/hover_menu/)) el.className+=' '+name;
	}
	else el.className=el.className.replace(/hover_menu/g, ' ');
}
