jQuery(function(){
	var is_active=jQuery("#menu-submenu-on-press-page").find(".current-menu-item");
	if (is_active.html()==null)
	{jQuery("#menu-submenu-on-press-page li:first").addClass("current-menu-item");
	}
jQuery('<br/>').insertAfter('.vr_wrapper input');
jQuery("#sub_menu").find("a").each(function(){
		
		jQuery(this).addClass("btn_rounded");
	var content=jQuery(this).html();
	var newcontent='<span>'+content+'</span>';
	jQuery(this).html(newcontent);
	});

	jQuery("#slideshow").slideshow();
	
	jQuery("#footer [rel='mailing_list']:first").click(function(){
		 jQuery("#mailing_list").notification();
	});
	
	jQuery("[flash]").each(function(){
		var obj = jQuery(this);
		obj.flash({
			swf : obj.attr("flash"),
			height : obj.height(),
			width : obj.width()	
		});
	});
	
	jQuery("form [href='#submit']").click(function(e){
		e.preventDefault();
		jQuery(this).parents("form:first").submit();
	});
	
	jQuery("#content .magazines:first").magazines();
	
	jQuery(".video .list a").click(function(e){
		
		e.preventDefault();
		if( jQuery(this).is(".active") ){ return; }
		jQuery(".video .list .active").removeClass("active");
		jQuery(this).addClass("active");
		if (jQuery(this).hasClass("yt")){
		jQuery(".play").empty().attr("flash", jQuery(this).attr("href"));
		jQuery(".play").each(function(){
			var obj = jQuery(this);
			obj.flash({
				swf : obj.attr("flash"),
				height : obj.height(),
				width : obj.width()	
			});
		});
				} else {

        var path =jQuery(this).attr("href");
        // configuration options        
        var config = "{";
        config += "'clip': {'url' : '" + path + "',";
		 config += "'autoPlay': false }";
        config += "}";
 		jQuery(".play").each(function(){
			var obj = jQuery(this);
			obj.flash(            {
                swf: 'http://releases.flowplayer.org/swf/flowplayer-3.2.4.swf',
          flashvars:
                {
                    config: config 
                },
			height : obj.height(),
		   autoPlay: false,
				width : obj.width()	
            });
		});          
}
	});
	
	jQuery(".video .list a:first").trigger("click");

	jQuery("select").change(function(e){
		if( jQuery(this).attr("autopost") == "true"){ jQuery(this).parents("form:first").submit(); }
	});
	
});

jQuery.fn.magazines = function(){
	
	var main = jQuery(this);
	var items = main.find("a");
	
	items.click(function(e){
		e.preventDefault();
		var obj = jQuery(this);
		var rel = obj.attr("href");
		jQuery(".magazine[rel='"+rel+"']:first").notification();
		bind_funcs( obj );
		
	});
	
	function bind_funcs( clicked ){
		
		var preview = jQuery("#notification .preview");
		var big_img = preview.find(".big_image img");
		var thumb = preview.find(".thumbs");
		var thumbs = preview.find(".thumbs a");
		
		preview.append('<div class="prev">PREV</div><div class="next">NEXT</div>');
		
		var prev = preview.find(".prev");
		var next = preview.find(".next");
				
		thumbs.click(function(e){
			
			e.preventDefault();
			var obj = jQuery(this);
			var href = obj.attr("href");
			var src = obj.find("img").attr("src");
			var alt = obj.find("img").attr("alt");
			
			thumb.find(".active").removeClass("active").show();
			
			obj.addClass("active").hide();
			
			big_img.attr({
				src: href,
				alt: alt,
				rel: src
			});
				
		}).eq(0).trigger("click");
		
		prev.click(function(e){
			e.preventDefault();
			jQuery("#notification, .opaque").remove();
			var previous = clicked.prev("a");
			var last = main.find("a:last");
			
			if( previous.attr("href") ){ previous.trigger("click"); }
			else{ last.trigger("click");	}	
			
		});
		
		next.click(function(e){
			e.preventDefault();
			jQuery("#notification, .opaque").remove();
			var next_thumb = clicked.next("a");
			var first = main.find("a:first");
			
			if( next_thumb.attr("href") ){ next_thumb.trigger("click"); }
			else{ first.trigger("click");	}	
			
		});
		
	};
	
};


jQuery.fn.slideshow = function(){
	
	var main = jQuery(this);
	var images = main.find("img");
	var speed = main.attr("speed")*1000;
	var counter = -1;
	
	images.each(function(){
		var obj = jQuery(this);
		var src = obj.attr("src");
		obj.removeAttr("src").attr("src2", src);
	});
	
	function load_image(){
		counter++;
		if( counter == images.size()-1 ){
			counter = -1;
		};
		var img = images.eq(counter);
		var src = img.attr("src2");
		
		var tmp = new Image();
		tmp.onload = function(){
			img.attr("src", src);
			
			swap_slide();
		};
		tmp.src = src;
		
	};
	
	function swap_slide(){
		
		var img = images.eq(counter);
		var active = main.find(".active");
		if( active ){active.removeClass("active").fadeOut("normal");}
		img.addClass("active").fadeIn("normal",function(){
			
			setTimeout(load_image, speed);
			
		});
		
	};
	
	load_image();
	
};

jQuery.fn.notification = function (options){
	
	if(options == null){options ={};}
	var o ={};
	o.text = jQuery(this).html() || options['text'];
	o.fade = options['fade'] || 'false';
	o.rainbow = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
	o.t_layer = options['t_layer'] || 'true';
	o.t_lvl = options['t_lvl'] || '0.5';
	o.t_color = options['t_color'] || '#000';
	o.delay = options['delay'] || 3000;
	o.speed = options['speed'] || 200;
	o.clickTAG = options['clickTag'] || '.close';
	o.type = options['type'] || 'default';
	o.id = 'notification';
	o.flag = 'true';
	o.scroller = "true";
	if(options.t_color == "rainbow"){o.t_color = o.rainbow;}
	jQuery("#notification").remove();
	
	if( !o.text ){
		
		o.fade = "true";
		o.text = "<div class='empty'>No content to show</div>"
		
	};
	
	o.html = '<div id="'+o.id+'">';
	o.html+= '<a class="close">&nbsp;</a>';
	o.html+= '<div class="middle">'+o.text+'</div>';
	o.html+= '<div class="left_shaddow">&nbsp;</div>';
	o.html+= '<div class="right_shaddow">&nbsp;</div>';
	o.html+= '<div class="top_shaddow">&nbsp;</div>';
	o.html+= '<div class="bottom_shaddow">&nbsp;</div>';
	o.html+= '<div class="nw_shaddow">&nbsp;</div>';
	o.html+= '<div class="ne_shaddow">&nbsp;</div>';
	o.html+= '<div class="sw_shaddow">&nbsp;</div>';
	o.html+= '<div class="se_shaddow">&nbsp;</div>';
	o.html+= '</div>';

	jQuery("body").append(o.html);  
	if(jQuery("#notification").outerHeight() > jQuery(window).height()-50){jQuery("#notification").css({position:"absolute"}); o.scroller="false";}
	if(jQuery("#notification").css("position") == "absolute" && o.scroller == "true"){jQuery(window).scroll(positions);}
	
	positions(false);

	jQuery(window).resize(function(){
		positions( false );	
	});
	
	function positions( switcher ){
		if(o.flag == "true"){
			var pos = {};
			pos.left = (jQuery(window).width() - jQuery("#"+o.id).outerWidth())/2;
			pos.top = jQuery(window).scrollTop() + ((jQuery(window).height() - jQuery("#"+o.id).outerHeight())/2);
			if(pos.left < 1){ pos.left = 0;}
			if(pos.top <0 || jQuery("#notification").outerHeight() > jQuery(window).height()){ pos.top = jQuery(window).scrollTop(); }
			if(jQuery("#notification").css("position") == "fixed"){ pos.top = ((jQuery(window).height() - jQuery("#"+o.id).outerHeight())/2); }
			
			if(switcher == false){
				jQuery("#"+o.id).css({'left':pos.left+'px', 'top':pos.top+'px'});
				jQuery("#opaque_notification").css({'left':0+'px', 'top':jQuery(window).scrollTop()+'px'});
			}
			else{
				jQuery("#"+o.id).animate({'left':pos.left+'px', 'top':pos.top+'px'},{duration:300, queue:false});
				jQuery("#opaque_notification").animate({'left':0+'px', 'top':jQuery(window).scrollTop()+'px'},{duration:300, queue:false});
				
			}
		}
		jQuery(".opaque").css({
		
			"width" : jQuery(window).width(),
			"height" : jQuery(window).height()
		
		});
		
	}

	if(o.t_layer == "true"){
      jQuery("body").append('<div class="opaque" id="opaque_'+o.id+'">&nbsp;</div>');
      jQuery("#opaque_"+o.id).css({'top':'0px', 'z-index':'900', 'left':'0px','background':o.t_color, 'height':jQuery(window).height()+'px', 'width':jQuery(window).width()+'px'}).fadeTo(0, o.t_lvl);
	}
	jQuery(window).resize(positions);
	var timeout = '';
	if(o.fade == "true"){ timeout = setTimeout(fade_out, o.delay)}

	jQuery(o.clickTAG).click(function(){fade_out(0); clearTimeout(timeout);});
	jQuery(o.clickTAG).css({'cursor':'pointer'});

   function fade_out( delay ){
	
      jQuery("#"+o.id).fadeTo(delay, "1.0").fadeOut(o.speed, function(){jQuery(this).remove();});
      if(o.t_layer == "true"){
         jQuery("#opaque_"+o.id).fadeTo(delay, o.t_lvl).fadeOut(o.speed, function(){jQuery(this).remove();});
      }
		o.flag = "false";
   }
	jQuery("form [href='#submit']").click(function(e){
		e.preventDefault();
		jQuery(this).parents("form:first").submit();
	});
}
