// JavaScript Document
$(document).ready(function(){
	//popup gallery
	$("a[rel=example_group]").fancybox({
		'overlayOpacity'    : 0.8,
		'overlayColor'      : '#000000',
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Screenshot ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		},
		'onComplete'	:	function() {
			$("#fancybox-wrap").hover(function() {
				$("#fancybox-title").show();
			}, function() {
				$("#fancybox-title").hide();
			});
		}
	});
	
	//global mouse over
	$("img.over").bind({
		mouseenter: function(){
			if($(this).attr("rel")!="0"){	
				this.src=this.src.replace('.' + getEXT(this.src),'_on.' + getEXT(this.src));
			}
		},
		mouseleave: function(){
			if($(this).attr("rel")!="0"){	
				this.src=this.src.replace('_on.' + getEXT(this.src),'.' + getEXT(this.src));	
			}
		}
	});
	
	$("#feedback_pop, #contact, #support").click(function(){
		$("#fancybox-close").css("top", "20px").css("background-image", "url(images/fancybox/fancy_close_c.png)").css("width", "17px").css("height", "16px").css("background-position", "0 0").css("right", "20px")
		$.ajax({
			type: "POST",
			url: "contact_form.php",
			data: "head="+$(this).attr("alt")+"&subject="+$(this).attr("alt"),
			cache	: false,
			success: function(data){
				$.fancybox(
				data,{
					'overlayOpacity'    : 0.8,
					'overlayColor'      : '#000000',
					'scrolling'         : 'no',
					'onClosed'          : function(){
						$("#fancybox-close").attr("style", "");
					}
				});
				var Send = false;
				$("#send").click(function(){
					$(".required").each(function(){
						//cheack if not empty
						if($(this).val()==""){
							$("#er_"+$(this).attr("id")).show();
							Send = false;
						}else{
							//#email
							if(isValidEmailAddress($("#email").val())){
								Send = true;
							}else{
								Send = false;
								$("#er_email").text("email is not valid").show();
							}
						}
						
						//hendle on focus
						$(this).focus(function(){
							if($("#er_"+$(this).attr("id")).is(":visible")){
								$("#er_"+$(this).attr("id")).hide();
							}
						});
					});
					//end of checking
					if(Send){
						$.ajax({
							type    : "POST",
							url     : "contact_process.php",
							cache	: false,
							data    : "name="+$("#Name").val()+"&Email="+$("#email").val()+"&Phone="+$("#phone").val()+"&Message="+$("#message").val()+"&subject="+$("#subject").val(),
							success: function(data){
								alert("email sent successfully");
							}
						});
					}
				});
			}
		});
	});
	
});

//get file extension
function getEXT(str){
	str=str.split('.');
	ext=str[str.length -1];
	return ext;
}
function isValidEmailAddress(emailAddress){
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
