//emulate jquery live to preserve jQuery.live() call
if( typeof jQuery.fn.live == 'undefined' ) {
    jQuery.fn.live = function( types, data, fn ) {
        jQuery( this.context ).on( types, this.selector, data, fn );
        return this;
    };
}

jQuery(document).ready(function($){

	$('body').removeClass('no_js').addClass('yes_js'); 
    
    
    $('#nav li, ul.sub-menu, ul.children').each(function(){
        n = $('ul.sub-menu:not(ul.sub-menu li > ul.sub-menu), ul.children:not(ul.children li > ul.children)', this).length;
        
        if(n) $(this).addClass('parent');
    });
        
    $('#nav ul > li').hover(
        function()
        {
            $('ul.sub-menu:not(ul.sub-menu li > ul.sub-menu), ul.children:not(ul.children li > ul.children)', this).stop(true, true).fadeIn(300);    
        },
    
        function()
        {
            $('ul.sub-menu:not(ul.sub-menu li > ul.sub-menu), ul.children:not(ul.children li > ul.children)', this).fadeOut(300);    
        }
    );              
    
    $('#nav ul > li').each(function(){
        if( $('ul', this).length > 0 )
            $(this).children('a').append('<span class="sf-sub-indicator"> &raquo;</span>')
    });  
    
    
    
    $('#nav li:not(.megamenu) ul.sub-menu li, #nav li:not(.megamenu) ul.children li').hover(
        function()
        {
            var options;
            
            winWidth = $(document).width();
            
            subMenuWidth = $(this).parent().outerWidth();
            space = $(this).offset().left + subMenuWidth * 2;
            
            if(space < winWidth) options = {left:subMenuWidth-20};
            else options = {left:subMenuWidth*-1};
            
            $('ul.sub-menu, ul.children', this).hide().css(options).stop(true, true).fadeIn(300);
        },
    
        function()
        {
            $('ul.sub-menu, ul.children', this).fadeOut(300);
        }
    ); 
    
    
    /* megamenu check position 
    $('#nav .megamenu').mouseover(function(){
	
		var main_container_width = $('.inner').width();
		var main_container_offset = $('.inner').offset();
		var parent = $(this);
		var megamenu = $(this).children('ul.sub-menu');
		var width_megamenu = megamenu.outerWidth();
		var position_megamenu = megamenu.offset();
		var position_parent = parent.position();
		
		var position_right_megamenu = position_parent.left + width_megamenu;
		
		// adjust if the right position of megamenu is out of container
		if ( position_right_megamenu > main_container_width ) {
			megamenu.offset( { top:position_megamenu.top, left:main_container_offset.left + ( main_container_width - width_megamenu ) } );		
		}
		
		//alert( 'width_megamenu = ' + width_megamenu + '; position_parent = top:' + position_parent.top + ', left:' + position_parent.left );
		//alert( 'width_megamenu = ' + width_megamenu + '; left = ' + main_container_offset.left + ( main_container_width - width_megamenu ) );
		
	});*/

	function yiw_lightbox()
	{   
	    $('#portfolio-gallery a.thumb').hover(
	                            
	        function()
	        {
	            $('<a class="zoom">zoom</a>').appendTo(this).css({
					dispay:'block', 
					opacity:0, 
					height:$(this).children('img').height(), 
					width:$(this).children('img').width(),
					'top':$(this).css('padding-top'),
					'left':$(this).css('padding-left'),
					//margin:'26px 16px',
					padding:0}).animate({opacity:0.4}, 500);
	        },
	        
	        function()
	        {           
	            $('.zoom').fadeOut(500, function(){$(this).remove()});
	        }
	    );
	    
		jQuery("a[rel^='prettyPhoto']").prettyPhoto({
	        slideshow:5000, 
	        autoplay_slideshow:false,
	        show_title:false
	    });
	}
	
	yiw_lightbox();
	
	
	$('.home_page_item_gallery a.thumb').hover(function(){
        $('<a class="zoom">zoom</a>')
            .appendTo(this)
            .css({
                dispay:'block', 
                opacity:0, 
                height:$(this).children('img').height(), 
                width:$(this).children('img').width(),
                'top':$(this).css('padding-top'),
                'left':$(this).css('padding-left'),
                //margin:'26px 16px',
                padding:0})
            .animate({opacity:0.4}, 500);
	}, function(){
	    $('.zoom').fadeOut(500, function(){$(this).remove()});
	});
	
	
	// slider
	//if ( $("#slider ul").length > 0 ) {
	if( typeof(yiw_slider_type) != 'undefined' ) {
    	if( yiw_slider_type == 'elegant' ) {
    		$("#slider ul").cycle({                                                    
    			easing 	: yiw_slider_elegant_easing,
    	    	fx 		: yiw_slider_elegant_fx,
    			speed 	: yiw_slider_elegant_speed,
    			timeout : yiw_slider_elegant_timeout,
    			before	: function(currSlideElement, nextSlideElement, options, forwardFlag) {
    				var width = parseInt( $('.slider-caption', currSlideElement).outerWidth() );
    				var height = parseInt( $('.slider-caption', currSlideElement).outerHeight() );
    				
    				$('.caption-top', currSlideElement).animate({top:height*-1}, yiw_slider_elegant_caption_speed);
    				$('.caption-bottom', currSlideElement).animate({bottom:height*-1}, yiw_slider_elegant_caption_speed);
    				$('.caption-left', currSlideElement).animate({left:width*-1}, yiw_slider_elegant_caption_speed);
    				$('.caption-right', currSlideElement).animate({right:width*-1}, yiw_slider_elegant_caption_speed);
    			},
    			after	: function(currSlideElement, nextSlideElement, options, forwardFlag) {
    				$('.caption-top', nextSlideElement).animate({top:0}, yiw_slider_elegant_caption_speed);
    				$('.caption-bottom', nextSlideElement).animate({bottom:0}, yiw_slider_elegant_caption_speed);
    				$('.caption-left', nextSlideElement).animate({left:0}, yiw_slider_elegant_caption_speed);
    				$('.caption-right', nextSlideElement).animate({right:0}, yiw_slider_elegant_caption_speed);
    			}
    	    });
    	} else if( yiw_slider_type == 'cycle') {
            $('#slider .images').cycle({
                fx      : yiw_slider_cycle_fx,
                speed   : yiw_slider_cycle_speed,
                timeout : yiw_slider_cycle_timeout,
                easing  : yiw_slider_cycle_easing,
                pager   : '.pagination',
                cleartype: true
            });                   
        
            $('#slider-pause').show();
                            
            $('#slider-pause').click(function(){
                $('#slider .images').cycle('pause');
                $(this).hide();
                $('#slider-play').show();
                return false;
            });
                            
            $('#slider-play').click(function(){
                $('#slider .images').cycle('resume');
                $(this).hide();
                $('#slider-pause').show();    
                return false;
            });
        } else if ( yiw_slider_type == 'elastic' ) {
            $('#slider.elastic').eislideshow({
                easing		: 'easeOutExpo',
                titleeasing	: 'easeOutExpo',
                titlespeed	: 1200,
                autoplay	: yiw_slider_elastic_autoplay,
                slideshow_interval : yiw_slider_elastic_timeout,
                speed       : yiw_slider_elastic_speed,
                animation   : yiw_slider_elastic_animation
            });
        } else if( yiw_slider_type == 'nivo' ) {
            $('#slider').nivoSlider({
                effect           : yiw_slider_nivo_fx,
                animSpeed        : yiw_slider_nivo_speed,
                pauseTime        : yiw_slider_nivo_timeout,
                directionNav     : yiw_slider_nivo_directionNav,
                directionNavHide : yiw_slider_nivo_directionNavHide,
                controlNav       : yiw_slider_nivo_controlNav
            });
        } else if( yiw_slider_type == 'carousel' ) {
            $('#slider ul.scrolling').jcarousel({
                wrap: "circular",
                auto: yiw_slider_carousel_auto,
                scroll: 1,
                start: 0,
                animation: yiw_slider_carousel_animation,
                //buttonNextHTML: null,
                buttonPrevHTML: null
            }); 
        }
    }
	
	// searchform on header    // autoclean labels
	$elements = $('#header #s, .autoclear');
    
	$elements.each(function(){
        if( $(this).val() != '' )	
			$(this).prev().css('display', 'none');
    }); 
    $elements.focus(function(){
        if( $(this).val() == '' )	
			$(this).prev().css('display', 'none');
    }); 
    $elements.blur(function(){ 
        if( $(this).val() == '' )	
        	$(this).prev().css('display', 'block');
    }); 

    $('a.socials, a.socials-small').tipsy({fade:true, gravity:'s'});
    
    $('.toggle-content:not(.opened), .content-tab:not(.opened)').hide();
    $('.toggle-title').click(function(){
        $(this).next().slideToggle(300);
        $(this).children('span.open-toggle').toggleClass('closed');
        $(this).attr('title', ($(this).attr('title') == 'Close') ? 'Open' : 'Close');
        return false; 
    });     
    $('.tab-index a').click(function(){           
        $(this).parent().next().slideToggle(300, 'easeOutExpo');
        $(this).toggleClass('opened');
        $(this).attr('title', ($(this).attr('title') == 'Close') ? 'Open' : 'Close');
        return false;
    });     
    
    $('.tabs-container').tabs();                                       
    $('.tabs-container').tabs( "option", "fx", { opacity: 'toggle' } );
    
    $('#slideshow images img').show();
    
    $("a[rel^='prettyPhoto']").prettyPhoto({theme: 'pp_default'});
    
    $('#content img:not(.icon, .internal), .thumb-project img, .widget_flickrRSS img, .more-projects-widget img').hover(function(){
        $(this).stop().animate({opacity: 0.65}, 700);
    }, function(){
        $(this).stop().animate({opacity: 1});
    });

    
    $('#portfolio a.thumb, .portfolio-slider a.thumb, #portfolio-bigimage a.thumb').hover(function(){
        $('<a class="zoom">zoom</a>').appendTo(this).css({
            dispay:'block', 
            opacity:0, 
            height:$(this).children('img').height(), 
            width:$(this).children('img').width(),
            'top':$(this).css('padding-top'),
            'left':$(this).css('padding-left'),
                    //margin:'26px 16px',
            padding:0}).animate({opacity:0.4}, 500);
    }, function(){           
        $('.zoom').fadeOut(500, function(){$(this).remove()});
    });
    
    
});          