$(document).ready(function(){
     var zindex = 1;
	 // Executed once all the page elements are loaded

	var preventClick=false;
	
	$("div.pic a").bind("click",function(e){
		/* This function stops the drag from firing a click event and showing the lightbox */
		if(preventClick)
		{
			e.stopImmediatePropagation();
			e.preventDefault();
		}
	});

	$("div.pic").draggable({
		/* Converting the images into draggable objects */
		containment: 'parent',
		start: function(e,ui){
			/* This will stop clicks from occuring while dragging */
			preventClick=true;
			zindex++;
			var cssObj = {'z-index' : zindex};
			$(this).css(cssObj);
		},
		
		stop: function(e, ui) {
			/* Wait for 250 milliseconds before re-enabling the clicks */
			setTimeout(function(){ preventClick=false; }, 250);
		}
	});
	 
	 
	 $('div.pic').each(function(){
			 var rot = Math.random()*40-40+'deg';
			 var left = Math.random()*400+'px';
			 var top = Math.random()*600+'px';
			 
			 $(this).css('-webkit-transform' , 'rotate('+rot+')');
			 $(this).css('-moz-transform' , 'rotate('+rot+')');
			 $(this).css('top' , left);
			 $(this).css('left' , top);
			 
			 $(this).mouseup(function(){
				 zindex++;
				 $(this).css('z-index' , zindex);
			 });
	     });
		 
	$('div.pic').dblclick(function(){
			$(this).css('-webkit-transform' , 'rotate(0)');
			$(this).css('-moz-transform' , 'rotate(0)');
	});
	
	// fancybox script
	$('a.fancybox').fancybox({
		'titlePosition'	: 'outside',
		'overlayShow'	: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	
	$(function() {
        var formEl = $('#main_contact_form');
        var validator = formEl.validate();
        formEl.submit(function() {
            // Remove label texts
            $('#main_contact_form .input input, #main_contact_form textarea').each(function() {
                var el = $(this);
                if (el.attr('rel') == el.val()) {
                    el.val('');
                }
            });
            
            if (!formEl.valid()) {
                validator.showErrors();
                return false;
            }
            
            $(this).ajaxSubmit({
                success: function() {
                    $('#form_progress').hide();
                    $('#form_thanks').show();
                }
            });
            formEl.hide();
            $('#footer_cform > h3').hide();
            $('#form_progress').show();
            return false;
        });
        
        $('#main_contact_form .input input, #main_contact_form textarea').each(function() {
            var el = $(this);
            var label = el.val();
            el.attr('rel', label);
        }).focus(function() {
            var el = $(this);
            if (el.attr('rel') == el.val()) {
                el.val('');
            }
        }).blur(function() {
            var el = $(this);
            if (el.val() == '') {
                var label = el.attr('rel');
                el.val(label);
            }
        });
    });
	
	
	//loader
	
	$('ul.menu li a').click( function(ev){
    ev.preventDefault(); // suppress natural click
    var idx = this.id.split('-')[1]; // grab the link "number"
    $('#footer div[id=c-'+idx+']') // find respective div
        .show() // show it
        //.siblings() // get its siblings
        //.hide(); // and hide them
    });
							   
});
