$(document).ready(function() {
	//$("#mediasc").scrollable({circular:true}).autoscroll({interval:10000}).navigator({navi:'#mediasc_navi'});
	$("#reel").photoreel(); 
	$(".imschsc").scrollable({circular:true}).autoscroll({interval:10000}).navigator();
	$("#newsscroller").scrollable({circular:true,vertical:true}).autoscroll({interval:10000}).navigator();
	
	if($.browser.msie) {
		menu = $(".menu");
		//$(".menublock").css("height", 31);
		//menu.css("width", 735);
		//menu.css("position", "absolute");
		//menu.css("zIndex", 100);
		//$(".menublock").append(menu);
	} else {
		menu = $(".menu");
	}
	var inmedia = false;
	$(".single .media").click(function() {
		if(inmedia) {
			return false;
		}
		if(inmedia == false) {
			inmedia = true;
		}
		idname = "singlemedia";
		$(this).attr("id", "singlemedia");
		var flv = $(this).attr("href");
		flowplayer(idname, {
			key: '#@f216df94b4672d59c4d',
			src: "/flash/flowplayer.commercial-3.2.7.swf",
			wmode: "transparent",
			canvas:  {
				// configure background properties
				background: '#FFFFFF',
			
				// remove default canvas gradient
				backgroundGradient: 'none',
			
				// setup a light-blue border
				border:'2px solid #778899'
			},
			version: [9, 115],
			onFail: function()  {}
		}, {
			key: '#@f216df94b4672d59c4d',
			autoHide: {
			    enabled: true,
			    hideDelay: 2000
			},
			clip:{
				url:flv,
				scaling:'fit'
			}
		});
		return false;
	});
	
	menu.children("li").each(function() {
		$(this).hover(function() {
			if($(this).children("ul").length > 0) {
				$(this).css("zIndex", 100);
				ul = $(this).children("ul");
				ul.show();
				if($.browser.msie) {
					//if($("#overlay").is(":hidden")) {
					//	$("#overlay").show();
					//	$("#overlay").css("opacity", 0);
					//}
					//$("#overlay").stop().animate({opacity: 0.5});
				} else {
					$(".block").stop().animate({opacity:0.3}, 500);
					$(".media").stop().animate({opacity:0.3}, 500);
				}
				// fade page out 
			}
		}, function() {
			if($(this).children("ul").length > 0) {
				$(this).children("ul").hide();
				if($.browser.msie) {
					//$("#overlay").stop().animate({opacity: 0}, function() {
					//	$("#overlay").hide();
					//});
				} else {
					$(".block").stop().animate({opacity:1}, 500);
					$(".media").stop().animate({opacity:1}, 500);
				}
				// fade page in
			}
		})
		
		if($(this).children("ul").length > 0) {
			me = $(this).children("ul");
			$(me).show();
			me.css("width", (me.children("li").length * me.children("li").eq(0).outerWidth()) + "px" );
			hh = 0;
			me.children("li").each(function() {
				h = parseInt($(this).children("ul").outerHeight());
				hh = h > hh ? h : hh;
			});
			hh = hh + 30;
			me.children("li").each(function() {
				$(this).css("height", hh + "px" );
			});
			$(me).hide();
		}
	});
	$(".readmore").click(function() {
		r = $(this).attr("rel");
		me = $("#" + r);
		if(me.is(":visible")) {
			slideToggle(me, false)
			$(this).find("span").text($(this).attr("more"));
			$(this).find("img").attr("src", "/gfx/readmore.gif");
			
		} else {
			slideToggle(me, true)
			$(this).find("span").text($(this).attr("less"));
			$(this).find("img").attr("src", "/gfx/readless.gif");
		}
		return false;
	});
	
	initForm();
	
	if($("#formmtabs").length > 0) {
		var frmm = $("form.memberform");
		var msubmits = $("form.memberform .fsubmit");
		var mfieldsets = frmm.find("fieldset");
		var msetActive = function(ind) {
			$("#formtabs li").removeClass("active");
			$("#formtabs li").eq(ind).addClass("active");
			
			mfieldsets.hide();
			mfieldsets.eq(ind).show();
		}
		mfieldsets.hide();
		
		$("#formmtabs li a").click(function() {
			pli = $(this).parent("li");
			ind = pli.index();
			msetActive(ind);
			return false;
		});
		
		msubmits.click(function() {
			if(this.name == "btnNext") {
				num = parseInt($(this).attr("num"));
				msetActive(num+1);
				
				return false;
			}
		});
		msetActive(0);
	}

	var addthis_config = {
      username: "ftth",data_track_clickback: true,ui_click:true
	}
	addthis.button(".addthis", addthis_config);
	
	$("#groupgroup").find("strong").click(function () {
		r = $(this).attr("rel");
		$("#groupgroup").find("ul").slideUp();
		$("#" + r).slideDown();
	});
	
	$(".navi").each(function() {
		tmpw = 0;
		$(this.children).each(function() {
			tmpw += parseInt($(this).outerWidth());
			
		});
		pw = $(this).parent("div").width();
		tmpw = (pw - tmpw) / 2;
		if(tmpw > 0) {
			$(this).css("marginLeft", tmpw);
		}
	});
	
	$(".popwindow").click(function() {
		frm = $(this).attr("rel");
		if(frm == "pop-up-signup") {
			if($("#loadform").children().length == 0) {
				geturl = $("#loadform").attr("member");
				$.get(geturl, function(da) {
					$("#pop-up-signup .pop-text").html("");
					$("#pop-up-signup .pop-text").append($(".title", da));
					$("#pop-up-signup .pop-text").append($(".content1", da));
					
					$("#loadform").html($("#small", da).html());
					$("#loadform").find(".title").remove();
					$("#loadform").find(".content1").remove();
					reCaptcha($("#loadform"));
					initForm();
				});
			}
		}
		
		if(frm == "pop-up-signupcompany") {
			if($("#loadformmember").children().length == 0) {
				geturl = $("#loadformmember").attr("member");
				$.get(geturl, function(da) {
					$("#pop-up-signupcompany .pop-text").html("");
					$("#pop-up-signupcompany .pop-text").append($(".title", da));
					$("#pop-up-signupcompany .pop-text").append($(".content1", da));
					
					$("#loadformmember").html($("#small", da).html());
					$("#loadformmember").find(".title").remove();
					$("#loadformmember").find(".content1").remove();
					reCaptcha($("#loadformmember"));
					initForm();
				});
			}
		}
		
		if($("#" + frm).is(":visible")) {
			
		} else {
			if($(".pop-window:visible").length > 0) {
				var me = $("#" + frm);
				$(".pop-window:visible").slideUp(500, function() {
					me.slideDown();
				});
			} else {
				$("#" + frm).slideDown();
			}
		}
		
	});
	$(".close-panel").click(function() {
		$(".pop-window").slideUp();
		return false;
	});
	$(".close-panel-small").click(function() {
		$(".pop-window").slideUp();
		return false;
	});
	
	$(".showhide").click(function() {
		$(this).parent().parent().children().each(function() {
			$(this).removeClass("active");
			rrr = $(this).children().eq(0).attr("rel");
			if(rrr) {
				$("#" + rrr).hide();
			}
		});
		$(this).parent().addClass("active");
		r = $(this).attr("rel");
		$("#" + r).show();
		return false;
	});
	
	/*
	$(".agendanav a").live("click", function() {
		$("#agenda").animate({opacity:0.5}, 200);
		$.get(this.href, function(da) {
			$("#agenda").html(($("#agenda", da).html()));
			$("#agenda").animate({opacity:1}, 200);
		});
		
		return false;
	});
	*/
	$(".agendanav a").live("click", function() {
		var agenda = $(this).parent().parent();
		//agenda.hide(200);
		$.get(this.href, function(da) {
			agenda.html($(".agenda", da).html());
			//agenda.show(200);
		});
		return false;
	});
	
	
	$(".harmonica").click(function() {
		if($(this).find("h3").hasClass("active")) {
			return false;
		}
		var pp = $(this).parent();
		pp.children().each( function() {
			if($(this).hasClass("harmonica")) {
				$(this).find("h3").removeClass("active");
			} else {
				//$(this).slideUp(500);
				slideToggle(this, false); 
			}
		});
		$(this).find("h3").addClass("active");
		
		var r = $(this).attr("rel");
		setTimeout(function() {
			slideToggle($(pp).find("." + r), true); 
		}, 200);
	});

	function slideToggle(el, bShow){
		
	  var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");
	  
	  // if the bShow isn't present, get the current visibility and reverse it
	  if( arguments.length == 1 ) bShow = !visible;
	  
	  // if the current visiblilty is the same as the requested state, cancel
	  if( bShow == visible ) return false;
	  
	  // get the original height
	  if( !height ){
	    // get original height
	    height = $el.show().height();
	    // update the height
	    $el.data("originalHeight", height);
	    // if the element was hidden, hide it again
	    if( !visible ) $el.hide().css({height: 0});
	  }
	
	  // expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
	  if( bShow ){
	    $el.show().animate({height: height}, {duration: 500});
	  } else {
	    $el.animate({height: 0}, {duration: 500, complete:function (){
	        $el.hide();
	      }
	    });
	  }
	}
});
function getNum() {
	var rand_no = Math.random();
	rand_no = Math.floor(rand_no * 10000);
	return rand_no;
}
function initForm() {
	$(".theform").each(function(i) {
		if($(this).attr("id")) {
			
		} else {
			fn = getNum();
			$(this).attr("id", "randform" + fn);
		}
		var tabs = $(this).find(".formtabs");
		var frm = $(this).find("form");
		var submits = frm.find(".fsubmit");
		var fieldsets = frm.find("fieldset");
		
		if(tabs.length > 0 ) {
			var setActive = function(ind) {
				tabs.find("li").removeClass("active");
				tabs.find("li").eq(ind).addClass("active");
				
				fieldsets.hide();
				fieldsets.eq(ind).show();
			}
			
			fieldsets.hide();
			
			tabs.find("li a").click(function() {
				pli = $(this).parent("li");
				ind = pli.index();
				setActive(ind);
			});
			
			submits.click(function() {
				if(this.name == "btnNext") {
					num = parseInt($(this).attr("num"));
					setActive(num+1);
					return false;
				}
			});
			setActive(0);
			
			var trg = $(this).attr("id");
			frm.attr("reltheform", trg);
			
			var options = { 
		          beforeSubmit:  initRequest
		        , success: showResponse 
		    }; 
		    $(frm).ajaxForm(options); 	
		} else {
			if(frm.attr("noajax")) {
				// sign up
			} else {
				var trg = $(this).attr("id");
				frm.attr("reltheform", trg);
				
				var options = { 
			          beforeSubmit:  initRequest
			        , success: showResponse 
			    };
			    
			    $(frm).ajaxForm(options); 	
			}
		}
		
	});
	
	$("input").placeholder(); 
}
var formtarget;
function initRequest(formData, jqForm, options) { 
	formtarget = $("#" + jqForm.attr("reltheform"));
}
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
	out = $(".theform", responseText).html();
    $(formtarget).html(out);
    reCaptcha(formtarget);
    initForm();
} 
function reCaptcha(trg) {
	$(trg).find(".recaptcha").each(function() {
		
		Recaptcha.create("6LfNk8MSAAAAAEu1uIFeAjZtrKvirX-MFB4nkKzX", this,{
      		theme: "red",
      		callback: Recaptcha.focus_response_field
    	});
	});
}


function printCompany() {
	var lnk = '/?module=company&popup=1';
	if (document.getElementById("allCompany").checked ) {
		lnk += '&all=1';
	}else if (document.getElementById("selectionCompany").checked ) {
		lnk += '&a=' + document.getElementById("selectionCompany").value;
	};
	PopUp(lnk, 'company', 600, 400, 'yes', 'center');	
}

function PopUp(page,name,w,h,scroll,pos){
	if(pos=="random"){
		LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
		TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
	} else if(pos=="center"){
		LeftPosition=(screen.width)?(screen.width-w)/2:100;
		TopPosition=(screen.height)?(screen.height-h)/2:100;
	} else if(pos=="right"){
		LeftPosition=(screen.width)?(screen.width-w):1024-w;
		TopPosition=0; h=Math.min(screen.height,600);
	} else {
		LeftPosition=0;
		TopPosition=20;
	}
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
	return window.open(page,name,settings);
}
function showHide (who) {
	t = $("#" + who);
	
	if(t.is(":visible")) {
		val = "none";
		t.slideUp();
		$("#im_" + who).attr("src", "/gfx/folder.gif");
		$("#link_" + who).text("show files...");
	} else {
		val = "block";
		t.slideDown();
		$("#im_" + who).attr("src", "/gfx/folder_images.gif");
		$("#link_" + who).text("hide files...");
	}
	$.cookies.set(who, val);
}

function openclose(i) {
	if($("#info" + i).is(":hidden")) {
		$("#info" + i).slideDown(500);
		$("#inf" + i).attr("src", "/gfx/infoopen.gif");
	} else {
		$("#info" + i).slideUp(500);
		$("#inf" + i).attr("src", "/gfx/infodicht.gif");
	}
}

function openAll() {
	$(".element").slideDown(500);
	$(".elminfo").attr("src", "/gfx/infoopen.gif");
}
function closeAll() {
	$(".element").slideUp(500);
	$(".elminfo").attr("src", "/gfx/infodicht.gif");
}

