/**
 * S&S Bathrooms
 * 
 * Generic Javascript functions
 * 
 * @author redliquid
 * @version 0.1
 */

var globalHref = '';
var aTarget = null;
var currentImage = '';

/**
 * Get the current coordinates of the first element in the set of matched
 * elements, relative to the closest positioned ancestor element that
 * matches the selector.
 * @param {Object} selector
 */
jQuery.fn.positionAncestor = function(selector) {
    var left = 0;
    var top = 0;
    this.each(function(index, element) {
        // check if current element has an ancestor matching a selector
        // and that ancestor is positioned
        var $ancestor = $(this).closest(selector);
        if ($ancestor.length && $ancestor.css("position") !== "static") {
            var $child = $(this);
            var childMarginEdgeLeft = $child.offset().left - parseInt($child.css("marginLeft"), 10);
            var childMarginEdgeTop = $child.offset().top - parseInt($child.css("marginTop"), 10);
            var ancestorPaddingEdgeLeft = $ancestor.offset().left + parseInt($ancestor.css("borderLeftWidth"), 10);
            var ancestorPaddingEdgeTop = $ancestor.offset().top + parseInt($ancestor.css("borderTopWidth"), 10);
            left = childMarginEdgeLeft - ancestorPaddingEdgeLeft;
            top = childMarginEdgeTop - ancestorPaddingEdgeTop;
            // we have found the ancestor and computed the position
            // stop iterating
            return false;
        }
    });
    return {
        left:    left,
        top:    top
    }
};

function getFilename(fullPath)
{
	return fullPath.replace(/^.*(\\|\/|\:)/, '');
}

$(document).ready(function(){
	
	$('a').focus(function(){
		$(this).blur();
	});
	
	$('a[rel="colorbox"]').colorbox();
	
	// menu handling
	$('#menuContainer ul > li > a.main').mouseover(function(){
		if ( !($(this).parent().find('ul').hasClass('active') ))
		{
			$('#menuContainer ul li').removeClass('active');
			$('#menuContainer ul li ul').fadeOut('fast').removeClass('active');
			$(this).parent().find('ul').fadeIn('fast').addClass('active');
			$(this).parents('li').addClass('active');
		}
	});
	
	// $('.clickDiv').css('opacity', 0.3);
	
	$('#productImagesList li').mouseenter(function(){
		aTarget = $(this).find('a');
		
		$(this).find('a').stop().fadeTo('fast', 0.5);
		
		var div = $('.clickDiv').clone();
		$(div).show();
		$(this).append(div);
		
	}).mouseleave(function(){
		$(this).find('a').stop().fadeTo('fast', 1);
		$(this).find('.clickDiv').remove();
	});
	
	$('#productImagesList li div.clickDiv').live('click', function(){
		$(aTarget).click();
	});
	
	$('#brandsImagesList li').mouseenter(function(){
		currentImage = $(this).find('img').attr('src');
		
		// get new image
		var filename = getFilename(currentImage);
		var newImage = 'public/upload/brands/color/' + filename;
		
		$(this).find('img').attr('src', newImage);
	}).mouseleave(function(){
		$(this).find('img').attr('src', currentImage);
	});
	
	// modification 21394872384 of this file :/
	// ignorance is bless
	$('#projectDescription ul li').mouseenter(function(){
		aTarget = $(this).find('a');
		
		$(this).find('a').stop().fadeTo('fast', 0.5);
		
		var div = $('.clickDiv').clone();
		
		// get img dimension
		var width = $(this).find('img').width();
		var height = $(this).find('img').height();
		
		$(div).width(width);
		$(div).height(height);
		$(div).css('margin-top', '20px');
		
		$(div).show();
		$(this).append(div);
		
	}).mouseleave(function(){
		$(this).find('a').stop().fadeTo('fast', 1);
		$(this).find('.clickDiv').remove();
	});
	
	$('#projectDescription li div.clickDiv').live('click', function(){
		$(aTarget).click();
	});	
	
});
