$(window).load(function () {

});

$(document).ready(function(){
	

	if( $('#aside:contains("romana")').length)  {
		$(".newsletter-email").attr("title", "Email address").attr("value", "Email address");
		$("#ajaxSearch_input").attr("title", "Search on website").attr("value", "Search on website");
	};
		
	var pageWidth = $(window).width(),
		headerSwitchWidth = (pageWidth / 2) - 180,
		headerSwitch = $.cookie('headerSwitch');
	
	if ( $(".gallery-foto ul").length ) {
		$(".gallery-foto ul li a").colorbox();		
	};
	
	// add the "def" class to those Ul's and OL's where no class is specified
	$("#main > ul:not([class]), #main > ol:not([class])").addClass("def");

	$("#header").append('<a href="#" id="header-switch" class="min"></a>');
	$("#header-switch").width(headerSwitchWidth);
	
	// check cookie
	if (headerSwitch == 'max') {
		$("#header, #header-wrapper").height(600);
		$("#header-switch").attr("class", "max");
	};
	
	// adding the details from the first featured prod in the header
	var link = $("#featured-gallery li:first").find("a").attr("href"),
		name = $("#featured-gallery li:first").find("a").html();
	$("#featured-details").attr("href", link);
	$("#header-switch").html(name);
	
	// center the featured gallery imgs
	$("#featured-gallery img").css({ left: - (1920 - pageWidth) / 2});
	
	$("#featured-gallery img").click(function(){
		  window.location=$(this).prev("a").attr("href"); return false;
		});
	
	
	if( $(".related-nav-proj").length ) {
		// hide the first categ at projects
		$(".related-nav-proj").each(function() {
			$(this).find("li.tip:first strong").hide();
		})		
	};
	
	$('#main p img.floatCenter').parent().css('text-align', 'center');
	
	if( $("a[href $='.pdf']").length ) {
		// pdf linking :)
		$("a[href $='.pdf']").addClass("pdf-download").wrapInner("<span></span>");
		$("p a[href $='.pdf']:last").after('<div class="clear"></div>');
	};
	
	$(".#sidebar .tip:last-child").addClass("tipLast");
	$(".#sidebar .tip:last-child li:last-child").addClass("last");
	
	if( $(".gallery-trigger li").length ) {
		// numerotate gallery :)
		var galleryTriggerLi = $(".gallery-trigger li").size();
		var galleryTriggerLiStart = 1;
		while( galleryTriggerLiStart <= galleryTriggerLi) {
			$(".gallery-trigger li").each(function() {
				$(this).find("a").attr("rel", "gallery-foto-" + galleryTriggerLiStart);
				galleryTriggerLiStart = galleryTriggerLiStart + 1;
			});

		};		
	};
	
	if( $(".gallery-foto").size() > 0 ) {
		$(this).append('<div class="clear"></div>');
		
		var galleryFoto = $(".gallery-foto").size();
		var galleryFotoStart = 1;
		while( galleryFotoStart <= galleryFoto) {
			$(".gallery-foto").each(function() {
				$(this).attr("id", "gallery-foto-" + galleryFotoStart);
				galleryFotoStart = galleryFotoStart + 1;
			});
		};
	};
			
	// header height animation & cookie
	$("#header-switch").click(function() {
		if( $(this).attr("class") == "min") {			
			$("#header, #header-wrapper").animate({ height: "600px" });
			$(this).animate({ top: "560px" }).attr("class", "max");
			$.cookie('headerSwitch', 'max', {path: "/"});
		} else {
			$("#header, #header-wrapper").animate({ height: "300px" });
			$(this).animate({ top: "260px" }).attr("class", "min");
			$.cookie('headerSwitch', 'min', {path: "/"});
		}
		return false;
	});
	
	if( $(".related-nav-proj").size() > 1) {
		$("#sidebar").prepend('<ul id="project-tab"></ul>');
		
		$(".tab-title").each(function() {
			var toclone = $(this).html();
			var classes = $(this).attr("class");
			classes = classes.replace("tab-title ","");
			$(this).hide();
			if( $('#aside:contains("romana")').length)  {
				$("#project-tab").append('<li class="' + classes + '"><a href="#">Projects ' + toclone + '</a></li>');
			} else {
				$("#project-tab").append('<li class="' + classes + '"><a href="#">Proiecte ' + toclone + '</a></li>');
			};
			
		});
	};
		
	// animate related nav padding
	$('.related-nav a:not(.selected), .depTitle:not(.depTitleActive) a').each(function() {
	        $(this).hover(function() {
	            $(this).stop().animate({ paddingLeft: '10px' }, 200); }
	                , function() {
	            $(this).stop().animate({ paddingLeft: '0px' }, 120);
	        });
	    });
	
		$('.related-nav-proj a:not(.selected)').each(function() {
		        $(this).hover(function() {
		            $(this).stop().animate({ paddingLeft: '19px' }, 200); }
		                , function() {
		            $(this).stop().animate({ paddingLeft: '9px' }, 120);
		        });
		    });
		
		
		//ACCORDION BUTTON ACTION	
		$('div.depTitle a').click(function() {
			
			if ( $(this).parent().next().is(':visible') ) {
				$(this).parent().next().slideUp('normal');
				$(this).parent().removeClass("depTitleActive");
				return false;
			} else {
				$('.depTitleActive').removeClass("depTitleActive");
				$('div.departamentHolder').slideUp('normal');	
				
				$(this).parent().next().slideDown('normal');
				$(this).parent().addClass("depTitleActive");
				return false;
			}
			
		});

		//HIDE THE DIVS ON PAGE LOAD	
		$("div.departamentHolder").hide();
	
 
	// featured gallery, kinda ok
	var featPrevNext = $("#featured-next, #featured-prev");
	
	$('#featured-gallery li:not(.start)').hide();
	
	$("#featured-next").click(function() {
		if( $("#featured-gallery li:last").is(":visible") ) {
			// featPrevNext.hide();
			$("#featured-gallery li:last").fadeOut();
			$("#featured-gallery li:first").fadeIn();
			
			var link = $("#featured-gallery li:first").find("a").attr("href"),
				name = $("#featured-gallery li:first").find("a").html();
			$("#featured-details").attr("href", link);
			$("#header-switch").html(name);
			
		} else {
			// featPrevNext.hide();
			$("#featured-gallery li:visible").fadeOut().next().fadeIn();
			
			var link = $("#featured-gallery li:visible").next().find("a").attr("href"),
				name = $("#featured-gallery li:visible").next().find("a").html();
			$("#featured-details").attr("href", link);
			$("#header-switch").html(name);
			
		}		
		return false;
	});
		
	$("#featured-prev").click(function() {
		if( $("#featured-gallery > li:first").is(":visible") ) {
			// featPrevNext.hide();
			$("#featured-gallery li:first").fadeOut();
			$("#featured-gallery li:last").fadeIn();
			
			var link = $("#featured-gallery li:last:visible").find("a").attr("href"),
				name = $("#featured-gallery li:last:visible").find("a").html();
			$("#featured-details").attr("href", link);
			$("#header-switch").html(name);
		} else {
			// featPrevNext.hide();
			$("#featured-gallery li:visible").fadeOut().prev().fadeIn();
			
			var link = $("#featured-gallery li:visible").find("a").attr("href"),
				name = $("#featured-gallery li:visible").find("a").html();
			$("#featured-details").attr("href", link);
			$("#header-switch").html(name);
		}		
		return false;
	});
	
	$('.gallery-trigger a').each(function() {
		$(this).click(function() {
			var rel = $(this).attr("rel");
			var that = $(this);
			
			if( $(this).parent().is(".selected") ) {
				$("#" + rel).slideUp(400);
				$(this).parent().removeClass("selected");
			} else {
				
				if( $(".gallery-foto:visible").size() > 0 ) {
					$(".gallery-foto:visible").slideUp(400, function() {
						$(".gallery-trigger .selected").removeClass("selected");
						$("#" + rel).slideDown(400);
						that.parent().addClass("selected");		
					});
				} else {
					$("#" + rel).slideDown(400);
					$(this).parent().addClass("selected");			
				}				
	
			}

			return false;
		});
	});	
		
	// stupid tab - need to find a better solution
	$("#project-tab a").each(function() {
		$(this).click(function() {
			var rel = $(this).attr("rel");
			
			$(".project-tab-list").hide().removeClass("selected");
			$("#" + rel).slideDown().addClass("selected");
			
			$("#project-tab a").parent().removeClass("selected");
			$(this).parent().addClass("selected");
			return false;
		});
	});
	
	if( $("#project-tab li").size() > 0 ) {
		// numerotate gallery :)
		var projectTabli = $("#project-tab li").size();
		var projectTabliStart = 1;
		while( projectTabliStart <= projectTabli) {
			$("#project-tab li").each(function() {
				$(this).find("a").attr("rel", "project-list-" + projectTabliStart);
				projectTabliStart = projectTabliStart + 1
			})

		}
		
		$("#project-tab li:last").addClass("alt").after('<div class="clear"></div>')		
	}
	
	if( $(".related-nav-proj").size() > 0 ) {
		
		$(".related-nav-proj").wrap('<div class="project-tab-list"></div>');
		
		var projectListLi = $(".related-nav-proj").size();
		var projectListLiStart = 1;
		while( projectListLiStart <= projectListLi) {
			$(".related-nav-proj").each(function() {
				$(this).parent().attr("id", "project-list-" + projectListLiStart);
				projectListLiStart = projectListLiStart + 1
			})

		}	
	}
	
	if ( $("#project-tab li.selected").size() == 1 ) {
		var rel = $("#project-tab li.selected a").attr("rel");
		$("#" + rel).addClass("selected")
	} else {
		$("#project-tab li:first").addClass("selected")
		$(".related-nav-proj:first").parent().addClass("selected")
	}
	
	$('#select-box').funkytSelect();
	$('#select-box').hide()
	
	
	$(".funkytSelectCaption").click(function() {
	        $(this).parent().find("ul").toggle();
			if( $(this).is(".openned") ) {
				$(this).removeClass("openned")
			} else {
				$(this).addClass("openned")
			}
			return false;
	    });

	    $(".funkytSelectList a").click(function() {
			var funkytSelectId = $(this).parents("div").attr("id");
			var inputSelect = funkytSelectId.replace("-funkytSelect","");

	        var valueReplace = $(this).html();
			$('#' + funkytSelectId).find(".funkytSelectCaption").html(valueReplace);
	        $('#' + funkytSelectId).find("ul").hide();

			$('#' + inputSelect).val($(this).find("span.value").html())
			
			$(".funkytSelectCaption").removeClass("openned")
			return false;
	    });
	
		$('input:text, textarea').each(function() {
		
			var usedinput = $(this);		
			var usedinputtitle = $(this).attr('title');
		
			$(this).click(remove)
		
			function remove() {
				if(usedinput.val() == usedinputtitle) {
					$(this).val("");
				}
			}
		
			$(this).blur(function() {
				if(usedinput.val() == "") {
					$(this).val(usedinputtitle);
					// $(this).addClass('formError')
				} else {
					// $(this).removeClass('formError')
				}
			}).focus(remove)
		
		
		});


		$(".formular input[type=file]").filestyle({ 
		     image: "http://bermo.develora.com/style/img/choose-file.gif",
		     imageheight : 35,
		     imagewidth : 71,
		     width : 472
		 });
		
	
	$(".tabformular li a").each(function() {
		$(this).click(function() {
			var rel = $(this).attr("rel");
			$(".tabformular li.selected").removeClass("selected");
			$(this).parent().addClass("selected")
			$(".formular").hide();
			$('#' + rel).show()
			return false;
		})
	})
	
	$('#ColaborezForm .captcha img').clone().appendTo("#AngajezForm .captcha")
	
	
	$(".club-list .info").simpletooltip();
	
//	$('.accTrigger:first-child a').addClass()
	
	$('.accTrigger a').click(function() {
		
			if( $(this).hasClass('open') ) {
				
				$('.accContent.open').removeClass('open').slideUp('500');
				$('.accTrigger a.open').removeClass('open');
				
				return false;
				
			} else {
				
				$('.accTrigger a.open').removeClass('open');
				$('.accContent.open').removeClass('open').slideUp('500');
				
				$(this).addClass('open').parent().next().slideDown('300').addClass('open');
				
			}
				
			return false;
			
		})
	
});

$.fn.funkytSelect = function() { 
	return this.each(function() {
		var inputSelect = $(this);
		var inputSelectId = $(this).attr("id");	
		var selected = inputSelect.find("option[selected]");
		var options = $("option", inputSelect);
		
		var funkytSelectHtml = '<div id="' + inputSelectId + '-funkytSelect" class="funkytSelectHolder"></div>';	
		inputSelect.after(funkytSelectHtml);
		
		$('#' + inputSelectId + '-funkytSelect').append('<a class="funkytSelectCaption" href="#">' + selected.text() + '<span class="value">' + selected.val() + '</span></a>');
		$('#' + inputSelectId + '-funkytSelect').append('<ul class="funkytSelectList"></ul>');
	
		options.each(function(){
	        $('#' + inputSelectId + '-funkytSelect ul').append('<li><a href="#">' + 
	            $(this).text() + '<span class="value">' + 
	            $(this).val() + '</span></a></li>');
	    });
	
		// inputSelect.hide();
	});
}