function windowWidth() {
	var myWidth = 0;
	var myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
	} else if( document.documentElement && document.documentElement.clientWidth ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	} else if( document.body && document.body.clientWidth ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
	}
  
	return myWidth;
}

$(document).ready(function () {
	
	var currentIndex = 0;

	$.easing.exponential = function (x, t, b, c, d, s) {
		t /= d/2;
		if (t < 1) return c/2 * Math.pow( 2, 10 * (t - 1) ) + b;
		t--;
		return c/2 * ( -Math.pow( 2, -10 * t) + 2 ) + b;
	};
	
	$.easing.outExpo = function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	}

	$.easing.quadratic = function (x, t, b, c, d, s) {
		t /= d/2;
		if (t < 1) return c/2*t*t*t*t + b;
		t -= 2;
		return -c/2 * (t*t*t*t - 2) + b;
	};

	$.easing.sin = function (x, t, b, c, d, s) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	};
	
	var totalPic = $('.frame').size();
	var currentPic = 0;
	var increment = 100/totalPic;
	var currentValue;
	var newPic;
	var newMousePic;
	var repositioned = false;
	var minimized = true;
	var largeTimer;
	var mediumTimer;
	
	$('#pictureViewer').serialScroll({
		axis: 'x',
		duration: 800,
		items:'.frame', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		stop: true,
		lock: false,
		offset: -($('#landscape').width()/2),
		interval: 9000,
		easing: 'outExpo',
		force: true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
		constant: false,
		onBefore:function( e, elem, $pane, $items, pos ){
			$('.scrollArea').css({ 'left':'-2000px','width':'4000px' });
			clearTimeout(largeTimer);
			clearTimeout(mediumTimer);
			largeTimer = setTimeout(function() {
				$('.scrollArea').css({ 'left':'-600px','width':'1200px' });
				mediumTimer = setTimeout(function() {
					$('.scrollArea').css({ 'left':'-450px','width':'900px' });
				}, 200);
			}, 400);
				
/*			if(repositioned == false) {
				$.scrollTo('#containerLarge',400);
				repositioned = true;
			} */
//			console.log("Width: "+windowWidth());
			this.offset = -($('#landscape').width()/2);
//			console.log(this.offset);
			newPic = pos;
			currentValue = $('#slider').slider('option','value');
//			console.log(currentValue);
			if(currentValue == 0) {
				currentPic = 0;
			}
			else if(currentValue == 100) {
				newPic = totalPic - 1;
			}
			else {
				currentPic = Math.floor(currentValue / increment);
//				console.log(newPic);
			}
			if(newPic != currentPic) {
				$('#slider').slider( 'value', newPic*increment);
				currentPic = newPic;
//				console.log($('#slider').slider( 'value'));
			}
		},
		onAfter:function(pos) {
		}
			
	});
	
    $('.scrollArea').bind('mousewheel', function(event, delta) {
			vel = Math.abs(delta);
			difference = vel*.3;
			if(difference < 1 && difference > 0) {
				difference = 1;
			}
			if(delta > 0) {
				newPic = Math.round(currentPic - difference);
			}
			else {
				newPic = Math.round(currentPic + difference);
//				console.log(newPic);
			}
			if(newPic >= totalPic) {
				newPic = totalPic - 1;
//				console.log(newPic);
			}
			else if(newPic < 0) {
				newPic = 0;
//				console.log("zero: "+newPic);
			}
			if(newPic != currentPic) {
				$('#pictureViewer').trigger( 'goto', [ newPic ] );
				currentPic = newPic;
			}
			else newPic = currentPic;
			return false;
    });
    $('#containerLarge .picture').bind('mousewheel', function(event, delta) {
			vel = Math.abs(delta);
			difference = vel*.3;
			if(difference < 1 && difference > 0) {
				difference = 1;
			}
			if(delta > 0) {
				newPic = Math.round(currentPic - difference);
			}
			else {
				newPic = Math.round(currentPic + difference);
//				console.log(newPic);
			}
			if(newPic >= totalPic) {
				newPic = totalPic - 1;
//				console.log(newPic);
			}
			else if(newPic < 0) {
				newPic = 0;
//				console.log("zero: "+newPic);
			}
			if(newPic != currentPic) {
				$('#pictureViewer').trigger( 'goto', [ newPic ] );
				currentPic = newPic;
			}
			else newPic = currentPic;
			return false;
    });
	

	if( ! $('#checkSlideshow').attr('checked') ) {
		$('#pictureViewer').trigger( 'stop' );
	}	
	$('#checkSlideshow').change(function(value) {
		if( ! $(this).attr('checked') ) {
			$('#pictureViewer').trigger( 'stop' );
		}
		else  $('#pictureViewer').trigger( 'start' );
	});
	

	if( ! $('#checkMouseScroll').attr('checked') ) {
		var mouseScroll = false;
	}	
	else var mouseScroll = true;
	$('#checkMouseScroll').change(function(value) {
		if( ! $(this).attr('checked') ) {
			mouseScroll = false;
		}
		else  mouseScroll = true;;
	});	
    
    var currentMouseX;
    var currentMouseY;
	$('#pictureViewer').mousemove(function(e){
		if(mouseScroll) {
			currentMouseX = e.pageX;
			mouseIncrement = $('#pictureViewer').width() / totalPic;
			
			if(currentMouseX == 0) {
				newMousePic = 0;
			}
			else if(currentMouseX == 100) {
				newMousePic = totalPic - 1;
			}
			else {
				newMousePic = Math.floor(currentMouseX / mouseIncrement);
			}
			if(newMousePic != currentPic) {
				$('#pictureViewer').trigger( 'goto', [ newMousePic ] );
				currentPic = newMousePic;
			}
		}
	});
	$('#scrollArea').mousemove(function(e){
		if(mouseScroll) {
			offset = $(this).offset();
			currentMouseX = e.pageX;
			mouseIncrement = $('#pictureViewer').width() / totalPic;
			
			if(currentMouseX == 0) {
				newMousePic = 0;
			}
			else if(currentMouseX == 100) {
				newMousePic = totalPic - 1;
			}
			else {
				newMousePic = Math.floor(currentMouseX / mouseIncrement);
			}
			if(newMousePic != currentPic) {
				$('#pictureViewer').trigger( 'goto', [ newMousePic ] );
				currentPic = newMousePic;
			}
		}
	});
	
	$("#slider").slider({
		slide: function(event, ui) {
			currentValue = $('#slider').slider('option','value');
//			console.log(currentValue);
			if(currentValue == 0) {
				newPic = 0;
			}
			else if(currentValue == 100) {
				newPic = totalPic - 1;
			}
			else {
				newPic = Math.floor(currentValue / increment);
//				console.log(newPic);
			}
			if(newPic != currentPic) {
				$('#pictureViewer').trigger( 'goto', [ newPic ] );
				currentPic = newPic;
			}			
		}
	});
	
	$(window).keydown(function(event) {
//		console.log(event.keyCode);
		if(event.keyCode == '39') $('#pictureViewer').trigger( 'next' );
		if(event.keyCode == '37') {
//			console.log('back');
			$('#pictureViewer').trigger( 'prev' );
		}
	});
	
	
	$('a.leftArrow').hover(function() {
		$(this).animate({
			left: '-111'
		},100);
	},function() {
		$(this).animate({
			left: '-95'
		},100);
	});	
	$('a.leftArrow').click(function() {
		$('#pictureViewer').trigger( 'prev' );
		return false;
	});
	
	$('a.rightArrow').hover(function() {
		$(this).animate({
			right: '-111'
		},100);
	},function() {
		$(this).animate({
			right: '-95'
		},100);
	});
	$('a.rightArrow').click(function() {
		$('#pictureViewer').trigger( 'next' );
		return false;
	});
	
	
	$('a.pictureLink').fancybox({ 
		overlayOpacity: 0.8,
		overlayColor: '#000',
		onStart: function() {
//			console.log($(this).fancybox('pos'));
		}
	});
	
	$('a.pictureLink').bind('next', function(){
//		console.log('test');
	});
	
	$('#contactLink,#minMax').click(function() {
		if(minimized) {
			minimized = false;
			$('#minMax').css('background','url(/images/minimize-maximize.png) no-repeat right top');
			$('#footer').animate({
				height: '627px'
			}, 1100,'exponential');
			$.scrollTo('5000px',1100,{easing:'exponential'});
		}
		else {
			minimized = true;
			$('#minMax').css('background','url(/images/minimize-maximize.png) no-repeat left top');
			$('#footer').animate({
				height: '65px'
			}, 1100,'exponential');
		}
	});
	
    $('input').map(function() {
		if($(this).attr('value') != $(this).attr('alt')) {
			$(this).css('color','#7a7a7a');
		}
    });
	$('input').focus(function() {
		if($(this).attr('value') == $(this).attr('alt')) {
			$(this).attr('value','');
			$(this).css('color','#7a7a7a');
		}
	});
	$('input').blur(function() {
		if($(this).attr('value') == '') {
			$(this).attr('value',$(this).attr('alt'));
			$(this).css('color','#bababa');
		}
	});
	
    $('textarea').map(function() {
		if($(this).val() != 'Enter your message') {
			$(this).css('color','#7a7a7a');
		}
    });
	$('textarea').focus(function() {
		if($(this).val() == 'Enter your message') {
			$(this).val('');
			$(this).css('color','#7a7a7a');
		}
	});
	$('textarea').blur(function() {
		if($(this).val() == '') {
			$(this).val('Enter your message');
			$(this).css('color','#bababa');
		}
	});
	
	$(':checkbox').iphoneStyle({
	  checkedLabel: 'YES',
	  uncheckedLabel: 'NO'
	});

	
	
	
	// a custom method making the default value for companyurl ("http://") invalid, without displaying the "invalid url" message
	jQuery.validator.addMethod("defaultInvalid", function(value, element) {
		return value != element.defaultValue;
	}, "");

	jQuery.validator.messages.required = "";
	$('#contactRyan').validate({submitHandler: function() {			
			$email = $('#formEmail').val();
			$name = $('#formName').val();
			$subject = "New message from " + $name;
			$msg = escape($('#message').val());
			$phone = $('#formPhone').val();
			
			var datastr ='name=' + $name + '&email=' + $email + '&subject=' + $subject + '&msg=' + $msg + '&phone=' + $phone;
//			$("#response").css("display", "block");
			$("#response").html("Sending message .... ");
			$("#response").fadeIn(500);
			setTimeout("send('"+datastr+"')",2000);
		}
	});
	
});

function send(datastr){
	$.ajax({	
		type: "POST",
		url: "/scripts/mail.php",
		data: datastr,
		cache: false,
		success: function(html){
			$("#response").fadeIn("slow");
			$("#response").html(html);
			setTimeout('$("#response").fadeOut("slow")',4000);
		}
	});
}
