(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(!$.support.opacity)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(!$.support.opacity)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeTo = function(speed,to,callback) {
		return this.animate({opacity: to}, speed, function() {
			if (to == 1 && jQuery.browser.msie)
				this.style.removeAttribute('filter');
			if (jQuery.isFunction(callback))
				callback();
		});
	};
})(jQuery);

function initMainMenu() {
	var timeout    = 100;
	var closetimer = 0;
	var ddmenuitem = 0;
	var ddmenuitem_pos_left_default = 0;
	
	function menu_open() {
		menu_canceltimer();
		menu_close();
		ddmenuitem = $(this).find('ul').css('display', 'block');
		if (ddmenuitem.length > 0) {
			var ddmenuitem_offset = ddmenuitem.offset();
			var ddmenuitem_pos = ddmenuitem.position();
			ddmenuitem_pos_left_default = ddmenuitem_pos.left;
			var ddmenuitem_width = ddmenuitem.outerWidth(true);
			if (ddmenuitem_offset.left + ddmenuitem_width > $('body').width()) {
				ddmenuitem.css('left', ddmenuitem_pos.left - (ddmenuitem_offset.left + ddmenuitem_width - $('body').width()));
			}
		}
	}
	
	function menu_close() {
		if (ddmenuitem) {
			ddmenuitem.css('display', 'none');
			ddmenuitem.css('left', ddmenuitem_pos_left_default);
		}
	}
	
	function menu_timer() {
		closetimer = window.setTimeout(menu_close, timeout);
	}
	
	function menu_canceltimer() {
		if(closetimer) {
			window.clearTimeout(closetimer);
			closetimer = null;
		}
	}
	
	$(document).ready(function() {
		$('#mainmenu > li').bind('mouseover', menu_open)
		$('#mainmenu > li').bind('mouseout',  menu_timer)
	});

	document.onclick = menu_close;
};

function initTabs(activeTab) {
	var showTabId = activeTab;
	initReports(showTabId);
	$('.tabulated').each(function() {
		$(this).find('.tabsSeparator').slice(1).hide();
		$(this).find('.content').slice(1).hide();
		$(this).find('.tab:first').addClass('active');
		$(this).find('.tab').each(function(){
			$(this).click(function() {
				var parent = $(this).parent().parent();
				showTabId = '#'+$(this).children('a').attr('id')+'View';
				$('.tab').each(function(){
					if ($(parent).find('.tab').hasClass('active')) {
						$(parent).find('.tab').removeClass('active');
					}
				});
				$(parent).find('.tabsSeparator').css('display', 'none');
				$(this).next().css('display', 'block');
				$(this).addClass('active');
				$(parent).find('.content').customFadeOut(500);
				setTimeout(function() {
					$(showTabId).customFadeIn(500);
					if(showTabId == '#photoreportsTabView' || showTabId == '#videoreportsTabView') {
						initReports(showTabId);
					};
				}, 500);
			})
		});
	});
}

function initReports(reportsID) {
	var curIndex = 0;
	var prevIndex = 0;;
	var current = '';
	var first = $('.mask', reportsID).eq(0);
	first.attr('first', 'first').hide();
	$('.currentReport', reportsID).eq(0).show();
	$('.reportsListItem', reportsID).bind({
		mouseover: function() {
			var index = $(this).index();
			var show = $('.currentReport', reportsID).eq(index);
			curIndex = index;
			current = $('.mask', reportsID).eq(curIndex);
			current.customFadeOut();
			if(prevIndex > -1 && prevIndex !== curIndex) {
				prev = $('.mask', reportsID).eq(prevIndex);
				prev.customFadeIn();
			}
			if(!current.attr('first') && first !== '') {
				first.removeAttr('first').customFadeIn();
				first = '';
			}
			if($('.currentReport:visible', reportsID).index() !== index) {
				$('.currentReport:visible', reportsID).hide();
			}
			if(show.is(':hidden')) {
				show.customFadeIn();
			}
		},
		mouseleave: function() {
			var index = $(this).index();
			prevIndex = index;
		}
	});
	$('.currentReport', reportsID).click(function() {
		if ($(this).find('a').length > 0 && reportsID !== 'videoreportsTabView') {
			document.location = $(this).find('a').attr('href');
		}
	});
}

function initNews() {
	var current = '';
	var first = $('.newsMask').eq(0);
	first.attr('first', 'first').hide();
	$('.newsListItem').bind({
		mouseover: function() {
			current = $('.newsMask', this)
			current.customFadeOut();
			if(!current.attr('first') && first !== '') {
				first.removeAttr('first').customFadeIn();
				first = '';
			}
		},
		mouseleave: function() {
			current.customFadeIn();
		},
		click: function(e) {
			document.location = $(this).find('a').attr('href');
		}
	});
}

function add2fav(x)
{
	if (document.all  && !window.opera) {
		 if (typeof window.external == "object") {
			window.external.AddFavorite (document.location, document.title);
			return true;
		  }
		  else return false;

	}
	else{
		x.href=document.location;
		x.title=document.title;
		x.rel = "sidebar";
		return true;
	}
}

function setHome(objSrc)
{ 
    var homepage = "http://pfkloko.ru";    
    if (objSrc.style.behavior)    
    {
        // MSIE
        objSrc.setHomePage(homepage);    
    }
    else if (netscape
                && netscape.security 
                && netscape.security.PrivilegeManager 
                && navigator.preference)    
    {
        // Mozilla/FireFox
        netscape.security.PrivilegeManager.enablePrivilege("UniversalPreferencesWrite");        
        navigator.preference("browser.startup.homepage", homepage);    
    }
} 

$(function() {
	initMainMenu();
	initTabs('#photoreportsTabView');
	initNews();
})
