$(document).ready(function() {
  
  $('.slideshow img').hide();
  $('.slideshow').addClass('loader');
  
  $('.slideshow img').load(function(){
        $(this).show();
        $('slideshow').removeClass('loader');
  });

	$(function(){
		setInterval("slideshow()", 4000);
	});
	
	//LOADING POPUP  
  //Click the button event!  
  $("#popupContactOpen").click(function(){  
    //centering with css  
    centerPopup();  
    //load popup  
    loadPopup();  
  });
  
  //CLOSING POPUP  
  //Click the x event!  
  $("#popupContactClose").click(function(){  
  disablePopup();  
  });  
  //Click out event!  
  $("#backgroundPopup").click(function(){  
  disablePopup();  
  });
  
  //Validate Contact Form
  
  $("#contactform").validate();
	
	$("#contactform").submit(function(){
        if($("#contactform").valid()){
            var str = $(this).serialize();

             $.ajax({
               type: "POST",
               url: "http://minwye.com/controls/mailer.php",
               data: str,
               success: function(msg){
                  $("#note").ajaxComplete(function(event, request, settings){
                    if(msg == 'OK'){
                        result = '<div class="notification_ok">Thank you for your message.<br /> I will be in contact with you shortly!</div>';
                        $("#contactformfields").hide();
                        $(".popupContact").animate({
                          height: 100
                        });
                    }else{
                        result = "There was a problem with your submission, please contact me directly at <a href=\"mailto:minwye@gmail.com\">minwye@gmail.com</a>";
                    }
                    $(this).html(result);
                  });
               }
             });
            return false;
        }
    });
});

function slideshow(){
	var $active = $('.slideshow img.active');
	
	if($active.length == 0) $active = $('.slideshow img:last');
	
	var $next = $active.next().length?$active.next():$('.slideshow img:first');
	
	$active.addClass('last-active');
	
	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 2000, function(){
			$active.removeClass('active last-active');
		});
	
}

//0 means disabled; 1 means enabled;  
var popupStatus = 0;  

//loading popup with jQuery magic!  
function loadPopup(){  
//loads popup only if it is disabled  
if(popupStatus==0){  
$("#backgroundPopup").css({  
"opacity": "0.7"  
});  
$("#backgroundPopup").fadeIn("fast");  
$(".popupContact").fadeIn("fast");  
popupStatus = 1;  
}  
}

//disabling popup with jQuery magic!  
function disablePopup(){  
//disables popup only if it is enabled  
if(popupStatus==1){  
$("#backgroundPopup").fadeOut("fast");  
$(".popupContact").fadeOut("fast");  
popupStatus = 0;  
}  
}

//centering popup  
function centerPopup(){  
//request data for centering  
var windowWidth = document.documentElement.clientWidth;  
var windowHeight = document.documentElement.clientHeight;  
var popupHeight = $(".popupContact").height();  
var popupWidth = $(".popupContact").width();  
//centering  
$(".popupContact").css({  
"position": "absolute",  
"top": windowHeight/2-popupHeight/2,  
"left": windowWidth/2-popupWidth/2  
});  
//only need force for IE6  
  
$("#backgroundPopup").css({  
"height": windowHeight  
});  
  
}  
