//Insert Flash Function
function insert_flash(file,div,width,height,transparent)
{
	var flashvars = {};
	if(transparent==1)
	{
		var params = {
		  wmode: "transparent",quality: "high"
		};
	}
	else
	{
	var params = {
	  quality: "high"
	  };
	}
		
	var attributes = {};
	swfobject.embedSWF(""+file+"", ""+div+"", ""+width+"", ""+height+"", "9.0.0","flash/expressInstall.swf", flashvars, params, attributes);
}

/**
 * Clears the form data.  Takes the following actions on the form's input fields:
 *  - input text fields will have their 'value' property set to the empty string
 *  - select elements will have their 'selectedIndex' property set to -1
 *  - checkbox and radio inputs will have their 'checked' property set to false
 *  - inputs of type submit, button, reset, and hidden will *not* be effected
 *  - button elements will *not* be effected
 */
$.fn.clearForm = function() {
    return this.each(function() {
        $('input,select,textarea', this).clearFields();
    });
};

/**
 * Clears the selected form elements.
 */
$.fn.clearFields = $.fn.clearInputs = function() {
    return this.each(function() {
        var t = this.type, tag = this.tagName.toLowerCase();
        if (t == 'text' || t == 'password' || tag == 'textarea')
            this.value = '';
        else if (t == 'checkbox' || t == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;
    });
};

/**
 * Resets the form data.  Causes all form elements to be reset to their original value.
 */
$.fn.resetForm = function() {
    return this.each(function() {
        // guard against an input with the name of 'reset'
        // note that IE reports the reset function as an 'object'
        if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType))
            this.reset();
    });
};
/*Tooltips*/
function initTooltips(){ $('a[title!=""], span[title!=""]').tipsy({gravity: $.fn.tipsy.autoNS}); $('div.ppy-nav a').tipsy({gravity: 's'}); $('span.bottom_projects[title!=""]').tipsy({gravity: 'e'}); /*, input[title]*/ }
//External Links
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "external")
		anchor.target = "_blank";
	}
}
$(window).load(function(){
						
	initTooltips();
	
	$('a.anchor').click(function(){
		$.scrollTo( this.hash, 800, { easing:'linear' });
		return false;
	});
	
	/*Scroll-Pane*/
	$('#paneagenda').jScrollPane({dragMinHeight:83, dragMaxHeight:83});
	
	/*Load External Links*/
	externalLinks();
	
	$('#sdt_menu > li').bind('mouseenter',function(){
		var $elem = $(this);
		$elem.find('img').css('z-index','999')
			 .stop(true)
			 .animate({
				'width':'158px',
				'height':'104px',
				'left':'0px'
			 },400,'easeOutBack')
			 .andSelf()
			 .find('.sdt_wrap')
			 .stop(true)
			 .animate({'top':'130px'},500,'easeOutBack')
			 .andSelf()
			 //.find('div').addClass("current")
			 .andSelf()
			 .find('.sdt_active')
			 .stop(true)
			 .animate({'height':'105px'},300,function(){
			var $sub_menu = $elem.find('.sdt_box');
			if($sub_menu.length){
				var left = '160px';
				if($elem.parent().children().length == $elem.index()+1)
					left = '-160px';
				$sub_menu.show().animate({'left':left},200);
			}
		});
	}).bind('mouseleave',function(){
		var $elem = $(this);
		var $sub_menu = $elem.find('.sdt_box');
		if($sub_menu.length)
			$sub_menu.hide().css('left','0px');

		$elem.find('.sdt_active')
			 .stop(true)
			 .animate({'height':'0px'},300)
			 .andSelf()
			 //.find('div').removeClass("current")
			 .andSelf().find('img').css('z-index','100')
			 .stop(true)
			 .animate({
				'width':'0px',
				'height':'0px',
				'left':'80px'},400)
			 .andSelf()
			 .find('.sdt_wrap')
			 .stop(true)
			 .animate({'top':'25px'},500);
	});
	
	$.ajax({
	  url:'/ajax/action/contacts',
	  data:{},
	  dataType:"html",
	  success:function(data){ $("div.right-contacts").html( data );}
	});
	$("div.right-contacts").addClass("loader");
	
	$('div.right-contacts').
		ajaxStart(function(){
			$(this).addClass("loader");
		}).
		ajaxStop(function(){
			$(this).removeClass("loader");
		});
	
	$('.ppy4').popeye();
	$('.ppy2').popeye();
	$('.ppy1.left').popeye();
	$('.ppy3.left').popeye();
	$('.ppy1.right').popeye({direction:'left'});
	$('.ppy3.right').popeye({direction:'left'});
	
	$("select#results").selectbox({inputClass: 'selectbox', containerClass:'selectbox'});
	var is_searching_archives = false;
	function getNewsArchives(val)
	{
		if(!is_searching_archives) {
			is_searching_archives = true;
			jQuery('#archive-container').html("").fadeOut(2000);
			jQuery('#news-archive-loader').fadeIn();
			jQuery.ajax({type: 'POST', url: '/ajax/action/news-archive', data: 'interval=' + val, cache: false, success: function(data){ 
				jQuery('#news-archive-loader').fadeOut();
				jQuery('#archive-container').html(data).fadeIn(2000);
				initTooltips();
				is_searching_archives = false;
				}
			});
		} 
	}
	
	$('div.selectbox ul li').live('click', function(){  getNewsArchives($('form#formMonth select#results').val()); });
	
	var open = 0;
	var slideEffect = "easeInOutExpo";
	//SHOW UPPER DIV WHEN SITEMAP BUTTONS IS PRESSED
	$(".newsletterButton").live('click',function(){
		if(open==0){										 
			$('#newsletterContainer').animate({ width: 530 }, 500, slideEffect , function(){
				//$("#newsletterContainer #form_content").html("").css("display","block").css("min-height","105px").css("width","420px").addClass("loader");
				$("#newsletterContainer").addClass("loader");
				$.ajax({
					type: "POST",
					url: "/ajax/action/newsletter/add",
					cache: false,
					success: function(html){
						$("#newsletterContainer").removeClass("loader").children("#form_content").html(html).slideDown(500,slideEffect ); 
						initTooltips(); 
					}
				});	
				$(".newsletterButton").addClass('current');  
				open=1;	
			});
		} else {
			$('#newsletterContainer').animate({ width: 70 }, 500, slideEffect , function(){	
				$("#newsletterContainer #form_content").css("display","none");
				$("#newsletterContainer #form_content div").remove();
				$(".newsletterButton").removeClass('current');  
				open=0;	
			});
		}
	});
	
});
