$( function () {

// MEGA MENU
    $( '.nav-item' ).megaMenu({
        megaMenuContainer: '.sub-menu',
        effect: 'fade',
        delay: 200,
        speed: 200
    });

// HOME PAGE SLIDESHOW
	// First let's center the text vertically, while the slideshow is invisible (by css)...
	$( '#slideshow .slide .slide-content p' ).each(
		function( index, paragraph ) {
	    	height = $( paragraph ).height();
			$( paragraph ).css( 'paddingTop', (280-height)/2 + 'px' );
	});
	// ...now, let's prepare the slideshow...
    $( '#slideshow' ).slideshow({
        slide: '.slide',
        navigation: '#slideshow-nav a',
        navigationSelectedClass: 'selected',
        slideshow: false,
        slideshowSpeed: 3000,
        startFromIndex: 1,
        transition: 'fade',
        transitionSpeed: 400,
        mouseOverFreeze: true
    });
	// ...and, finally, show the slideshow now that everything's in place.
	$( '#slideshow' ).css( 'visibility', 'visible' );

// CLIENTS SLIDESHOW
    $( '#client-slideshow' ).slideshow({
        slide: '.client-slide',
        next: '#client-slideshow-nav #next',
        previous: '#client-slideshow-nav #prev',
        navigationSelectedClass: 'selected',
        slideshow: false,
        slideshowSpeed: 3000,
        startFromIndex: 1,
        transition: 'fade',
        transitionSpeed: 400,
        mouseOverFreeze: true
    });
	
// OFFERS SLIDESHOW
    $( '#offers-banner' ).slideshow({
        navigation: '#slideshow-selector a',
        slide: '.slide-offer',
        next: '.next-slide-a',
        previous: '.previous-slide-a',
        navigationSelectedClass: 'selected',
        slideshow: true,
        slideshowSpeed: 6000,
        startFromIndex: 1,
        transition: 'fade',
        transitionSpeed: 700,
		resizeMode: 'auto',
        mouseOverFreeze: true
    });
	
// PREETY TABLES
	$( '#content table caption' ).wrapInner( '<span class = "caption-content" />' );
	$( '#content table tr:odd' ).addClass( 'even' );
	$( '#content table tr:last td:first' ).addClass( 'bot-left' );
	$( '#content table td:last' ).addClass( 'bot-right' );
	
// MENUS
	// Hide all children lists, except for the ones that are above the current category and the one directly below it
	$ ( '#menu .children' ).not( $ ( '.current-cat, .current_page_item' ).parents() ).not( $ ( '.current-cat .children:first, .current_page_item .children:first' ) ).css( 'display', 'none' );
		
	// On menu item click...
	$ ( '#menu .cat-item a' ).click( function( event ) {
		
		// If the item is not a top level category (it's parent's parent -ul- is not #menu)
		if ( $( this ).parent().parent().attr( 'id' ) != 'menu' ) {
		
			// If the item has a submenu list as a sibling...
			if ( $( this ).next( '.children' ).length > 0 ) {
			
				// Disable the hyperlink
				event.preventDefault();
			
				// If the submenu list is hidden
				if ( $( this ).next( '.children' ).css( 'display' ) == 'none' ) {
				
					// Show it
					$( this ).next( '.children' ).css( 'display', 'block' );
				
				} else {
				
					// Hide all submenu lists under this item
					$( this ).parent().find( '.children' ).css( 'display', 'none' );
				
				}
			
			}
		
		}
		
	});

// Image galleries width adjustment
	$( '.image-thumbnails' ).css( 'width', $('.product-thumbnail').width() + 15 + 'px' );

// CONTACT FORM VALIDATION & SUBMISSION
	$( '.messages' ).css('display', 'none');

    $( '.contact-form:not(#product-search)' ).formValidation({ 
		
    	callback: function ( $_error_elements ) {

			$('.messages').fadeOut();

            if ( $_error_elements ) {

                $( '.messages' ).addClass( 'failure' ).text( 'Συμπληρώστε σωστά όλα τα πεδία!' );
				$('.messages').fadeIn();

                return false;
            }

			$('.messages').fadeOut( 'fast', function() { 
				$( '.messages' ).removeClass( 'failure' ).html( '<img src = "' + TEMPLATE_URL + '/images/ajax-loader-small.gif" atl = "Loading..." /> Παρακαλώ περιμένετε... ' ).fadeIn(); 
			});

            $.ajax({
                url: BLOG_URL + '/wp-admin/admin-ajax.php',
                type: 'GET',
                data: 'action=contact_form&' + $( '.contact-form' ).serialize(),
                dataType: 'text', 
                success: 
                    function ( results ) {
                        if ( results == 'success' ) {
                            $( '.contact-form input' ).attr( 'value', '' );
                            $( '.contact-form textarea' ).text('');
                            $( '.contact-form .submit' ).attr( 'disabled', 'disabled' ).fadeOut( 'fast' );
							$('.messages').fadeOut( 'fast', function() {
								 $( '.messages' ).addClass( 'success' ).text( 'Το μήνυμά σας έχει σταλεί!' ).fadeIn();
							});
		                } else {
							$('.messages').fadeOut( 'fast', function() {
								 $( '.messages' ).addClass( 'failure' ).text( 'Παρακαλώ δοκιμάστε ξανά.' ).fadeIn();
							});

                        }
                    },
                error: 
                    function () {
                        alert( 'We be finding dem errors.' );
                    }
            });

            return false;
        }
    });


});
