$.fn.delayedHover = function (over, out, delay) {
	var timerIn = false;
	var timerOut = false;

	if ( ! delay) {
		var delay = 500;
	}

	return this.hover(function(e) {
		clearTimeout(timerIn);
		clearTimeout(timerOut);
		var el = this;
		if ($.isFunction(over)) {
			timerIn = setTimeout(function(){ over.apply(el, new Array(e, e)) }, delay);
		}
	}, function(e){
		clearTimeout(timerIn);
		clearTimeout(timerOut);
		var el = this;
		if ($.isFunction(out)) {
			timerOut = setTimeout(function(){ out.apply(el, new Array(e, e)) }, delay);
		}
	})
}
function fixPNG(element) {
	/*
	 * browser checking in fixPNG is depricated
	 */
	//if (/MSIE (5\.5|6|7|8|9).+Win/.test(navigator.userAgent))	{
		var src;
		if ( typeof element != 'object') {
			element = this;
		}
		if (element.getAttribute('src'))	{
			//if (/\.png$/.test(element.getAttribute('src'))) {
				src = element.getAttribute('src');
				element.src = "/images/blank.gif";
			//}
		} else {
			element.runtimeStyle.backgroundImage = "";
			src = element.currentStyle.backgroundImage.match(/url\("(.+)"\)/i);
			if (src) {
				src = src[1];
				element.runtimeStyle.backgroundImage = "none";
			} else {
			}
		}
		if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
	//}
}
function ie6fix() {
	$('.fixpng6').each(fixPNG);
	$('#main table.text tr:first-child').addClass('first-child');
	$('#main table.text tr:last-child').addClass('last-child');
	$('#main table.text tbody tr').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	$('#main table.text tbody tr:first-child').hover(function() {
		$(this).addClass('first-child-hover');
	}, function() {
		$(this).removeClass('first-child-hover');
	});
	$('#main table.text tbody tr:last-child').hover(function() {
		$(this).addClass('last-child-hover');
	}, function() {
		$(this).removeClass('last-child-hover');
	});
}
function init_iefix() {
	var ie6 = $.browser.msie && /MSIE (6).+Win/.test(navigator.userAgent) && !/MSIE (7|8|9).+Win/.test(navigator.userAgent);
	var input = $('input');
	if ($.browser.msie) {
		$('.png').each(fixPNG);
		input.each(function() {
			$this = $(this);
			$this.addClass($this.attr('type'));
		});
		if(ie6) {
			ie6fix();
		}
	}
}
function init_service() {
	var popup = $('#popup');
	var p_item = popup.find('.item');
	var select = $('#select a');
	var timeOut;

	popup.css('opacity', 0);

	select.click(function() {
		var $this = $(this);
		var pop = popup.find('.item[rel='+$this.attr('rel')+']');
		if($this.hasClass('active')) {
			$this.removeClass('active');
			popup.animate({'opacity' : 0}, 300);
			setTimeout(function() {
				popup.css('display', 'none');
			}, 300);
		}
		else {
			select.removeClass('active'); p_item.hide();
			popup.css('display', 'block'); pop.show();
			popup.animate({'opacity' : 1}, 300);
			$this.addClass('active');
		}
	});

}
function install_popup() {
	var popup = $('#main-popup .popup');
	var popup_content = $('#popup-content');
	var close_button = $('img.popup-close');
	var popup_buttons = $('#main-popup .buttons .item');
	var popup_buttons_link = $('#main-popup .buttons .item a');
	var popup_buttons_ins = $('#main-popup .buttons .item ins');

	popup.css({opacity: 0});

	close_button.click(function() {
		popup.animate({opacity: 0}, 300);
		setTimeout(function() {
			popup.hide();
		}, 300);
		popup_buttons.removeClass('active');
	});

	popup_buttons.hover(function() {
		var $this = $(this).find('a');
		if(!$this.parent().hasClass('active')) {
			$this.css('z-index', 3);
			$(this).find('ins').css('z-index', 8);
			$this.find('object').css('display', 'block');
		}
	},
	function() {
		var $this = $(this).find('a');
		$this.css('z-index', 0);
		$(this).find('ins').css('z-index', 1);
		$this.find('object').css('display', 'none');
	});

	popup_buttons_ins.click(function() {
		var $this = $(this).parent();
		if($this.hasClass('active')) {
			popup.show();
			popup.animate({opacity: 0}, 300);
			setTimeout(function() {
				popup.hide();
			}, 300);
			$this.removeClass('active');
		}
		else {
			$this.find('a').css('z-index', 0);
			$(this).css('z-index', 1);
			$this.find('a').find('object').css('display', 'none');
			popup_buttons.removeClass('active');
			$this.addClass('active');
			popup.find('.content').html(popup_content.find('ins[rel='+$this.attr('rel')+']').html());
			popup.show();
			popup.animate({opacity: 1}, 300);
		}

	});

}
var init_main_player = function() {
	
	var flashvars = {
		file:'/netcat_files/Media/avs.flv',
		frontcolor: 'ffffff',
		lightcolor: 'cc9900',
		screencolor: '000000',
		controlbar: 'none',
		skin: '/js/jwplayer/beelden.zip',
		image: '/netcat_files/Media/preview_0001.jpg'
	};

	var params = {wmode:'opaque', allowFullScreen:'true', allowScriptAccess:'always'};

	var attributes = {id:"main-player", name:"main-player"};

	swfobject.embedSWF('/js/jwplayer/jwplayer.swf', "main-player", "100", "75", "9.0.0", false, flashvars, params, attributes);
}
var init_inner_player = function() {

	var elm = $('#inner-video');
	
	var flashvars = {
		file:elm.attr('href')
	};

	var params = {wmode:'opaque', allowFullScreen:'true', allowScriptAccess:'always'};

	var attributes = {id:"inner-video", name:"inner-video"};

	swfobject.embedSWF('/js/jwplayer/jwplayer.swf', "inner-video", parseInt(elm.css('width')), parseInt(elm.css('height')), "9.0.0", false, flashvars, params, attributes);
}
function email() {
	var obj = $('.ins-email');
	var email = '';
	email+='<a href=\'mailto:info@';
	email+='aviasystems.ru\'>info@';
	email+='aviasystems.ru</a>';
	obj.replaceWith(email);
}
function init_submenu() {
	var items = $('#submenu .item');
	var subs = items.find('.sub');
	var ssubs = subs.find('.ssub');
	
	subs.hide();
	ssubs.hide();	
	
	items.each(function() {
		if($(this).find('.active').hasClass('active')) {
			$(this).find('.sub').show();
		}
	});
	
	subs.each(function() {
		if($(this).find('.active').hasClass('active')) {
			$(this).find('.ssub').show();
		}
	});
	
	items.hover(function() {
		if(!$(this).find('.active').hasClass('active')) {
			$(this).find('.sub').show();
		}
	},
	function() {
		if(!$(this).find('.active').hasClass('active')) {
			$(this).find('.sub').hide();
		}
	});
	
	subs.hover(function() {
		if(!$(this).find('.active').hasClass('active')) {
			$(this).find('.ssub').show();
		}
	},
	function() {
		if(!$(this).find('.active').hasClass('active')) {
			$(this).find('.ssub').hide();
		}
	});
}
$(function() {
	var button = $('.button');
	var menu_item = $('#menu .item');
	var decs = $('input.decs');

	init_iefix();
	init_service();
	install_popup();
	init_main_player();
	init_inner_player();
	email();
	//init_submenu();

	decs.each(function() {
		if($(this).attr('value')) {
			$(this).addClass('active');
		}
	});

	decs.focus(function() {
		$(this).addClass('active');
	}).blur(function() {
		if(!$(this).attr('value')) {
			$(this).removeClass('active');
		}
	});

	button.mousedown(function() {
		$(this).addClass('active');
	}).mouseup(function() {
		$(this).removeClass('active');
	}).mouseout(function(){
		$(this).removeClass('active');
	});
	
	menu_item.hover(function() {
		$(this).find('.menu-item').addClass('active');
		$(this).find('.submenu').show();
	},
	function() {
		$(this).find('.menu-item').removeClass('active');
		$(this).find('.submenu').hide();
	});
	menu_item.find('.submenu .sub-item').each(function() {
		$(this).delayedHover(function() {
			var item = $(this);
			//item.find('a.hov').addClass('underlined');
			$('#'+item.find('a.hov').attr('rel')).removeClass('hidden');
		},
		function() {
			var item = $(this);
			//item.find('a.hov').removeClass('underlined');
			$('#'+item.find('a.hov').attr('rel')).addClass('hidden');
		}, 500);
	});

});
