$(function(){
    /**
     * @author: < studio(at)cfstudio(dot)pl >
     * @support < kamil(at)cfstudio(dot)pl >
     * @copyright CFStudio 2011.
     */
	var isiPad = navigator.userAgent.match(/iPad/i) != null; 
	if(isiPad){
		$('#menu-trigger').css({'height':70});
	}
     
    var maxSize = $('#slider-movement').find('img').size();
    maxSize = 593*maxSize;
    $('.project-img').css({'width':maxSize});
    $('.project-scroll').jScrollPane();
    var fixFn = {
        navigation:function(){
            var height,parentHeight,fix;
            $('.top-message').each(function(){
                height = $(this).height();
                parentHeight = $(this).parent().height();
                fix = Math.round((parentHeight-height)/2);
                $(this).css({'margin-top':fix});
            });
        }
    }
    fixFn.navigation();
    var navigationFn = {
        elem:'',
        control:function(width){
            navigationFn.elem = $('#menu-trigger');
            if(width > 800){navigationFn.elem.fadeOut(500);}else{navigationFn.elem.fadeIn(500);}
        },
        manual:function(){
            var width;
            $('.n-3').resizable({
                'maxHeight':131,'minHeight':131,'minWidth':20,'maxWidth':1014,'handles':'e',
                resize:function(){
                    width = $('.n-3').width();
                    navigationFn.control(width);
                    width = width - 1014;
                    $('.navigation-wrapper').css({'left':width});
                },
                stop:function(){
                    width = $('.n-3').width();
                    if(width > 800){
                        navigationFn.control(width);
                        width = 1014;
                        $('.n-3').animate({
                            'width':1014
                        },500);
                        $('.navigation-wrapper').animate({'left':+width-1014},300);
                    }
                }
            });
        },
        clickable:function(){
            $('.ui-resizable-e').dblclick(function(){
                $('.navigation-wrapper').stop(true,false).animate({'left':-800},500);
                $('.n-3').stop(true,false).animate({
                    'width':20
                },500);
                $('#menu-trigger').fadeIn(600);
            });
            var width,nwidth = 1014,has;
            $('#menu-trigger').click(function(){
                width = $('.n-3').width();
                if(width < nwidth){
                    has = false;
                }else{
                    has = true;
                }
                if(!has){
                    $('.n-3').animate({
                        'width':1014
                    },500);
                    setTimeout(function(){
                        $('.navigation-wrapper').animate({
                            'left':0
                        });
                    },100);
                    $(this).fadeOut(600);
                }else{
                    $('.n-3').animate({
                        'width':20
                    },500);
                    $('.navigation-wrapper').animate({
                        'left':-800
                    },200);
                }
            });
        },
        menu:function(){
            $('.menu').each(function(){
                var has = $(this).hasClass('active');
                if(!has){
                    var inactive = '<div class="inactive">';
                    $(this).append(inactive);
                }
            });
            $('.menu').hover(function(){
                $(this).find('.inactive').stop(true,false).fadeTo(500,0);
            },function(){
                $(this).find('.inactive').stop(true,false).delay(100).fadeTo(700,1);
                
            });
        },
        ride:function(elem){
            var subelem,pos,height;
            $(elem).hover(function(){
                subelem = $(this).find('.sub-menu');
                height = subelem.height();
                pos = $(this).position();
                subelem.css({'left':pos.left,'top':pos.top,'z-index':100,'opacity':1,'margin-top':(131-height)/2});
                subelem.stop(true,false).fadeTo(200,1);
            });
            $(elem).mouseleave(function(){
                $(this).find('.sub-menu').fadeOut(50);
            });
        }
    }
    navigationFn.manual();
    navigationFn.clickable();
    navigationFn.menu();
    navigationFn.ride('.menu');
    var gfxFn = {
        fading:function(elem,find){
            $(elem).find(find).hover(function(){
                $(this).stop(true,false).fadeTo(400,0.85);
            },function(){
                $(this).stop(true,false).fadeTo(400,1);
            });
        },
        brief:function(elem,toToggle){
        	$(elem).click(function(){
        		$(toToggle).slideToggle(400);
        	});
        }
    }
    gfxFn.fading('.cont-body','img');
    //gfxFn.brief('#brief-wrapper','#hidden-wrapper');
    var extFn = {
    	blank:function(elem){
    		$(elem).each(function(){
    			$(this).attr('target','_blank');
    		});
    	}
    }
    extFn.blank('.external');
    
    var nimg = {
    	load:function(elem){
    		var value;
    		$(elem).each(function(){
    			$(elem).onImagesLoaded(function(_img){
					$(_img).fadeIn(3000).next().fadeOut(300);
				});
    		});
    	}
  	}
  	nimg.load('.portfolio-nimage');
  	$("#facebook-widget").hover(function(){$(this).stop(true,false).animate({left:"0"},700);},function(){$(this).stop(true,false).animate({left:"-300"},500);},300);
	$('.rollover').hover(function(){$(this).css({'background-position':'0px -25px'});},function(){$(this).css({'background-position':'0px 0px'});},300);
	
	$('#cf-contact').submit(function(evt){
		evt.preventDefault();
		var date = $(this).serialize();
		$.ajax({
		   type: "POST",
		   url: "http://www.cfstudio.pl/contact.php",
		   data: date,
		   success: function(msg){
		     showShadow();
		     $('.report').html(msg);
		     var is = false;
		     var si = 0;
		     is = msg.split('h2');
		     si = is.length;
		     if(si > 1){
		     	$('#cf-contact').find('input[type=text]').val('');
		     	$('#cf-contact').find('textarea').val('');
		     }
		   }
		 });
	});
	$('#shadow').click(function(evt){
		evt.preventDefault();
		hideShadow();
	});
	$('.closeForm').click(function(evt){
		evt.preventDefault();
		hideShadow();
	});
	function showShadow(){
		var dH = $(document).height();
		var dW = $(document).width();
		var fP = $('#cf-contact').offset();
		$('#shadow').css({'width':dW,'height':dH,'opacity':0.7}).fadeIn(300);
		$('#contact-report').css({'top':fP.top+100,'left':fP.left+250}).delay(100).fadeIn(300);
		
	}
	function hideShadow(){
		$('#contact-report').fadeOut(300);
		$('#shadow').delay(100).fadeOut(300);
	}
	
	var $container = $('#portfolio-projects');
	var max = $container.find('.cont-body').size();
	/* Preload images in order to elements. by cfstudio.pl 2011. */
	function preloadInOrder(num){
		var $elem = $('#portfolio-projects .cont-body').eq(num);
		var src = $elem.attr('rel');
		var img = new Image(284);
		img.src = src;
		$(img).load(function(){
			$elem.find('.loader').fadeOut(300).remove();
			$elem.find('.project-maintance').append(this).css({'display':'none'}).fadeIn(300);
			if(num+1 < max){
				preloadInOrder(num+1);
			}
		});
	}
	preloadInOrder(0);
	
});
