$(function (){  
	$('.iphoneShadow').animate({opacity:0.6},10);
	$('.downloadOptions').animate({opacity:0.6},10);
	
	// Switches between pictures in the iPhone
	function makeswitch(runCount,runCount1) {
		$('#switch' + runCount).hide().addClass('iimgOff').removeClass('iimgCurr');
	    $('#switch' + runCount1).addClass('iimgCurr').removeClass('iimgOff').fadeIn(300);
		$('iimgOff').hide(); // incase I wasn't clear before!!!! 
		return false;
	}
	
	// Define what picture to switch to for every switch made
	var runCount = 0;    
	function timerMethod() {
		/*if( $('#fl2').is(':visible') ) {
			clearInterval(timerId); // stop timer on rotation // This whole block is not relevent anymore, think about removing/updating.
		}*/
		runCount++;
		var runCount1 = runCount+1;

		if( runCount == 5 ) {
			runCount1 = 1;
		}
		if( runCount == 6 ) {
			runCount = 0;
		}
	    
		makeswitch(runCount,runCount1);
	
						//Sliding image effect:
						//$('#switch' + runCount).animate({marginLeft: '-455px'},500).removeClass('iimgOff').addClass('iimgCurr');
	}
	
	function refreshLoveNow(){
		$(this).animate({opacity: 0.5}, 500).animate({opacity: 1}, 500);
		var sUrl = 'http://adamtal.com/wp-content/themes/adamtal/refreshLove.php';
		$('#ilove').load(sUrl);
		$('#ilove').animate({opacity: 0.5}, 500).animate({opacity: 1}, 500);
	}
	
	$('#refreshLove').mouseover(refreshLoveNow);
	
	// Run the switch every Xms
	var timerId = setInterval(timerMethod, 8000);
	var timerLove = setInterval(refreshLoveNow, 5000);
	
	// Next Pic button
	$('#nextPic').click(function(){
		var aaa = $('.iimgCurr').attr('id');
		var bbb = aaa.substring(6,7);
		var ccc = parseInt(bbb);
		var runCount = ccc;
		var runCount1 = ccc+1;
		if( runCount == 5 ) {
			runCount1 = 1;
		}
		if( runCount == 6 ) {
			runCount = 1;
		}
		makeswitch(runCount,runCount1);
		return false;
	});
	
	// Previous Pic button
	$('#prevPic').click(function(){
		var aaa = $('.iimgCurr').attr('id');
		var bbb = aaa.substring(6,7);
		var ccc = parseInt(bbb);
		var runCount = ccc;
		var runCount1 = ccc-1;
		if( runCount == 1 ) {
			runCount1 = 5;
		}
		if( runCount == 6 ) {
			runCount = 1;
		}
		makeswitch(runCount,runCount1);
		return false;
	});
		
	// Define items to rotate (I rotate all images with the iPhone since I can't rotate the contatining div)
	var a = $("#iPhoneBodyImg").rotate(0); // iPhone body
	var b = $("#iimg1").rotate(0); // images inside iPhone...
	var c = $("#iimg2").rotate(0);
	var d = $("#iimg3").rotate(0);
	var e = $("#iimg4").rotate(0);
	var f = $("#iimg5").rotate(0);
		
	// Onmouseover, Trigger Rotation
	
	$(".flip2").mouseover(function(){
		$('.iimgOff').hide();
		$('.glare').hide();
		$(this).css('backgroundPosition', '-34px 0px');
		$('.flip1').css('backgroundPosition', '0px 0px');
		a[0].rotateAnimation(90);
		b[0].rotateAnimation(90);
		c[0].rotateAnimation(90);
		d[0].rotateAnimation(90);
		e[0].rotateAnimation(90);
		f[0].rotateAnimation(90);
		$('#iphoneShadowImg').animate({width: '100px', height: '17px', opacity: 0.0},400);
		return false;
	});

	// Onmouseout, Rotate it back
	$(".flip1").mouseover(function(){
		$('.iimgOff').hide();
		$(this).css('backgroundPosition', '0px -43px');
		$('.flip2').css('backgroundPosition', '-34px -43px');
		a[0].rotateAnimation(0);
		b[0].rotateAnimation(0);
		c[0].rotateAnimation(0);
		d[0].rotateAnimation(0);
		e[0].rotateAnimation(0);
		f[0].rotateAnimation(0);
		$('#iphoneShadowImg').animate({width: '599px', height: '17px', opacity: 0.6},1350);
		$('.glare').animate({opacity: 0},1200).animate({opacity: 1},100);
		setInterval(timerMethod, 8000); // Get the timer going again...
		return false;
	});
	
	$(".flip1").click(function(){
		return false;
	});
	$(".flip2").click(function(){
		return false;
	});
	
	$('#readOther').click(function(){
		$(this).animate({opacity: 0.5}, 500).animate({opacity: 1}, 500);
		$(this).text('Sorry, no more posts at the moment...');
		return false;
	})
	/*
	$('input#sTime').blur(function() {
		var sTime = $(this).val();
		var sDur = $(this).siblings('#length').val();
		var sUrl = 'http://www.shoham-dance.com/adm/timeTable/timeGen.php?sTime=' + sTime + '&sDur=' + sDur;
		//$(this).siblings('#eTime').text('');
		$(this).siblings('#eTime').load(sUrl);
		$(this).siblings('#eTime').animate({opacity: 0.5}, 500).animate({opacity: 1}, 500);
	});
	*/
});

$(window).load(
    function() {
        $('.iPhoneBody').fadeIn(800);	// Show iPhone only when all images are ready
    }
);

