$(function() {
    Cufon.replace('h1');
    Cufon.replace('h2');
    Cufon.replace('h3');

    var delay = 400,
        _tm = [];

    function clearTimeouts () {
        for (var i = 0, length = _tm.length; i < length; i++) {
          clearTimeout(_tm[i]);
        };
    };

    $('.custom_button').mouseenter(function () {
        var p = $(this).parent('div');
        p.data('hidden', false);
        p.next('.hover_menu').fadeIn('fast');
        $(this).addClass('active');
        clearTimeouts();
    }).mouseleave(function () {
        var that = $(this);
        if (!$(this).parent('div').data('hidden')) {
            _tm.push(setTimeout(function () {
                that.removeClass('active').parent('div').next('div').fadeOut('fast');
            }, delay));
        };
    });

    $('.hover_menu').mouseenter(function () {
        var hidden = $(this).prev('div').data('hidden');

        if (!hidden) {
            $(this).fadeIn('fast');
        };
        clearTimeouts();
    }).mouseleave(function () {
        var that = $(this);
        if (!$(this).prev('div').data('hidden')) {
            _tm.push(setTimeout(function () {
                that.removeClass('active').fadeOut('fast');
                that.prev('div').children('.custom_button').removeClass('active');
            }, delay));
        };
    });

    $('#p-friend').click(function () {
        $('#p-share').find('.addthis_button_email').trigger('click');
    });

    $('#p-like').click(function () {
        var url = 'http://www.facebook.com/share.php?u=' + $('#p-share').attr('addthis:url');
        var newWindow = window.open(url, '_blank');
        newWindow.focus();
        return false;
    });

    $('.search select').change(function() {
        var target = $(this).find(':selected').eq(0).attr('rel');
        if (target) {
            window.location = target;
        }
    });

	var p = $('#designer-all-pic');


    $('.designer-link').mouseenter(function () {
        var current_link = $(this)[0];
        if(navigator.appVersion.indexOf("MSIE 7.") != -1){
        	var offset = current_link;
        	var offsetTop = current_link.offsetTop - 5;
        	var offsetLeft = 0;
        	while(offset.offsetParent){
        		offset=offset.offsetParent;
        		offsetTop += offset.offsetTop;
      		}
      		offset = current_link;
      		while(offset.offsetParent){
      			if(offset == document.body) break;
        		offset=offset.offsetParent;
        		offsetLeft += (offset .offsetLeft)? offset .offsetLeft: 0;	
        	}
      		
        	p.css({ top: offsetTop,
                   left: offsetLeft
        	});
        } else {  
           	p.css({ top: current_link.offsetTop - 5,
                   left: current_link.offsetLeft + current_link.offsetWidth + 30
        	});
        }
        p.show();
    }).mouseleave(function () {
        p.hide();
    });

    if($('.featured-styles')[0]) {
        var banners = $('.featured-styles .banners');
        var controls = $('.featured-styles .controls');
        var markers = controls.find('.marker')        

        //controls.css({left: (950 - controls.width()) / 2 + 'px', top: '335px'});
        if (!$.browser.msie) controls.css('margin-left', (950 - controls.width()) / 2 + 'px');
        
        var cycle = banners.cycle({
            fx: 'fade',
            next: '.featured-styles .next',
            prev: '.featured-styles .prev',
            before: function(currSlideElement, nextSlideElement, options, forwardFlag) {
                var i = $(nextSlideElement).index('.featured-styles .banners li');
                markers.removeClass('activeSlide');
                markers.eq(i).addClass('activeSlide');
            },
            //pager: '.featured-styles .controls',
            pagerAnchorBuilder: function(idx, slide) {
                return '.featured-styles .controls .marker:eq(' + idx + ')';
            }
        });
    }
});

