﻿$(document).ready(function() {
   
    /* recommendations */
    $('.recommendations .details').hide();
    $('.recommendations h3')
        .click(function() {
            var $recommendation = $(this).parent();
            var $details = $recommendation.find('.details');
            if (!$recommendation.hasClass('open')) {
                $details.slideDown();
                $recommendation
                    .addClass('open')
                    .animate({paddingBottom: '10px', marginTop: '10px'});
            }
            else {
                $details.slideUp();
                $recommendation
                    .animate({paddingBottom: '0', marginTop: '0'}, function(){ $(this).removeClass('open') });
            }
        })
        .css({cursor: 'pointer'});

    /* overview page */
    $('.overview-content-pane').not(':first').hide();
    $('.overview-right-pane a:first').addClass('current');
    $('.overview-right-pane a').click(function() {
        //$('.overview-right-pane a.current')
        var index = $(this).parent().parent().find('a').index(this);
        $('.overview-content-pane').hide();
        $('.overview-content-pane').eq(index).fadeIn();
        
        $('.overview-right-pane a').removeClass('current');
        $(this).addClass('current');
    });
    
    /* gallery */
    $('ul.gallery-thumbnails a').not('.admin-edit-portfolio-item a')
        .click(function(e) {
            e.preventDefault();
	    $('ul.gallery-thumbnails a.active').removeClass('active');
	    $(this).addClass('active');
            $('.gallery-big').html('<a id=\'btnback\'>«הקודם</a>&nbsp;&nbsp;&nbsp;<a id=\'btnnext\'>הבא»</a><br /><img src="' + $(this).attr('href') + '" />');
	   
	    
	    $('.gallery-big img, .gallery-big #btnnext').css({ cursor: 'pointer' }).click(function() {
		     $('ul.gallery-thumbnails a.active').parent().next().find('a').trigger('click');
	           
	      });
	    
	    $('.gallery-big #btnback').css({ cursor: 'pointer' }).click(function() {
	    	     $('ul.gallery-thumbnails a.active').parent().prev().find('a').trigger('click');
	    });
        })
        .filter(':first').trigger('click');
        
    /* hp gallery */
    var totalWidth = ($('ul.hp-gallery-thumbnails li').length * 128) + 'px';
    $('ul.hp-gallery-thumbnails').css({width: totalWidth}).addClass('original');
    $('.hp-gallery-mask').append('<ul class="reset hp-gallery-thumbnails clone" style="width: ' + totalWidth + '"></ul>');
    
    $('ul.hp-gallery-thumbnails.clone').html($('ul.hp-gallery-thumbnails.original').html());
    //var $clone = $('ul.hp-gallery-thumbnails').clone().removeClass('original').addClass('clone');
    
    scrollHPGallery();
    scrollHPGalleryClone();
    $('.icon-1 a').fadeOut(350, function() {
        $('.icon-1 a').fadeIn(350, function() {
            $('.icon-2 a').fadeOut(350, function() {
                $('.icon-2 a').fadeIn(350, function() {
                    $('.icon-3 a').fadeOut(350, function() {
                        $('.icon-3 a').fadeIn(350, function() {
                            $('.icon-4 a').fadeOut(350, function() {
                                $('.icon-4 a').fadeIn(350, function() {
                                    $('.icon-5 a').fadeOut(350, function() {
                                        $('.icon-5 a').fadeIn(350, function() { });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
    });
});

function scrollHPGallery() {
    var totalWidth = $('ul.hp-gallery-thumbnails.original').width() + 'px';
    var duration = ($('ul.hp-gallery-thumbnails.original').width() + 750) * 15
    $('ul.hp-gallery-thumbnails.original')
        .css({right: '0'})
        .animate({right: '-' + totalWidth}, duration, 'linear', function() { scrollHPGallery() });
}

function scrollHPGalleryClone() {
    var totalWidth = $('ul.hp-gallery-thumbnails.original').width() + 'px';
    var duration = ($('ul.hp-gallery-thumbnails.original').width() + 750) * 15
    $('ul.hp-gallery-thumbnails.clone')
        .css({right: totalWidth})
        .animate({right: '0'}, duration, 'linear', function() { scrollHPGalleryClone() });
}
