var aimgCount = 0;
var image_content;
$(document).keypress(function(e) {
	 var code = (e.keyCode ? e.keyCode : e.which);
	 if(code == 13){
		 return false;
	 }
	});
function clearText(field){
	if (field.defaultValue == field.value) field.value = '';
	else if (field.value == '') field.value = field.defaultValue;
} 

function newsletter(){
	var email = $('#newsletter').val();    
	$.ajax({
		url:'/blog/newsletter',
		data:'email='+email,
		type:'POST',
		success: function(data){        
			$('#subscription').html(data);
		}
	});
}

function case_study(id){
	var imgSliderDiv = $('.image-slider_1');
	var leftImgSlide = imgSliderDiv.position().left
	if(leftImgSlide != 0){
		leftImgSlide = 0;
		imgSliderDiv.css({
			'left':leftImgSlide+'px'  
		});
	}
	
	$.ajax({
		url:'/casestudy/getCaseStudyImages',
		data:'id='+id,
		type:'POST',
		success: function(data){
			aimgCount = 0;
			$('#imgpreview').html(data);
			var imgSliderDiv = $('.image-slider_1');
			imgSliderDiv.find('a').each(function(){
				aimgCount = aimgCount +1;
			});
			imgSliderDivWidth = imgSliderDiv.width();
			imgPrevWidth = $(".image-slider_1 img").width();
			//imgPrevWidth = imgPrevWidth +10;
			imgTot = imgSliderDiv.find('a').size();
			impPreRealWdt = imgPrevWidth * imgTot / 1;
			imgSliderDiv.css({
				'width': impPreRealWdt
			});
		}
	});
}

function imgslide(){
	var imgSlider = $('.image-slider');
	imgSliderWidth = imgSlider.width();
	imgWidth = $('.img-window').width();
	imgSum = imgSlider.find('a').size();
	imgReelWidth = imgWidth * imgSum / 6;
	imgSlider.css({
		'width': imgReelWidth
	});



	slide = function() {
		$('#right_small').click(function() {
			var l = Math.round(imgSlider.position().left);
			if (l <= imgSliderWidth - imgReelWidth) {
				return ;
			}
			l = (l - imgWidth < -imgSliderWidth) ? imgSliderWidth - imgReelWidth : l - imgWidth;
			imgSlider.animate({ left: l
			}, 1000);
		});

		$('#left_small').click(function() {
			var l = imgSlider.position().left;
			if (l === 0) {
				return;
			}
			l = (l + imgWidth > 0) ? 0 : l + imgWidth;        
			imgSlider.animate({left: l}, 1000);
		});



	};
	slide();
}

function image_slide_preview(){
	var imgSliderDiv = $('.image-slider_1');
	imgSliderDivWidth = imgSliderDiv.width();
	imgPrevWidth = $(".image-slider_1 img").width();
	//imgPrevWidth = imgPrevWidth +10;
	imgTot = imgSliderDiv.find('a').size();
	var count = imgTot;
	impPreRealWdt = imgPrevWidth * imgTot / 1;    
	imgSliderDiv.css({
		'width': impPreRealWdt
	});
	var imgCount = 0;
	

	$('#right_large').click(function() {
		l=0;
		var leftUnit = -(imgTot-1)*600;
		if(aimgCount !=0){
			aimgCount = aimgCount -1;
		}
		l = Math.round(imgSliderDiv.position().left);  
		/*console.log(leftUnit);
		console.log(l);*/
		if (aimgCount ==0 || l==leftUnit){
			return ;
		}
		if(l - imgPrevWidth < -imgSliderDivWidth){
			/*l =imgSliderDivWidth - impPreRealWdt;*/
			l = l - imgPrevWidth;
		}else{
			l = l - imgPrevWidth;
		}
		imgSliderDiv.animate({ left: l
		}, 500);
	});

	
	$('#left_large').click(function() {
		var imgSliderDiv = $('.image-slider_1');
		var totImg =0;
		$('.image-slider_1').find('a').each(function(){
			totImg = totImg +1;
		});
		aimgCount = totImg;
		taoatlLeft = -totImg*600;
		var l = imgSliderDiv.position().left;
		if (l == taoatlLeft) {
			return;
		}
		l = (l + imgPrevWidth > 0) ? 0 : l + imgPrevWidth;

		imgSliderDiv.animate({left: l}, 500);
	});
}


function formSubmit(){
	if($('#code').val() == ""){
		$("#errorCodeMsg").html("Please enter the CAPTCHA");
		return false;
	}
	$.post("success", $('#getaqoute').serialize(),
			   function(data) {
			    	if(data == "error"){
			    		var msg = "The code you entered was invalid";
			    		$("#errorCodeMsg").html(msg);
			    		return false;
			    	}else{
                        location.replace("http://www.om-eg.com/success/");   
			    		$("#errorCodeMsg").html("");
			    	}
			   });
	return false;
}
