$(document).ready(function() {
   // qTip
   $.fn.qtip.styles.mystyle = { // Last part is the name of the style
   		name: 'dark', 
		border: {
			width: 7,
			radius: 5,
			color: '#333333'
		},
		tip: true
	}
   $('.qtip').qtip({
	   show: 'mouseover',
	   hide: 'mouseout',
	   style: {name: 'mystyle'}
	})
	$('a[title]').qtip({ style: { name: 'mystyle' } })
	$('img[title]').qtip({ style: { name: 'mystyle' } })
	
	// LI animation
	var fadeDuration = 150; //time in milliseconds
	
	$('.v-animated-list li a').hover(function() {
        $(this).stop().animate({ paddingLeft: '20px' }, fadeDuration);
        $(this).append("<span></span>");
        $(this).children('span').show().stop().animate({ left: -5 }, fadeDuration);
    }, function() {
        $(this).animate({ paddingLeft: '0px' }, fadeDuration);
        $(this).children('span').animate({ left: -35 }, fadeDuration).fadeOut(fadeDuration, 
            function() {
                $(this).children('span').remove();
            }
        );          
    });
    
    // Form input focus
    // Select form fields and attach them higlighter functionality
    var p = jQuery(this);
    p.find('input, select, textarea').focus(function() {
        p.find('.focused').removeClass('focused');
        $(this).addClass('focused');
    }).blur(function() {
        p.find('.focused').removeClass('focused');
    });
    
    $('div.expandable p').expander({
        slicePoint: 300,
        expandText: 'read more',
        userCollapseText: '[^]'
    });
    
    equalizeColumns();
    
    $(".date-field").datePicker().val(new Date().asString()).trigger('change');
});

function equalizeColumns() {
    equalizeColumns(0);
}
function equalizeColumns(addAdditionalHeight) {

    // Equalise out the column heights
    try {
    	var cl = $("#col-left").height() + addAdditionalHeight;
    	var cr = $("#col-right").height() + addAdditionalHeight;
    	
    	if (cl > cr) {
    	   $("#col-right").height(cl);
    	}
    	else {
    	   $("#col-left").height(cr);
    	}
	}
	catch(err) {}
}

function render_buttons() {
    $('.btn').each(function() {
        var b = $(this);
        var tt = b.text() || b.val();
        if ($(this).is(':input')) {
            b = $('<a>').insertAfter(this).addClass(this.className).attr('id',this.id);
            $(b).click(function(){
                $(this).parents('form').submit();
            });
            $(this).remove();
        }
        b.text('').css({cursor:'pointer'}).append($('<span>').text(tt).append('</span>'));
    });
}

function loadVideo(advert_name) {
    $("#advert-popup-holder").load("/load-advert/"+advert_name+"/",
        function () {
            $.blockUI({
                message: $("#advert-popup-holder"),
                css: blockuiform_css()
            });
        }
    );
}

function loadYoutubeVideo(youtube_id) {
    $("#youtube-popup-holder").load("/load-youtubevideo/"+youtube_id+"/",
        function () {
            $.blockUI({
                message: $("#youtube-popup-holder"),
                css: blockuiform_css()
            });
        }
    );
}

// Buttons
$(document).ready(function() {
    render_buttons();
});

function add_notification(message) {
    if($('.messages').length > 0) {
        $('.messages').append('<li><span>' + message + '</span></li>');
    }
    else {
        $('#bd').before('<ul class="messages"><li><span>' + message + '</span></li></ul>');
    }
};

function fixie6png() {
    if ($.browser.msie && $.browser.version.substr(0,1)<7) {
        $('.contentholder-black-wrapper').supersleight('', false);
        $('.contentholder-white-wrapper').supersleight('', false);
        $('#results-filter-openclose').supersleight('', false);
        $('#footer-right').supersleight('', true);
        $('.button-toolbar-hoverpane').supersleight('', false);
        $('.button-toolbar-content-icon').supersleight('', true);
        $('#fav-icon-div').supersleight('', true);
        $('.step_stage_icons').supersleight('', true);
    }
}

function beforeForm() {
    $.blockUI({
        message: '<div style="text-align:center;"><h3>Processing, please wait...</h3></div>', 
        css: blockuimessage_css()
    });
    return true;
}

function processJson_complete(data) {
    $.unblockUI();
    if (eval(data.success)) {
        $.blockUI({
            message: $('#success_form'),
            css: blockuimessage_css()
        });
    }
    else {
        $.blockUI({
            message: $('#failed_form'),
            css: blockuimessage_css()
        });
    }
}

function blockuimessage_css() {
    css = {
        top:  ($(window).height() - 500) /2 + 'px', 
        left: ($(window).width() - 500) /2 + 'px', 
        border: 'none',
        textAlign: 'left',
        padding: '0px 15px 15px 15px', 
        backgroundColor: '#333333', 
        '-webkit-border-radius': '10px', 
        '-moz-border-radius': '10px', 
        opacity: .8, 
        color: '#fff'
    }
    return css
}

function blockuiform_css() {
    css = {
        top:  ($(window).height() - 500) /2 + 'px', 
        left: ($(window).width() - 500) /2 + 'px', 
        border: 'none', 
        textAlign: 'left',
        width: '460px',
        padding: '0px 15px 15px 15px', 
        backgroundColor: '#333333', 
        '-webkit-border-radius': '10px', 
        '-moz-border-radius': '10px', 
        opacity: .9, 
        color: '#fff'
    }
    return css
}