/*
 * Copyright (c) 2009 - 2010 Aleksey Garlex
 *   http://www.garlex.ru
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */
$(document).ready(function(){
    /*=== Title page portfolio ===*/
    var ptlink = $('.portfolio ul li');
    
    ptlink.mouseover(function(){
        // Size and position for blocks
        var wPort = $('.portfolio').width();
        var pb = $(this).position();

				$('.tooltip').remove();
        
        // Creat tooltip objects
        var Tooltip = $('<div class="tooltip"></div>');
        var textTooltip = $('.notice', this).clone();
        var imgClone = $('a', this).clone();
        
        // Insert content in tooltip
        Tooltip.appendTo('.portfolio').append(imgClone).append(textTooltip);
        
        var wb = Tooltip.width();
        
        if (pb.left + wb < wPort) {
            Tooltip.css({
                'top': pb.top - 2,
                'left': pb.left - 2
            });
        }
        else {
        
            Tooltip.css({
                'top': pb.top - 2,
                'left': pb.left - (wb - 111)
            }).addClass('r');
        }
        
        // Close tooltip
        imgClone.mouseout(function(){
				Tooltip.remove().removeClass('r');
        });
    });
/*-=== Slider for portfolio ===-*/
		// Creat objects
		var pLink = $('.b_slider');
		var pBlock = $('.p_block');
		var pItem = $('.p_item');
		var nItem = pItem.length;
		
		// Size for blocks
		var wItem = pItem.width() + 45;
		var stopAnim = wItem * (nItem - 3);
		
		pLink.click(function(){
				var pos = pBlock.position();
        // Break animations
				$('.b_sl').queue(function () {
					if(pos.left > '-' + wItem-45){$(this).hide();}
					$(this).dequeue();
				});
		
				if ($(this).hasClass('b_sr')) {
						if (pos.left > '-' + stopAnim) {
								pBlock.animate({'left': '-=' + wItem},{queue:false});
								$('.b_sl').show();
						}
						else {
								pBlock.animate({'left':'0'},{queue:false});
								$('.b_sl').hide();
						}
				}
				else {
						if (pos.left < 0) {
								pBlock.animate({'left': '+=' + wItem},{queue:false});
						}
				}
				return false;
		});
    
/*-=== Popup portfolio window ===-*/
		$(function(){
				$('a.big_img').lightBox();
		});
    
});

/* To be continued... */
