﻿$(function() {
	
	$("a.external").bind("click", function() {
		
		window.open($(this).attr('href'));
		return false;
		
	});
	
	//traverse active
	jQuery.each($(".menu .active"), function() {
	
		$($(this).parent("ul").parents("li:eq(0)")).parent("ul").parents("li:eq(0)").addClass("active");
		$(this).parent("ul").parents("li:eq(0)").addClass("active");

	});
	$("ul.dropdown li").bind("click", function() {
		var linkObj = $(this).find("a");
		if($(linkObj).attr('href')!="") {
			if($(linkObj).hasClass('external')) {
				window.open($(linkObj).attr('href'));
			} else {
				document.location.href = $(linkObj).attr('href');
			}
		}
	
	});
	

	// gallery start
	if($("div.gal").length>0) {
		
		$("#gal_switch_label").html(fLang.m.gal.switchView);
		
		// gallery type 1
		$("div.gal img").css("opacity",0.8);
		$("div.gal img").hover(
			function() { $(this).stop().animate({ 'opacity' : 1}, 300); },
			function() { $(this).stop().animate({ 'opacity' : 0.8}, 300); }
		);
		
		// init images

		$.fn.colorbox.settings.transition = "fade";
		$.fn.colorbox.settings.bgOpacity = "0.8";
		$.fn.colorbox.settings.contentCurrent = fLang.m.gal.imageCurrentOutOfTotal;
		$("div.gal a").colorbox();		

		// switch view		
		$("a.switch_thumb").toggle(
			function(){
				$(this).addClass("swap");
				$("ul.display").fadeOut("fast", function() {
					if($("ul.display").hasClass("thumb_view")) {
						$(this).fadeIn("fast").removeClass("thumb_view");
					} else {
						$(this).fadeIn("fast").addClass("thumb_view");
					}
				});
			}, 
			function () {
				$(this).removeClass("swap");
				$("ul.display").fadeOut("fast", function() {
					if($("ul.display").hasClass("thumb_view")) {
						$(this).fadeIn("fast").removeClass("thumb_view");
					} else {
						$(this).fadeIn("fast").addClass("thumb_view");
					}
				});
			}
		); 
	}

	if($(".dynForm").length>0) {
		init_dynForms();
	}	
	
	if($("#cform").size()>0) {
		
		var currForm = $("#cform");
	
		var newForm = $("<form />");
		newForm.attr({
			'method' : 'post',
			'action' : ''
		});			
		
		currForm.wrap(newForm);		
	}
	
});﻿

function init_dynForms() {
	jQuery.each($(".dynForm"), function() {
		var currForm = $(this);
	
		var newForm = $("<form />");
		newForm.attr({
			'method' : 'post',
			'action' : '/' + $(this).attr('rel') + '.html'
		});			
		
		currForm.wrap(newForm);
		
		jQuery.each($("div.dyn", currForm), function() {
		
			var parentForm = currForm;
			var this_id = $(this).attr('id');
			var this_name = $(this).html();
			var this_type = $(this).attr('class').replace(/dyn /ig,"");
			
			var new_input = $("<input type=\"" + this_type + "\" name=\"" + this_name + "\" id=\"" + this_id + "\" />");

			$(parentForm).append(new_input);
			$(this).remove();
		});		
		
	});

	
	$("#fSend").bind('click', function() {
		if( $("#fName").val()!="" && $("#fPhone").val()!="" && $("#fEmail").val()!="" && $("#fCountry").val()!="" && $("#fName").val()!='Name' && $("#fPhone").val()!='Phone number' && $("#fEmail").val()!='Email' && $("#fCountry").val()!='Country') {
			$(this).parents('form:first').submit();
		} else {
			alert("Please fill in all the fields");
		}
	});
	
	$("#fName").val('Name')
	.bind('focus', function() {
		if($(this).val()=='Name') {
			$(this).val('');
		}
	})
	.bind('blur', function() {
		if($(this).val()=='') {
			$(this).val('Name');
		}
	});

	$("#fPhone").val('Phone number')
	.bind('focus', function() {
		if($(this).val()=='Phone number') {
			$(this).val('');
		}
	})
	.bind('blur', function() {
		if($(this).val()=='') {
			$(this).val('Phone number');
		}
	});	
	
	$("#fEmail").val('Email')
	.bind('focus', function() {
		if($(this).val()=='Email') {
			$(this).val('');
		}
	})
	.bind('blur', function() {
		if($(this).val()=='') {
			$(this).val('Email');
		}
	});	
	
	$("#fCountry").val('Country')
	.bind('focus', function() {
		if($(this).val()=='Country') {
			$(this).val('');
		}
	})
	.bind('blur', function() {
		if($(this).val()=='') {
			$(this).val('Country');
		}
	});

}

