//$(document).ready(function() {

	//BETA logo
	var listEl = document.createElement('li');
	listEl.className = 'extended';
	var logoEl = document.createElement('p');
	var imgEl = document.createElement('img');
	imgEl.src = "/images/bullets/beta.gif";
	imgEl.alt = "BETA Version";
	$("#utils > ul").prepend(listEl);
	$("#utils > ul > li:eq(0)").append(logoEl);
	$("#utils > ul > li:eq(0) > p").append(imgEl);

	//terms
	$("p.terms > a").fancybox({
		'overlayOpacity' : 0.5,
		'frameWidth':600,
		'hideOnContentClick':false
	});
	
	//slide content onLoad
	$("#content").hide();
	//setTimeout('$("#page #main #content").slideDown(400)',200);
	$("#content").slideDown(400);
		
	//show submenus onClick
	$("#header ul.primaryNav li div.menuItem p.itemTitle").each(function(i) {
		$(this).click(function() {
			$("#header ul.primaryNav li.submenu").hide();
			$("#header ul.primaryNav li.submenu").eq(i).show(300);
			$("#header ul.primaryNav li div.menuItem").removeClass("selected");
			$("#header ul.primaryNav li div.menuItem").eq(i).addClass("selected");
			//e.preventDefault();
		});
	});

	$("#header ul.primaryNav li div.menuItem").each(function(i) {
		$(this).click(function() {
			$("#header ul.primaryNav li.submenu").hide();
			$("#header ul.primaryNav li.submenu").eq(i).show(300);
			$("#header ul.primaryNav li div.menuItem").removeClass("selected");
			$("#header ul.primaryNav li div.menuItem").eq(i).addClass("selected");
			var myURL=location.href;
			if (i==0){
				var urlToGo=$("#header #menu ul.primaryNav li.about div.submenuItem ul li:first p a").attr("href");
				if (myURL.indexOf("despre-noi")==-1 && myURL.indexOf("about-us")==-1) { 
					setTimeout('location.href="'+urlToGo+'"',700); 
				}
			}
			if (i==1){
				var urlToGo=$("#header #menu ul.primaryNav li.services div.submenuItem ul li:first p a").attr("href");
				if (myURL.indexOf("servicii")==-1 && myURL.indexOf("services")==-1) { 
					setTimeout('location.href="'+urlToGo+'"',700); 
				}
			}
			if (i==2){
				var urlToGo=$("#header #menu ul.primaryNav li.projects div.submenuItem ul li:first p a").attr("href");
				if (myURL.indexOf("proiecte")==-1 && myURL.indexOf("projects")==-1) { 
					setTimeout('location.href="'+urlToGo+'"',700); 
				}
			}
			if (i==3){
				var urlToGo=$("#header #menu ul.primaryNav li.events div.menuItem p.itemTitle a").attr("href");
				location.href=urlToGo;
			}
		});
	});
	
	if($('body').hasClass('team')) {
		//about us: team
		$("body.team #page #main #content #primary #body ul.leftColumn li div.people h3.name a").each(function(i,el) {
			if ($(this).hasClass("active")) {
				$("body.team #page #main #content #primary #body ul.rightColumn li").eq(i).show();
			}
			$(this).click(function(e) {
				$("body.team #page #main #content #primary #body ul.rightColumn li").hide();
				$("body.team #page #main #content #primary #body ul.rightColumn li").eq(i).fadeIn();
				$("body.team #page #main #content #primary #body ul.leftColumn li div.people h3.name a").removeClass("active");
				$(this).addClass("active");
				e.preventDefault();
			})
		});
	}

	if($('body').hasClass('values')) {
		//about us: values
		$("#page #main #content #primary #body .leftColumn ul.presentation li h2").each(function(i) {
			if ($(this).hasClass("active")) {
				$("#page #main #content #primary #body .leftColumn ul.presentation li p.arrow").eq(i).show();
				$("#page #main #content #primary #body div.details").eq(i).show();
			}
			$(this).mouseover(function() {
				$("#page #main #content #primary #body .leftColumn ul.presentation li p.arrow").hide();
				$("#page #main #content #primary #body .leftColumn ul.presentation li p.arrow").eq(i).show();
			}).click(function(e) {
				$("#page #main #content #primary #body div.details").hide();
				$("#page #main #content #primary #body div.details").eq(i).fadeIn();
				e.preventDefault();
			});
		});
	}

	if($('body').hasClass('events')) {
		//events
		$("#page #main #content #primary #body ul.eventList li p a").each(function(i) {
			$(this).click(function(e) {
				$("#page #main #content #primary #body ul.eventList, body.events #page #main #content #primary #head").hide();
				$("#page #main #content #primary #body ul.eventDetails > li").hide();
				$("#page #main #content #primary #body ul.eventDetails li div.eventDescription div.eventBody ul li, #page #main #content #primary #body ul.eventDetails li div.eventDescription div.eventBody ol li").show();
				$("#page #main #content #primary #body ul.eventDetails > li").eq(i).slideDown();
				e.preventDefault();
			});
		});

		$("#page #main #content #primary #body ul.eventDetails li div.eventDescription div.eventHead p.backToEvents a").each(function(i) {
			$(this).click(function(e) {
				$("#page #main #content #primary #body ul.eventDetails li").hide();
				$("#page #main #content #primary #body ul.eventList").slideDown();
				$("#page #main #content #primary #body ul.eventList, body.events #page #main #content #primary #head").show();
				e.preventDefault();
			});
		});
	}

	if($('body').hasClass('services')) {
		//services
		$("#page #main #content #primary #body .leftColumn div.presentation div.service h2").each(function(i) {
			if ($(this).hasClass("active")) {
				$("#page #main #content #primary #body .leftColumn div.presentation div.service p.arrow").eq(i).show();
				$("#page #main #content #primary #body div.details").eq(i).show();
			}
			$(this).mouseover(function() {
				$("#page #main #content #primary #body .leftColumn div.presentation div.service p.arrow").hide();
				$("#page #main #content #primary #body .leftColumn div.presentation div.service p.arrow").eq(i).show();
			}).click(function(e) {
				$("#page #main #content #primary #body div.details").hide();
				$("#page #main #content #primary #body div.details").eq(i).fadeIn();
				e.preventDefault();
			});
		});

		//services: approvals
		$("#page #main #content #primary #body div.details h2.subList a ").each(function(i) {
			$(this).click(function(e) {
				$("#page #main #content #primary #body div.details ul.subList_content").slideUp();
				$("#page #main #content #primary #body div.details ul.subList_content").eq(i).slideDown();
				e.preventDefault();
			});
		});
	}
	
	if($('body').hasClass('contact')) {
		//contact
		var myURL= document.location.href;
		if (myURL.indexOf("/en/")==-1) { 
			var numeContact="Nume";
			var mesajContact="Mesaj";
		}else{ 
			var numeContact="Name";
			var mesajContact="Message";
		}
		$("div.contactForm form div.element input[name='name']").focus(function() {
			if (this.value == numeContact) {
				this.value = '';
			}
		}).blur(function() {
			if (this.value == '') {
				this.value = numeContact;
			}
		});

		$("div.contactForm form div.element input[name='email']").focus(function() {
			if (this.value == 'Email') {
				this.value = '';
			}
		}).blur(function() {
			if (this.value == '') {
				this.value = 'Email';
			}
		});

		 $("div.contactForm textarea#formMessage").focus(function() {
			if ($(this).val() == mesajContact) {
				$(this).val("");
			}
		}).blur(function() {
			if ($(this).val() == "") {
				$(this).val(mesajContact);
			}
		});
		
		$("div.contactForm form#contactForm").submit(function() {
			if ($("input").val()=="" ||  $("input").val()=="Nume" || $("input").val()=="Name" || $("input").val()=="Email" || $("textarea#formMessage").val()=="" || $("textarea#formMessage").val()=="Mesaj" || $("textarea#formMessage").val()=="Message") {
				$("input[name!='submit']").css({'color':'red', 'font-weight':'bold'});
				$("textarea#formMessage").css({'color':'red', 'font-weight':'bold'});
				return false;
			}
			return true;
		});
	}

	//show project details
	if($('body').hasClass('projects') || $('body').hasClass('homepage')) {
		$("div.projectsOverview ul.projectsList li p a.highlighted").each(function(i) {
			var projectName=$(this).attr("rel");
			$(this).click(function(e) {
				var $content = $("body.projects #page #main #content");
				$("#primary #body div.projectsOverview", $content).slideUp("slow");
				$("#pageTitle2").hide();
				$("#primary #body ul.projectDetails", $content).slideDown("slow");
				$("#primary #body ul.projectDetails > li#"+projectName, $content).removeClass("hide").addClass("show");
				e.preventDefault();
			});
		});

		$("div.projectsOverview ul.projectsList li div.icons p.hasInfo a").each(function(i) {
			$(this).click(function(e) {
				// var projectName=$("div.projectsOverview ul.projectsList li p a.highlighted").eq(i).attr("rel");
				var projectName= $(this).parent().parent().parent().find("a.highlighted").attr("rel");
				//$("body, #outer #container #page").css({'background' : 'url("/images/projects/'+bgSrc+'/bg-'+bgSrc+'_'+j+'.jpg") no-repeat center center', 'background-attachment' : 'fixed'});
				var $body = $("body.projects #page #main #content #primary #body");
				$("div.projectsOverview", $body).slideUp("slow");
				$("#pageTitle2").hide();
				$("ul.projectDetails", $body).slideDown("slow");
				$("ul.projectDetails > #"+projectName, $body).removeClass("hide").addClass("show");
				e.preventDefault();
			});
		});
		
		//back to projects
		$("ul.projectMenu li p a.backToProjects").each(function(i) {
			$(this).click(function(e) {
				//$("body, #outer #container #page").css({'background' : 'url("/images/backgrounds/bg-projects.jpg") no-repeat center center', 'background-attachment' : 'fixed'});
				$("#page #main #content #pageTitle2").show().removeClass("hide").addClass("show");
				var $body = $("body.projects #page #main #content #primary #body");
				$("ul.projectDetails > li", $body).eq(i).removeClass("show").addClass("hide");
				$("ul.projectDetails", $body).slideUp("slow");
				$("div.projectsOverview", $body).slideDown("slow");
				e.preventDefault();
			});
		});

		//photo gallery
		$("ul.projectMenu li p a.gallery, div.projectsOverview ul.projectsList li div.icons p.hasPictures a, div.projectsOverview ul.projectsList li p a.noDetails").click(function(e) {
			if($(this).hasClass('hasNothing')) {
				e.preventDefault();
				return false;
			}
			var galleryId = $(this).attr("rel");
			setTimeout(function() {
				//$('#page').load('../_gallery/'+galleryId+'.html');
				jQuery.ajax({
					url: './'+galleryId+'.html',
					dataType: "html",
					complete: function(res, status){
						var selector = false;
						if( status == "success" || status == "notmodified" ) {
							// See if a selector was specified
							$('#page').append( 
								selector 
								?
								jQuery("<div/>").append(res.responseText.replace(/<script(.|\s)*?\/script>/g, "")).find(selector)
								:
								res.responseText
							);
							doSlideshow("#page > ul."+galleryId);
							$("#page > ul."+galleryId).fadeIn();
						}
					}
				});
				//$("body.projects #page ul."+galleryId).fadeIn();
			}, 600);
			$("#page #main #content").slideUp("slow");
			setTimeout(function() {
				$("#page ul.galleryNav").fadeIn();
			},1000);
			e.preventDefault();
			return false;
		});
		
		//back to project details / projects list
		$("#page ul.galleryNav li p a.close").click(function(e) {
			$("#page ul.galleryNav, #page ul.image-slide").fadeOut();
			setTimeout('$("#page #main #content").slideDown("slow")',300);
			e.preventDefault();
		});
		
		
		//slideshow
		var doSlideshow = function(selector) {
			var s = selector || '#page > ul.image-slide';			
			$(s).each(function() {
				var slidesCount = 0;
				var lastSlide = null;
				var enableAutomaticSlide = true;
				var animationInProgress = false;
				var container =  this;
				var slideDuration = 3;//seconds
				
				// cound and mark slides
				$(this).children('li').each(function(i, currentSlide) {
					slidesCount++;
					lastSlide = 'slide'+slidesCount;
					$(this).addClass('slide'+slidesCount);
					if(i == 0) {
						$(this).addClass('current');
					}
					else if(i > 0) {
						$(this).hide();
					}
				});
				
				// create the navigation and the fader
				if(slidesCount > 1) {
					var nav = document.createElement('div');
					nav.className = 'nav';
					var a = document.createElement('a');
					a.innerHTML = '&nbsp;';
					a.className = 'prev';
					/*a.onclick = function(e) {
						if(!animationInProgress) {
							var currentSlide = parseInt($(container).find('div.nav > span.current').text());
							var nextSlide = (currentSlide - 1) >= 1 ? currentSlide - 1 : slidesCount;
							changeSlide(currentSlide, nextSlide);
						}
						e.stopPropagation();
						return false;
					};*/

					//pb gallery nav - prev
					$("#page ul.galleryNav li p a.prev").click(function(e) {
						var currentSlide = parseInt($(container).find('div.nav > span.current').text());
						var nextSlide = (currentSlide - 1) >= 1 ? currentSlide - 1 : slidesCount;
						changeSlide(currentSlide, nextSlide);
						//e.stopPropagation();
						//e.preventDefault();
						return false;
					});

					nav.appendChild(a);
					var span = document.createElement('span');
					span.className = 'current';
					span.innerHTML = '1';
					nav.appendChild(span);
					span = document.createElement('span');
					span.className = 'separator';
					span.innerHTML = '/';
					nav.appendChild(span);
					span = document.createElement('span');
					span.className = 'slides';
					span.innerHTML = slidesCount;
					nav.appendChild(span);
					span = null;
					a = document.createElement('a');
					a.innerHTML = '&nbsp;';
					a.className = 'next';
					/*a.onclick = function(e) {
						if(!animationInProgress) {
							var currentSlide = parseInt($(container).find('div.nav > span.current').text());
							var nextSlide = (currentSlide + 1) <= slidesCount ? currentSlide + 1 : 1;
							changeSlide(currentSlide, nextSlide);
						}
						e.stopPropagation();
						return false;
					};*/

					//pb gallery nav - next
					$("#page ul.galleryNav li p a.next").click(function(e) {
						var currentSlide = parseInt($(container).find('div.nav > span.current').text());
						var nextSlide = (currentSlide + 1) <= slidesCount ? currentSlide + 1 : 1;
						changeSlide(currentSlide, nextSlide);
						//e.stopPropagation();
						//e.preventDefault();
						return false;
					});

					nav.appendChild(a);
					a = null;
					$(this).append(nav);
					nav = null;
					fader = document.createElement('div');
					fader.className = 'fader';
					$(this).append(fader);
					fader = null;
				}
				$(this).children('div.nav').children('span').hide();
				
				var changeSlide = function(oldSlide, newSlide) {
					//fade
					animationInProgress = true;
					$(container).find('li.slide'+oldSlide).removeClass('current').fadeOut(100, function() {
						$(container).find('li.slide'+newSlide).addClass('current').fadeIn(300, function() {
							animationInProgress = false;
						});
					});
					/*$(container).find('div.fader').fadeIn(500, function() {
						// slide
						$(container).find('div.slide'+oldSlide).removeClass('current').hide();
						$(container).find('div.slide'+newSlide).addClass('current').show();
					}).fadeOut(500,  function() {
						animationInProgress = false;
					});*/
					// span
					$(container).find('div.nav > span.current').text(newSlide);
				}
				
				if($(container).hasClass('automatic-slide')) {
					var slideShow = function() {
						if(enableAutomaticSlide) {
							var slideTimer = setInterval(function() {
								if(!animationInProgress) {
									var currentSlide = parseInt($(container).find('div.nav > span.current').text());
									var nextSlide = (currentSlide + 1) <= slidesCount ? currentSlide + 1 : 1;
									changeSlide(currentSlide, nextSlide);
								}
							}, slideDuration*1000);
						}
					}
					slideShow();
				}
			});
		}

		//homepage slideshow
		doSlideshow("#page > ul#slideshow");
	}	

	
//});

// IE6 Menu Fix
// @moved ie6.js
//$(function(){
	$('#page #header #menu ul.primaryNav li div.menuItem').addClass('whiteBkg');
//});

//$(function(){	
	if ($("body").hasClass("projects")){
		
		var listExceptions = function() {
			this.exceptions = ['birouri-si-hoteluri', 'office']
			for(var i = this.exceptions.length - 1; i >= 0; i--) {
				if(document.location.href.indexOf(this.exceptions[i]) != -1) {
					return true;	
				}
			}
			return false;
		}

		elementsNr = listExceptions() ? 18 : 15;
		projectsNr = $("ul.projectsList").children().length;

		var displayProjects = function(start,finish){
			$("ul.projectsList li").hide();
			for (var j=start; j<finish ; j++){
				$("ul.projectsList li:eq("+j+")").show();
			}
		}

		// HIDE photos icon when projects have no picture gallery
		var $divIcons = $('ul.projectsList li div.icons');
		$("p.hasPictures a", $divIcons).each(function(i,e){
			var galleryElement = $(e).attr("rel");
			if (!$("ul").hasClass(galleryElement) && window.console){
				console.log('[RDX][WARN] gallery hide js action needed?');
				//$(e).parent().remove();
				//$("#"+galleryElement+" ul.projectMenu li:eq(1)").hide()
				//$("#"+galleryElement+" ul.projectMenu li:eq(0)").css({"float":"left"});
				//$("#"+galleryElement+" ul.projectMenu li:eq(2)").css("float","right");
				//if ($.browser.msie){
				//	$("#"+galleryElement+" ul.projectMenu ").css("width","210px");
				//}
			}
		});
		$divIcons.each(function(i,e){
			if ($("ul.projectsList li div.icons:eq("+i+") p:visible").length == 0){
				$("ul.projectsList li p.projectTitle:eq("+i+") a").removeClass();
				$("ul.projectsList li p.projectTitle:eq("+i+") a").addClass("hasNothing");
			}	
		});

		// Check if there are too many projects and the display is over the logo
		if (projectsNr > elementsNr){
			var anchorsNr = Math.ceil(projectsNr / elementsNr);
			$("ul.projectsList li:gt("+elementsNr+")").hide();
			$("p.linkNav").removeClass('hidden');
			for (var i=0; i<anchorsNr; i++){
				var tempAnchor = document.createElement('a');
				tempAnchor.className = 'linkList';
				tempAnchor.title = i+1;
				tempAnchor.innerHTML = i+1;
				$("p.linkNav").append(tempAnchor);
			}
			$("p.linkNav a:eq(0)").addClass("disabled");
			$("p.linkNav a").each(function(i,e){
				$(e).click(function(){
					var str = 0;
					var fin = 0;
					$("p.linkNav > a.linkList").removeClass("disabled");
					$("p.linkNav > a.linkList:eq("+i+")").addClass("disabled");
					
						switch ((i+1)%3){
							case 1:
								str = (i*elementsNr);
								fin = (i+1)*elementsNr+1;
								break;
							case 2:
								str = (i*elementsNr+1)
								fin = (i+1)*elementsNr+2
								break;
							case 0:
								str = (i*elementsNr)+2;
								fin = (i+1)*elementsNr+3;
								break;
						}
						displayProjects(str,fin);
				});
			});
		}
	}

	//movie player 
	/*$('a.play_film').click(function(){
		$(this).hide();
		$('#movie-container').show();
		$('#slideshow').hide();
		return false;
	});

	$('a.close-movie').click(function(){
		$('a.play_film').show();
		$('#movie-container').hide();
		$('ul#slideshow').show();
		doSlideshow('#page > ul#slideshow');
		return false;
	}); */
	


	$("#video_thumb a").fancybox({
		'overlayOpacity' : 0.98,
		'frameWidth':600,
		'frameHeight':450,
		'hideOnContentClick':false
	});
	
	$('div.video_strip a.video_title').fancybox({
		'overlayOpacity' : 0.98,
		'frameWidth':600,
		'frameHeight':450,
		'hideOnContentClick':false
	}); 
	
	var params = {
		movie: "../swf/ngxplayer.swf", 
		wmode: 'transparent',
		allowScriptAccess: 'always',
		quality: 'high'		
	}; 
	swfobject.embedSWF("../swf/ngxplayer.swf", "swf-container", "600", "450",'9.0.0','','', params);


//});
