$(document).ready(function() {
    
    if (!(jQuery.browser.msie && parseInt(jQuery.browser.version) < 7)) {
        $('.select-replace').each(function(){
            $(this).replaceSelects({
                replaceForIphone: false
            })
        });
    }
    $("a.fancy").each(function(){
        $(this).fancybox({
            'height': 570,
            'width': 1000,
            'scrolling'		: 'auto',
            'titleShow'		: false,
            'transitionIn' : 'none',
            'transitionOut' : 'none',
            'type':'iframe',
            'onCancel'		: function() {
                window.location.reload(true);
            }
        });
    });
    
    $("a.fancy-small").each(function(){
        $(this).fancybox({
            'height': 380,
            'width': 500,
            'scrolling'		: 'no',
            'titleShow'		: false,
            'transitionIn' : 'none',
            'transitionOut' : 'none',
            'type':'iframe'
        });
    });

    $("a[href*='build-a-bracelet']").each(function(){
        $(this).fancybox({
            'height': 660,
            'width': 980,
            'autoScale': false,
            'centerOnScroll' : true,
            'scrolling'		: 'no',
            'titleShow'		: false,
            'transitionIn' : 'none',
            'transitionOut' : 'none',
            'type': 'iframe',
            'onCleanup'		: function() {
                return confirm("If you close this window without clicking save your bracelet will be lost. Do you want to continue?");
            }
        })
    })
  	
    $('#fancy_close_link').click(function () {
        parent.top.$('#fancy_close').trigger('click');
        return false;
    });

    $('INPUT.auto-hint').each(function(i, el){
        if($(this).val() == ''){
            //$(this).val($(this).attr('title'));
            $(this).val($(this).attr('title'));
        }
        $(el).focus(function(){
            if ($(this).val() == $(this).attr('title')) {
                $(this).val('');
            }       
        });
        $(el).blur(function(){
            if ($(this).val() == '') {
                $(this).val($(this).attr('title'));
            }
        });
    });       
    
    //home page gallery
    $('#rotate UL').each(function(){
        $(this).cycle({
            fx:      'fade',
            speed:    1500,
            timeout:  5000,
            pager: '#nav-rotate'
        });
    });    
    
    $(function () {
        $('.tooltip-delete').each(function () {
            // options
            var distance = 10;
            var time = 250;
            var hideDelay = 500;
  
            var hideDelayTimer = null;
  
            // tracker
            var beingShown = false;
            var shown = false;
  
            var trigger = $(this);
            var popup = $('.tooltip-delete-content ul', this).css('opacity', 0);
  
            // set the mouseover and mouseout on both element
            $([trigger.get(0), popup.get(0)]).mouseover(function () {
                // stops the hide event if we move from the trigger to the popup element
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
  
                // don't trigger the animation again if we're being shown, or already visible
                if (beingShown || shown) {
                    return;
                } else {
                    beingShown = true;
  
                    // reset position of popup box
                    popup.css({
                        bottom: 20,
                        left: -76,
                        display: 'block' // brings the popup back in to view
                    })
  
                    // (we're using chaining on the popup) now animate it's opacity and position
                    .animate({
                        bottom: '+=' + distance + 'px',
                        opacity: 1
                    }, time, 'swing', function() {
                        // once the animation is complete, set the tracker variables
                        beingShown = false;
                        shown = true;
                    });
                }
            }).mouseout(function () {
                // reset the timer if we get fired again - avoids double animations
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
  
                // store the timer so that it can be cleared in the mouseover if required
                hideDelayTimer = setTimeout(function () {
                    hideDelayTimer = null;
                    popup.animate({
                        bottom: '-=' + distance + 'px',
                        opacity: 0
                    }, time, 'swing', function () {
                        // once the animate is complete, set the tracker variables
                        shown = false;
                        // hide the popup entirely after the effect (opacity alone doesn't do the job)
                        popup.css('display', 'none');
                    });
                }, hideDelay);
            });
        });
    });       

    // hide boxes with no content - user populated
    $('.box').each(function(i){
        var inner_html = $(this).html();
        var inner_html_trimmed = inner_html.replace(/^\s+|\s+$/g, '');
        if (inner_html_trimmed == ''){
            $(this).hide();
        }
    });
	
    //hide sf-menu if no other content
    $('.sf-menu').each(function(i){

        var inner_html = $(this).html();
        var inner_html_trimmed = inner_html.replace(/^\s+|\s+$/g, '');
        if (inner_html_trimmed == ''){
            $(this).hide();
        }

    });
	
    //hide sf-menu if no other content
    $('.comments').each(function(i){

        var inner_html = $(this).html();
        var inner_html_trimmed = inner_html.replace(/^\s+|\s+$/g, '');
        if (inner_html_trimmed == ''){
            $(this).hide();
        }

    });

    /* Only showing those sidebar block elements that contain content */
    $('.sidebox').each(function(i){

        $(this).children('.side-box').each(function(i){
            var inner_html         = $(this).html();
            var inner_html_trimmed = inner_html.replace(/^\s+|\s+$/g, '');
            if (inner_html_trimmed != '')
            {
                $(this).parent().show();
            }else $(this).parent().hide();
        });

    });

});

function clearBox(element)
{
    $(element).val("");
}
function changeBox()
{
    $('#pass_temp').css("display",'none');
    $('#password').css("display",'');
    $('#password').focus();
}
function restoreBox()
{
    if($('#password').val()=="")
    {
        $('#pass_temp').css("display",'');
        $('#password').css("display",'none');
    }
}

function clearValue(clearInput, defaultValue){
    if (clearInput.value == defaultValue) clearInput.value = '';
}

function rolloverOut(element,src){
    element.src = src;
}

function rolloverIn(element,src){
    element.src = src;
}

function showHide (showOrHide, elementToChange) {
    if (showOrHide == 'show')
    {
        $('#'+elementToChange).show();
    }
    else
    {
        $('#'+elementToChange).hide();
    }
}

function isDefined(object, variable){
    return (typeof(eval(object)[variable]) == 'undefined')? false : true;
}

function openWindow(url,width,height){
    window.open(url, '_blank','width='+width+',height='+height+',status=no,toolbar=no,menubar=no,location=no');
}