(function(window) {

    function Site() {
		init(this);
    }
    
    // public methods
    
    // private methods    
    function transInWork() {
        $('#work .container figure').animate({opacity:1},500);
        $('#work .project-details').animate({opacity:1},500);
    }
    
    function validateEmail(email) { 
        var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        return re.test(email);
    } 
    
    function checkForms() {
        var nameInput = document.getElementById("id_name");
        var emailInput = document.getElementById("id_sender");
        var messageArea = document.getElementById("id_message");
        var isValidEmail;
        
        if(emailInput.value.length!=0) {
            isValidEmail = validateEmail(emailInput.value);
        }
        
        if(nameInput.value=="" || emailInput.value=="" || messageArea.value=="") {
            $('.errorlist').remove();
            if(nameInput.value=="") {
                $('input#id_name').before('<ul class="errorlist"><li>This field is required.</li></ul>');
            }
            if(emailInput.value=="") {
                $('input#id_sender').before('<ul class="errorlist"><li>This field is required.</li></ul>');
            } else if(!isValidEmail) {
                $('input#id_sender').before('<ul class="errorlist"><li>Enter a valid e-mail address.</li></ul>');
            }
            if(messageArea.value=="") {
                $('textarea#id_message').before('<ul class="errorlist"><li>This field is required.</li></ul>');
            }            
            return false;
            
        } else {
            if(!isValidEmail) {
                $('input#id_sender').before('<ul class="errorlist"><li>Enter a valid e-mail address.</li></ul>');
                return false;
            }
            return true;
        }
    }
    
    function init(context) {
    	$(document).ready(function() {
            
            $(window).load( transInWork );
            $('#contactForm').submit( checkForms );
		
            $('.extLink').click( function() {
                var targ = this.getAttribute("href");
                window.open( targ );
                return false;
            });

        	/* Rollovers
        	================================================== */	
        	$('a.portfolio-thumb').hover(
        		function(){
        			$(this).children('img.thumb-border').animate({backgroundColor:"#99BDCC"},250);
        		},
        		function(){
        			$(this).children('img.thumb-border').animate({backgroundColor:"#FFFFFF"},250);
        		}
        	);
        	
        	$('#navigation ul li a').hover(
        	   function(){
        	       $(this).animate({color:"#FFFFFF"},250);
        	   },
        	   function(){
        	       $(this).animate({color:"#4D4D4D"},250);
        	   }
        	);
        	
        	$('button').hover(
        	   function(){
        	       $(this).animate({color:"#111"},200);
        	       $(this).animate({backgroundColor:"#9E9E9E"},200);
        	   },
        	   function(){
        	       $(this).animate({color:"#DADADA"},100);
        	       $(this).animate({backgroundColor:"#161616"},100);
        	   }
        	);
        	
        	$('#work .view-project').hover(
        	   function(){
        	       $(this).animate({backgroundPositionX:"7px"},200);
        	       //$(this).animate({color:"#555555"},250);
        	   },
        	   function(){
        	       $(this).animate({backgroundPositionX:"0px"},200);
        	       //$(this).animate({color:"#005B7F"},200);
        	   }
        	);
        	
/*
        	$('#work a.detail.prev').hover(
        	   function(){
        	       $(this).animate({backgroundPositionX:"-3px"},200);
        	   },
        	   function(){
        	       $(this).animate({backgroundPositionX:"0px"},200);
        	   }
        	);
        	
        	$('#work a.detail.next').hover(
        	   function(){
        	       $(this).animate({backgroundPositionX:"51px"},200);
        	   },
        	   function(){
        	       $(this).animate({backgroundPositionX:"47px"},200);
        	   }
        	);
*/
        	
        	$('#work a.detail').hover(
        	   function(){
        	       $(this).animate({color:"#666"},200);
        	   },
        	   function(){
        	       $(this).animate({color:"#005B7F"},200);
        	   }
        	);
        	
             /* Resize handler
        	================================================== */	
        	$('img.royalImage').load( function()
        	{
        	   var imageWidth = $('img.royalImage').width();
        	   var imageHeight = $('img.royalImage').height();
        	   $('#work .main-view' ).animate({height:imageHeight,opacity:1},500);
        	   $('.royalSlider').css('height',imageHeight);	   
        	   var mySliderInstance = $("#mySlider").data("royalSlider");
        	   if(mySliderInstance) mySliderInstance.updateSliderSize();
        	});
        	
        	$(window).load( function()
        	{
        	   var imageWidth = $('img.royalImage').width();
        	   var imageHeight = $('img.royalImage').height();
        	   $('#work .main-view' ).animate({height:imageHeight,opacity:1},500);
        	   $('.royalSlider').css('height',imageHeight);
        	   var mySliderInstance = $("#mySlider").data("royalSlider");
        	   if(mySliderInstance) mySliderInstance.updateSliderSize();
        	});
        	
            $(window).resize( function () 
            { 
                var imageWidth = $('img.royalImage').width();
                var imageHeight = $('img.royalImage').height();
                $('#work .main-view').css( 'height',imageHeight );
                $('.royalSlider').css('height',imageHeight);
                var mySliderInstance = $("#mySlider").data("royalSlider");
                if(mySliderInstance) mySliderInstance.updateSliderSize();
            });
            
            
            /* Nav animated scroll
        	================================================== */
        	
            $('.scrollPage').click( function() 
            {
                var elementClicked = $(this).attr("href");
                var destination = $(elementClicked).offset().top;
                $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-50}, 750 );
                return false;
            });
            
        	/* Tabs Activiation
        	================================================== */
        	var tabs = $('ul.tabs'),
        	    tabsContent = $('ul.tabs-content');
        	
        	tabs.each(function(i) {
        		//Get all tabs
        		var tab = $(this).find('> li > a');
        		tab.click(function(e) {
        			
        			//Get Location of tab's content
        			var contentLocation = $(this).attr('href') + "Tab";
        			
        			//Let go if not a hashed one
        			if(contentLocation.charAt(0)=="#") {
        			
        				e.preventDefault();
        			
        				//Make Tab Active
        				tab.removeClass('active');
        				$(this).addClass('active');
        				
        				//Show Tab Content
        				$(contentLocation).show().siblings().hide();
        				
        			} 
        		});
        	});
        	
        	/* Royal Slider
        	================================================== */
        	
        	$("#mySlider").royalSlider({
            	preloadNearbyImages:true,               // Preloads two nearby images, if they have lazy loading enabled
                imageScaleMode:"none",                  // Scale mode of all images. Can be "fill", "fit" or "none"
                imageAlignCenter:false,		           // Aligns all images to slide center
                            
                keyboardNavEnabled:false,	            // Keyboard arrows navigation	
                        
                directionNavEnabled: true,              // Direction(arrow) navigation
                directionNavAutoHide: false,            // Direction(arrow) navigation auto hide on hover. 
                                                        // (On touch devices arrows are always shown)
                hideArrowOnLastSlide:true,              // Auto hide right arrow on last slide and left on first slide.
                                                        // Always true for touch devices.
                                                        
                slideTransitionType: "move",            // Slide transition type: Can be "fade" or "move".
                slideTransitionSpeed:500,               // Slide transition speed in ms (1s = 1000ms).
                slideTransitionEasing:"easeInOutSine",  // Easing type for slide transition. View supported easing types
                
                captionAnimationEnabled: true,          // Set to false if you want to remove all animations from captions  
                captionShowEffects:["fade","moveleft"], // Default array of show effects. 
                                                        // Types: 'fade', 'movetop', 'movebottom', 'moveleft', 'moveright'.
                                                        // e.g. ['fade', 'movetop'], ['fade'], ['movebottom']
                                                        
                captionMoveOffset:20,                   // Default distance for move effect in px
                captionShowSpeed:400,                   // Default caption show speed in ms
                captionShowEasing:"easeOutCubic",       // Default caption show easing
                captionShowDelay:200,                   // Default delay between captions on one slide show
                
                controlNavEnabled: true,                // Control navigation (bullets, thumbs)  enabled
                controlNavThumbs: false,                // Use thumbs for control navigation
                                                        // (use data-thumb="myThumb.jpg" attribute in html royalSlide div)
                controlNavThumbsNavigation:true,        // Enables navigation for thumbs
                controlNavThumbsSpeed:400,		    // Thumbnails navigation move speed (1000ms = 1s)
                controlNavThumbsEasing:"easeInOutSine", // Thumbnails navigation easing type
                
                slideshowEnabled: true,                // Autoslideshow enabled          
                slideshowDelay:5000,                    // Delay between slides in slideshow
                slideshowPauseOnHover: true,            // Pause slideshow on hover
                slideshowAutoStart:true,                // Auto init slideshow 
                
                lockAxis: true, 						// Drag navigation only on one axis at once
                
                welcomeScreenEnabled: false,            // Slider welcome(loading) screen enabled
                welcomeScreenShowSpeed:500,             // Welcome screen fade out speed
                
                minSlideOffset:20,                      // Minimum distance in pixels to show next slide while dragging    
                
                disableTranslate3d:false,               // Disables CSS3 transforms on touch devices
                
                removeCaptionsOpacityInIE8:false,       // If your animated captions with fade effect 
                					    				// have no background color, so turn this option on. 
                                                        // It's a fix for pixelated text bug in IE8 and lower. 
                                                        // Removes fade effect animation in ie8 and lower.
                                                        
                initSlideIndex: 0,			    // Start slide index (initing from 0).
                slideSpacing: 0,			    // Distance between slides in pixels.
                        
                blockLinksOnDrag: true,		    // Blocks all links when dragging.
                nonDraggableClassEnabled: false,        // Prevents dragging on all elements that have 'non-draggable' class.
                                                        // Works only inside slides. Turned off by default to improve performance.
                
                dragUsingMouse:true,					// Drag using mouse on devices with no touch support
                
                beforeSlideChange: function(){},        // Callback, triggers before slide transition
                afterSlideChange: function(){},         // Callback, triggers after slide transition	
                
                beforeLoadStart:function() {},	    // Callback, triggers before first image loading inits
                loadingComplete: function() {},         // Triggers after load complete and before welcome screen animation
                allComplete: function() {}	            // Callback, triggers after loading and welcome screen animation
            });  
        
        	
        });
    }
    window.Site = Site;
    
}(window));

//-----------------------------------------------

var site = new Site();

function hideURLbar() {
    window.scrollTo(0, 1);
}

var ua = navigator.userAgent.toLowerCase();

if(ua.indexOf("android") > -1 || ua.indexOf("iphone") > -1) {
	addEventListener("load", function() 
	{ 
		setTimeout(hideURLbar, 0); 
	}, false); 
}

