(function($){
	$.fn.megaswitcher = function(settings) {
		return this.each(function() {        
			var $i = $(this),
				current,
				childs = $i.find('.selection li').length,
				paused = false;
			
			// some kind of mouse check, needs tweaking!!!
			/*$i.bind({
				mouseover: function(){ paused = true; },
				mouseout: function(){ paused = false; $i.find('.selection li.active').trigger('dblclick'); }
			});*/
			
			$i.find('.selection').delegate('li.active', 'dblclick', function(e){
				e.preventDefault();
				current = $i.find('.selection li').index(this);
				/*if(!paused){*/
					var _delay = $(this).attr('name') > 0 ? (parseInt($(this).attr('name')) * 1000) : 6000;
					$(this).delay(_delay).show(0, function(){
						if((current + 1) < childs){ // if not last
							$(this).removeClass('active').next().addClass('active').show(0, function(){
								$i.find('.image img').addClass('tempp');
								$(this).find('img').clone().hide().appendTo($i.find('.image')).fadeIn(400, function(){
									$i.find('.image img.tempp').remove();
								});
								var _text = $(this).find('img').attr('alt');
								var _link = $(this).find('a.linked').attr('href');
								if(_link != undefined){
									_text = '<a href="' + _link + '">' + _text + '</a>';
									$i.find('.image-link-overlay').attr('href', _link);
								}
								$i.find('.overlay .workarea .title').html(_text);
							}).trigger('dblclick');
						}else{
							$(this).removeClass('active');
							$i.find('.selection li:first').addClass('active').show(0, function(){
								$i.find('.image img').addClass('tempp');
								$(this).find('img').clone().hide().appendTo($i.find('.image')).fadeIn(400, function(){
									$i.find('.image img.tempp').remove();
								});
								var _text = $(this).find('img').attr('alt');
								var _link = $(this).find('a.linked').attr('href');
								if(_link != undefined){
									_text = '<a href="' + _link + '">' + _text + '</a>';
									$i.find('.image-link-overlay').attr('href', _link);
								}
								$i.find('.overlay .workarea .title').html(_text);
							}).trigger('dblclick');
						}
					});
				/*}*/
			});
			
			$i.find('.selection li.active').trigger('dblclick'); // trigger first
		});		
	};
})(jQuery);

/* BANNERS */
var expressInstall = "/js/swfobject/expressInstall.swf";
var pparams = {wmode: 'transparent', bgcolor: '#000000'};
var pioneervars = {clickTAG: "http://www.pioneer.lv/"};
swfobject.embedSWF("/uploads/sponsors/pioneer.swf", "pioneer-banner", "210", "210", "9", expressInstall, pioneervars, pparams);
var idejudruka = {clickTAG: "http://www.idejudruka.lv/"};
var iparams = {wmode: 'transparent'};
swfobject.embedSWF("/uploads/sponsors/idejudruka.swf", "idejudruka-banner", "210", "210", "9", expressInstall, idejudruka, iparams);
var eparams = {wmode: 'transparent'};
swfobject.embedSWF("/uploads/sponsors/ehr.swf", "ehr-banner", "210", "210", "9", expressInstall, null, eparams);
/* END OF BANNERS */

$(function(){
	if(isIE6 == true){
		$('img[src$="png"], #page, #logo-back, #logo, #expanding-twitter-bottom, .ictl, .ic, .open, .close, .overlay, .selection a, .content, .block, .bottom div, #sidebar, .sideBlock .title').addClass('ie6png'); // all pngs needs ie6 class. PNG fix.
	}
	
	$('.switcher').switcher({ 'firstClass' : 'first' });
	$('#navigation li.main').has('.submenu').hover(function(){
		$(this).children('a').addClass('hover hasSub');
		$(this).find('.hoverOnly').show();
	}, function(){
		$(this).find('.hover').removeClass('hover hasSub');
		$(this).find('.hoverOnly').hide();
	});
	var selection = $('#flashnews .selection li'), selectionTitle = $('#flashnews .title');
	selection.live({
		mouseover: function(){
			if(!$(this).hasClass('active')){
				$('#flashnews .selection li.active').removeClass('active');
				$(this).addClass('active');
				$('#flashnews .image img').addClass('tempp');
				$(this).children('img').clone().hide().addClass('temp').appendTo($('#flashnews .image')).fadeIn(400, function(){
					$('#flashnews .image img.tempp').remove();					
				});
				var _text = $(this).children('img').attr('alt');
				var _link = $(this).children('a.linked').attr('href');
				if(_link != undefined){
					_text = '<a href="' + _link + '">' + _text + '</a>';
					$('#flashnews .image-link-overlay').attr('href', _link)
				}
				selectionTitle.html(_text);
			}
		}
	});
	$('#flashnews').megaswitcher();
});
