var cc = 0;
var cl;
var ndis = false;
var pdis = true;
var anim = false; 

$(document).ready(function () {

if(navigator.platform != 'iPhone'){
	
// dim elements
$('.op').css('opacity','0.25');

$('section#nav').css('opacity','0.75');


$('.op, section#nav').mouseenter(function(){
		$(this).stop().animate({'opacity': '1'}, 300);
});


$('.op').mouseleave(function(){
		$(this).stop().animate({'opacity': '0.25'}, 300);
});


}

$('section#nav').mouseleave(function(){
		$(this).stop().animate({'opacity': '0.75'}, 300);
});

// project click-through
$('.project').click(function(){
	wl = $(this).attr('title');
	window.location = wl;
});


// page images

$('#portfolio').maximage({
        cycleOptions: {
            fx:'fade',
            speed: 800,
            timeout: 8000,
            prev: '#prev-btn',
            next: '#next-btn',
			pager:  '#th', 
    		pagerAnchorBuilder: function(idx, slide) {
		
			img = jQuery.Slides[idx].url;
			
        	return '<a href="#"><img src="' + img + '" width="50" height="50" /></a>'; 
    } 
        },
        onFirstImageLoaded: function(){
            jQuery('#cycle-loader').hide();
            jQuery('#maximage').fadeIn('fast');
			// content fades

$('.panel').fadeIn(400);

if($('.award').length>0){
$('.award').each(function(i){
		$(this).delay(i*100).fadeIn(1500);
	});
}

if($('.project').length>0){
$('.project').each(function(i){
		$(this).delay(i*100).fadeIn(1500);
	});
}
        }
});

$('#pause-play').toggle(function(){
		$('#portfolio').cycle('pause'); 
		$('#pla').show();
		$('#pau').hide();
	}, function(){
		$('#portfolio').cycle('resume'); 
		$('#pau').show();
		$('#pla').hide();
	
	});



$('#prev-btn img').rotate({duration:1, animateTo:180});

// add arrow to calls
$('.featured a, .call a').append('<div class="ca"></div>');

$('.featured a, .call a').hover(function(){
	$(this).find('.ca').stop().animate({ 'right': '10px' }, 200);
}, function(){
	$(this).find('.ca').stop().animate({ 'right': '15px' }, 200);
});

$('input[type="submit"]').hover(function(){
	$(this).stop().animate({ 'background-position': '91%' }, 150);
}, function(){
	$(this).stop().animate({ 'background-position': '90%' }, 150);
});

// tooltips
$('.award-icon img, #fs').tipsy({ gravity: 'e' });


// show pictures only

$('#fs').toggle(function(){
	$('section, #acon, .panel').hide();
	$('#toast').show();
	$('.tipsy').hide();
	$(this).attr('title','Show content');
}, function(){
	$('#toast').hide();
	$('section, #acon, .panel').show();
	$('.tipsy').hide();
	$(this).attr('title','Hide content');	
});

// register form
if($("#regform").length > 0){
$("#regform").validate({
	
	messages: {
     fullname: "Please enter your name <div class='erar'></div>",
	postcode: "Please enter your post code <div class='erar'></div>",
	enquiry: "Please enter an enquiry <div class='erar'></div>",
	//phone: "Please enter your phone number <div class='erar'></div>",
     email: {
       required: "Please enter an email address <div class='erar'></div>",
       email: "Please enter a valid email address <div class='erar'></div>"
     }
   }

});

$("#regform").submit(function(){
var str = $(this).serialize();
   $.ajax({
   type: "POST",
   url: "http://sammutdevelopments.com.au/php/contactForm.php",
   data: str,
   success: function(amsg){
$("#noter").ajaxComplete(function(event, request, settings){
if(amsg == 'OK') // Message Sent? Show the 'Thank You' message and hide the form
{
$('#regform').hide();
$('#confirmed').fadeIn(400);
}
else
{
$('input[type=text], textarea').blur();
result = amsg;
}
$(this).html(result);
});
}
});
return false;
});	
}


// show panel
$('.controls ').toggle(function(){
	$('.arr',this).rotate({duration:200, animateTo:180});
	$(this).siblings('.content').slideUp(300, function(){
	});
	$(this).siblings('.content').find('p, img').fadeOut(400);
}, function(){
	$(this).siblings('.content').find('p, img').show();
	$('.arr',this).rotate({duration:200, animateTo:0});
	$(this).siblings('.content').slideDown(300, function(){
	});
	
});




$('body').bind('mousewheel', function(event, delta) {

	if($('.award').length>0){
		if(delta < 0){
			
			if(!ndis && !anim){
	anim = true;
	$('#award-inner').animate({  top: '-=140px' }, 600, 'easeOutQuint', function(){ anim = false; });
	cc++
	if(cc >= cl){
		ndis = true;
		$('#next-a img').addClass('dis');
	}
	if(cc >= 0){
		pdis = false;
		$('#prev-a img').removeClass('dis');
	}
	}		
					
		}else{
			
		if(!pdis && !anim){
	anim = true;
	$('#award-inner').animate({  top: '+=140px' }, 600, 'easeOutQuint', function(){ anim = false; });
	cc--
	if(cc <= 0){
	pdis = true;
	$('#prev-a img').addClass('dis');
	}
	if(cc >= 0){
	  ndis = false;
	  $('#next-a img').removeClass('dis');
	}
	}			
		}
		
	}

});


// show next awards

$('#next-a').click(function(){
	if(!ndis){
	$('#award-inner').animate({  top: '-=140px' }, 600, 'easeOutQuint');
	cc++
	if(cc >= cl){
		ndis = true;
		$('#next-a img').addClass('dis');
	}
	if(cc > 0){
		pdis = false;
		$('#prev-a img').removeClass('dis');
	}
	}
});


$(window).keyup(function(event) {
	if (event.which == 40) {
   		if(!ndis){
	$('#award-inner').animate({  top: '-=140px' }, 600, 'easeOutQuint');
	cc++
	if(cc >= cl){
		ndis = true;
		$('#next-a img').addClass('dis');
	}
	if(cc >= 0){
		pdis = false;
		$('#prev-a img').removeClass('dis');
	}
	}
  	} 
	
	if (event.which == 38) {
   		if(!pdis){
	$('#award-inner').animate({  top: '+=140px' }, 600, 'easeOutQuint');
	cc--
	if(cc <= 0){
	pdis = true;
	$('#prev-a img').addClass('dis');
	}
	if(cc >= 0){
	  ndis = false;
	  $('#next-a img').removeClass('dis');
	}
	}
  	} 

});

$('#prev-a').click(function(){
	if(!pdis){
	$('#award-inner').animate({  top: '+=140px' }, 600, 'easeOutQuint');
	cc--
	if(cc <= 0){
	pdis = true;
	$('#prev-a img').addClass('dis');
	}
	if(cc >= 0){
	  ndis = false;
	  $('#next-a img').removeClass('dis');
	}
	}
});


// placeholders
if($('input[type=text]').length>0){
$('input[type=text], textarea').placeHoldize();
}

if(navigator.platform == 'iPhone'){
	$('#controls').hide();
	cl = 20;
}else{

 wh =  $(window).height();
	
	if(wh > 220){
	$('#award-wrap').height('140px');
	cl = 7;
	}
	
	if(wh > 360){
	$('#award-wrap').height('280px');
		cl = 6;
	}
	
	if(wh > 500){
	$('#award-wrap').height('420px');
		cl = 5;
	}
	
	if(wh > 640){
	$('#award-wrap').height('560px');
	cl = 4;
	}
	
	if(wh > 780){
	$('#award-wrap').height('700px');
		cl = 3;
	}
	
	if(wh > 920){
	$('#award-wrap').height('840px');
	cl = 2;
	}
	
	if(wh > 1060){
	$('#award-wrap').height('980px');
	cl = 1;
	}
	
	if(wh > 1200){
	$('#award-wrap').height('1120px');
	$('#acon').hide();
		cl = 0;
	}
	
}

});

$(window).resize(function() { 

cc = 0;
ndis = false;
pdis = true;


	delay(function(){
	  $('#award-inner').animate({ 'top': '0px'}, 500);
	  cc = 0;
	  ndis = false;
	  pdis = true;
	  $('#prev-a img').addClass('dis');
	  $('#next-a img').removeClass('dis');

    }, 500);


    wh =  $(window).height();
	
	if(wh > 220){
	$('#award-wrap').height('140px');
	cl = 7;
	}
	
	if(wh > 360){
	$('#award-wrap').height('280px');
	cl = 6;
	}
	
	if(wh > 500){
	$('#award-wrap').height('420px');
	cl = 5;
	}
	
	if(wh > 640){
	$('#award-wrap').height('560px');
	cl = 4;
	}
	
	if(wh > 780){
	$('#award-wrap').height('700px');
	cl = 3;
	}
	
	if(wh > 920){
	$('#award-wrap').height('840px');
	cl = 2;
	}
	
	if(wh > 1060){
	$('#award-wrap').height('980px');
	cl = 1;
	}
	
	if(wh > 1200){
	$('#award-wrap').height('1120px');
	$('#acon').hide();
	cl = 0;
	}else{
	$('#acon').show();
	}
	
});

var delay = (function(){
  var timer = 0;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  };
})();

