/* scriptoids and magic
 * kieranalington at gmail dot com
 * 2010
 */
$(document).ready(function(){
	
	initFonts();
	initForms();
        initOverlay();
        relatedWork();
	
});

function initFonts(){
	
		Cufon.replace('h1');
		Cufon.replace('h2');
		Cufon.replace('h3');
		Cufon.replace('span');
		Cufon.replace('label');

	}


function initForms() {
	
	$('.left form').validate();
	
	$('input.date_picker').date_input();

	$('.right input[type="text"]').focus(function(){
	
		var thisVal  = $(this).attr('value');
		var thisName = $(this).attr('name');
		if (thisVal == thisName) {
			$(this).attr('value', '');
		}
										   
	});

	$('.right input[type="text"]').blur(function(){
	
		var thisVal  = $(this).attr('value');
		var thisName = $(this).attr('name');
		if (thisVal == "") {
			$(this).attr('value', thisName);
		}
										   
	});
	
	}

function initOverlay(){

    $('.portfolio a').click(function(){

        var thisParent = $(this).parent('p').parent('.portfolio');
        var thisId     = thisParent.attr('name');

        loadContent('/ajax/portfolio.php?id='+thisId);

    });

    $('.close').live('click',function(){

        $('.overlayContent').fadeOut(function(){


            $('.overlayBg').fadeOut();

            $('.overlayContent').children('.content').html('');

        });

    });

}

function loadContent(data){

    window.scrollTo(0,0);

    var winWidth = $(window).width();
    var winHeight = $(window).height();

    $('.overlayBg').css({
        position:'fixed',
        width: winWidth,
        height: winHeight,
        zIndex:995
    })

    var halfWidth = (winWidth/2)-496;
    $('.overlayContent').css({
        position:'absolute',
        left: halfWidth,
        top: 100,
        zIndex:999
    })

    
    $('.overlayBg').fadeTo('fast',0.8, function(){

        $('.overlayContent').fadeIn('fast');

    });

    $('.overlayContent').children('.content').load(data,function(){

       initFonts();
       var overlayContentHeight = $('.overlayContent').height();

       $('body').height(overlayContentHeight+200);
       portfolioImages();
    });

   
}

function portfolioImages(){

    $('.portfolioImages img').live('mouseover',function(){
      
       var newImage = $(this).attr('src');
       var newTitle = $(this).text();
       $('.portfolioImages').children('.main').children('img').attr('src', newImage);
     
    });

}


function relatedWork(){

    $('.portfolioImages').live('mouseout',function(){

         $('.relatedTitle').text('Related Work');

       initFonts();

    });

    $('.portfolioRelated img').live('mouseover',function(){

       var thisTitle = $(this).attr('title');
       
       $('.relatedTitle').text(thisTitle);
       
       initFonts();

    });

    $('.portfolioRelated img').live('click',function(){

        $('.portfolioImages').fadeTo('slow',0.1);
        $('.portfolioInner').fadeTo('slow',0.1);
        $(this).attr('src', '/images/generic/loading.gif');

        var that = this;

       $(this).css({
            margin:4
        })

        setTimeout(function(){
            thisId =  $(that).attr('name');
            loadContent('/ajax/portfolio.php?id='+thisId);

        },2000);
        
       
        

    });
     
}
