$(function() {
	
	$("#top .pngFix").pngFix();
	$("#content").pngFix();
	$("#nav").pngFix();
	
	$("#banner2").Caroussel();
	
	if(LANG == "fr") { $('a.lightbox').lightBox(); }
	else {
		$('a.lightbox').lightBox({
			imageBtnPrev:			'/statics/img/lightbox-btn-prev.gif',
			imageBtnNext:			'/statics/img/lightbox-btn-next.gif',
			imageBtnClose:			'/statics/img/lightbox-btn-close.gif',
			txtImage:				'Image',
			txtOf:					'of'
		});
	}
	
	$("#support a.submit").click(function(e) {
		e.preventDefault();
		$("#support").get(0).submit();
	});
	
	$("#contactForm a.submit").click(function(e) {
		e.preventDefault();
		$("#contactForm input, #contactForm textarea").removeClass("err");
		if(!$.trim($("#contactForm input[name=name]").val()).length) {
			$("#contactForm input[name=name]").addClass("err").blur(function(){ $(this).removeClass("err") }).get(0).focus();
			return;
		} else if(!$.trim($("#contactForm input[name=email]").val()).length) {
			$("#contactForm input[name=email]").addClass("err").blur(function(){ $(this).removeClass("err") }).get(0).focus();
			return;
		} else if(!$.trim($("#contactForm textarea").val()).length) {
			$("#contactForm textarea").addClass("err").blur(function(){ $(this).removeClass("err") }).get(0).focus();
			return;
		} else {
			
			if(LANG == "fr") { $("#contactForm a.submit span").html("Envoi..."); }
			else { $("#contactForm a.submit span").html("Sending..."); }
			$.post("/sendMail.php",$("#contactForm").serialize(),function(data) {
				if(LANG == "fr") { $("#contactForm a.submit span").html("Envoyer"); }
				else { $("#contactForm a.submit span").html("Send"); }
				if(data == "DONE") {
					$("#contactForm input, #contactForm textarea").val("").removeClass("err");
					if(LANG == "fr") {
						$("#contactForm a.submit span").html("Merci!");
						window.setTimeout(function(){ $("#contactForm a.submit span").html("Envoyer"); },1500);
					} else {
						$("#contactForm a.submit span").html("Thank you!");
						window.setTimeout(function(){ $("#contactForm a.submit span").html("Send"); },1500);
					}
				}
			});
		}
	});
	
	$("a.contactGeneral").click(function(e) {
		e.preventDefault();
		$("#contactForm select[name=to]").val("general");
	});
	$("a.contactSales").click(function(e) {
		e.preventDefault();
		$("#contactForm select[name=to]").val("sales");
	});
	$("a.contactPresident").click(function(e) {
		e.preventDefault();
		$("#contactForm select[name=to]").val("president");
	});
	
	$.localScroll();
	
	//demoForm AJAX Submit
	/*	$("#demoForm").submit(function(e){
			e.preventDefault();
			$.post("/ajax.demo.sendMail.php", $("#demoForm").serialize(), function(data) { 										   
						//var data_array=data.split(",");
						if (data.success == "true") {
							$("#demoForm input, #demoForm textarea").each(function() { $(this).val(""); })
							//$("textarea[name=comm]").val("");
							$("#aftermsg").text(data.message).addClass("success").css("display", "block").css("width", "86%");
						} else {
							$("#aftermsg").text(data.message).addClass("error").css("display", "block").css("width", "86%");
						}
							
			}, "json");

		});*/
	
	function demo_post(){
						
						var dialog = $(this);
						dialog.find("#aftermsg").hide().html("").removeClass("success").removeClass("error");
						dialog.find("#aftermsg").css("display", "block").html("<img src='/statics/img/loading_block.gif' width='43' height='11' style='padding:1.02em'>");
						$.post("/ajax.demo.sendMail.php", dialog.find("form").serialize()+"&lang="+LANG, function(data) { 										   
									//alert(data.success);
									if (data.success == "true") {
										dialog.find("input, textarea").each(function() { $(this).val(""); })
										dialog.find("#aftermsg").text(data.message).addClass("success").show();
										setTimeout(function() {dialog.dialog("destroy")}, 1500);
									} else {
										dialog.find("#aftermsg").text(data.message).addClass("error").show();
										dialog.scrollTo(dialog.find("#aftermsg"), {offset:{top:-50}});
									}
										
						}, "json");
								
	}
		
	//DIALOGUE demo btn
	$(".btn_demo").click(function(e) { 
		
		e.preventDefault();
		
		//options
		var opts = {
	
			modal:true,
			width:600,
			height:560,
			draggable:false,
			resizable:false
		
		};
		if(LANG == "fr") { opts.buttons = { "Envoyer":demo_post }; }
		else { opts.buttons = { "Submit":demo_post }; }
		
		//show dialog
		var dialog = $("#demodiv").clone();
		dialog.dialog(opts);
	});
	
});

function refreshTransparency(selector) {
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6) {
		$(selector).each(function() {
			var bgIMG = $(this).css('background-image');
			if(bgIMG.indexOf(".png") != -1){
				var iebg = bgIMG.split('url("')[1].split('")')[0];
				$(this).css('background-image', 'none');
				$(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='crop')";
			}
			
		});
	}
}

$.fn.extend({
	Caroussel: function() {
		
		var car = $(this);
		
		var currentIndex = 0;
		var lastIndex = 0;
		var ie6 = (jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6) ? true:false;
		var carousselOver = false;
		
		car.find(".buttons .button").click(function(e)  {
		
			e.preventDefault();
			
			car.find(".buttons .button").removeClass("selected");
			$(this).addClass("selected");
			
			if(ie6) {
				car.find(".buttons .button").each(function() {
					$(this).get(0).runtimeStyle.filter = null;
				});
				$(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/statics/img/banners/button-over.png',sizingMethod='crop')";
			}
			
			var clickIndex = car.find(".buttons .button").index(this);
			if(clickIndex > currentIndex) {
				car.find(".items .item").eq(currentIndex).css({'zIndex':210}).show().fadeTo(0,1).css({'filter':""});
				car.find(".items .item").eq(clickIndex).css({'zIndex':209}).show().fadeTo(0,1).css({'filter':""});
			
				if(ie6) {
					car.find(".items .item").eq(currentIndex).hide();
				} else {
					car.find(".items .item").eq(currentIndex).stop().fadeTo("slow",0,function(){
						$(this).css({'filter':""});
						$(this).hide();
					});
				}
				
			} else if(clickIndex < currentIndex) {
				if(ie6) {
					car.find(".items .item").eq(currentIndex).hide();
					car.find(".items .item").eq(clickIndex).show();
				} else {
					car.find(".items .item").eq(currentIndex).css({'zIndex':200}).fadeTo("slow",0);
					car.find(".items .item").eq(clickIndex).css({'zIndex':210}).show().fadeTo(0,0);
					car.find(".items .item").eq(clickIndex).fadeTo("slow",1,function() {
						$(this).css({'filter':""});
						car.find(".items .item").eq(lastIndex).hide();
					});
				}
				
			} else {
				car.find(".items .item").hide();
				car.find(".items .item").eq(clickIndex).show();
			}
			lastIndex = currentIndex;
			currentIndex = clickIndex;
			refreshTransparency(car.find(".buttons .selected"));
			
		});
		
		var offset = car.offset();
		car.find(".items .item").each(function(i) {
			$(this).css({ 'position':'absolute', 'zIndex':(203-i) });
		});
		
		if(jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6) {
			refreshTransparency(car.find("div"));
			refreshTransparency(car);
		}
		
		if(currentIndex == 0) {
			car.find(".items .item .step1").fadeIn(1000,function() {
				window.setTimeout(function() {
					car.find(".items .item .step2").fadeIn(1500,function() {
						window.setTimeout(function() {
							car.find(".items .item .step3").fadeIn(1000,function() {
								car.find(".items .item .step4").fadeIn(1000,function() {
									car.find(".items .item .step5").fadeIn(1000);
								});
							});
						},700);
					});
				},300);
			});
		}
		
		var screenshotIndex = 3;
		var screenshotMax = 3;
		
		function loopScreenshot() {
			var index = (screenshotIndex == 1) ? screenshotMax:screenshotIndex-1;
			if(screenshotIndex < index) {
				car.find(".items .item .screen"+index).css("opacity",0).animate({"opacity":1},3000,function() {
					car.find(".items .item .screenshot").css("opacity",1);
					loopScreenshot();
				});
			} else {
				car.find(".items .item .screen"+screenshotIndex).css("opacity",1).animate({"opacity":0},3000,loopScreenshot);
			}
			
			screenshotIndex = index;
		}
		
		loopScreenshot();
		
		function loopButton() {
			
			if (!carousselOver) {
				
				var index = (currentIndex == 2) ? 0:currentIndex+1;	
				car.find(".buttons .button").eq(index).trigger("click");
				currentIndex = index;
			}
			
			buttonTimeout = setTimeout(loopButton, 5000);
		}
		
		var buttonTimeout = setTimeout(loopButton, 5000);
		car.mouseover( function() { carousselOver=true; clearTimeout(buttonTimeout); } );
		car.mouseout( function() { carousselOver=false; buttonTimeout = setTimeout(loopButton, 5000); } );
		
	}
});
