jQuery.fn.topLink = function(settings) {
    settings = jQuery.extend({
        min: 1,
        fadeSpeed: 200,
        ieOffset: 50
    }, settings);
    return this.each(function() {
        //listen for scroll
        var el = $(this);
        el.css('display','none'); //in case the user forgot
        $(window).scroll(function() {
            if($(window).scrollTop() >= settings.min)
            {
                el.fadeIn(settings.fadeSpeed);
            }
            else
            {
                el.fadeOut(settings.fadeSpeed);
            }
        });
    });
};

$(document).ready(function(){
    $("a.reply").click(function(event) {
        event.preventDefault();
        $("a.reply").parent().find('form').slideUp();
        $(this).parent().find('form').slideDown();
    });
     
    $(".mmenu li").dwFadingLinks({
        background: '#FFF',
        duration: 900
    });
     
    $(".mmenu a").dwFadingLinks({
        color: '#FFF',
        duration: 900
    });
    
    $('#top-link').topLink({
        min: 300,
        fadeSpeed: 500
    });
    //smoothscroll
    $('#top-link').click(function(e) {
        e.preventDefault();
        $.scrollTo(0,300);
    });
    
    $("#ub_inact").mouseenter(function() {
        $("#ub_act").fadeIn();
    });
    
    $("#ub_act").mouseleave(function() {
        $("#ub_act").fadeOut();
    });
    
    $(".widget").mouseenter(function() {
        var toFade = $(this).find('.widget_handle');
        if($(this).parent().parent().find(".widget_settings").css('display') == 'block') {
            toFade.show();
        } else {
            if (toFade.is(':animated')) {
                toFade.stop().fadeTo(500, 1);
            } else {
                toFade.fadeIn(500);
            }
        }
        //$(this).parent().find('.widget_handle').fadeIn();
    });
    
    /*
    $(".widget").mouseleave(function() {
        var toFade = $(this).find('.widget_handle');
        if($(this).parent().parent().find(".widget_settings").css('display') == 'block') {
            toFade.fadeOut.show();
        }
        else {
            if (toFade.is(':animated')) {
                toFade.stop().fadeTo(500, 0);
            } else {
                toFade.fadeOut(500);
            }
        }
    });
    */
    
    $(".edit_settings").click(function() {
        if($(this).parent().find(".widget_settings").css('display') == 'block') {
            $(this).parent().find(".widget_settings").slideUp();
        } else {
            $(this).parent().find(".widget_settings").slideDown();
        }
    });
    
    $(".widget_handle").mouseleave(function() {
        //$(this).parent().parent().find(".widget_settings").slideUp();
    });
    
    $("#hdr_overlay_hdl").mouseenter(function() {
        $("#hdr_overlay").fadeIn();
    });
    
    $("#hdr_overlay").mouseleave(function() {
        $("#hdr_overlay").fadeOut();
    });
    
    $('#quicknotice').hide();
    setTimeout(function(){ $('#quicknotice').slideDown('slow'); }, 4000);
    setTimeout(function(){ $('#quicknotice').slideUp('slow'); }, 8000);

    $(".widget_DISABLED_ATM").hover(function() {
        var col = $(this).parent().attr("id");
        var rep = 2;
        if(col == 'col_b') {
            rep = 4;
        }
        var it = 1;
        // Fix Latest additions margins
        $(this).find(".tn_holder").each(function() {
            if(it % rep == 0) {
                $(this).css('margin-right', '0px');
            } else {
                $(this).css('margin-right', '20px');
            }
            it++;
        });
    });

    $("#dialog").dialog({
			bgiframe: true,
			modal: true,
            minHeight: 480,
            width: 760,
			buttons: {
				'Save Layout': function() {
                    //$(this).dialog('close');
                    window.location.reload(true);
				}
			},
            autoOpen: false
		});
    
    $("#dialog_link").click(function() {
        $("#dialog").dialog('open');
    });



/*
    $("#columna, #columnb, #columnc, #columnd").sortable({
        placeholder: 'ui-state-highlight',
        connectWith: '.connectedSortable',
        stop: function(){ $("#columna, #columnb, #columnc, #columnd").sortable("refreshPositions"); serialize_order(); }
    }).disableSelection();
*/

    jQuery.fn.fadeToggle = function(speed, easing, callback) {
      return this.animate({opacity: 'toggle'}, speed, easing, callback); 
    };

    $(".widgetColumn").sortable({
        placeholder: 'ui-state-highlight',
        connectWith: '.widgetColumn',
        handle: '.widget_move',
        revert: true,
        forcePlaceholderSize: true,
        start: function(event, ui) { ui.item.bind("click.prevent", function(event) { event.preventDefault();  }); },
        stop: function(event, ui){ $(".widgetColumn").sortable("refreshPositions"); setTimeout(function(){ui.item.unbind("click.prevent");}, 300); serialize_order(); }
    //}).disableSelection();
    });

    $(".widgets_unlock").click(function() {
        $(".widget_handlers").fadeToggle();
        $(this).find("span span").text($(this).text() == 'Edit page' ? 'Done' : 'Edit page');
        $("#customize_panel").slideToggle();
    });
    
    $(".widget_toggle").click(function() {
        //$(this).parent().parent().find('.widget_settings').height($(this).parent().parent().find('.widget_html').height());
        //$(this).parent().toggleClass('subtitle_hover');
        $(this).parent().parent().find('.widget_html').slideToggle(function() {
            $(this).parent().find('.widget_settings').slideToggle();
        });
        $(this).find("span span").text($(this).text() == 'Edit' ? 'Done' : 'Edit');
    });
    
    $("#add_entry_toggle").click(function() {
        $(this).text($(this).text() == 'Add a new entry' ? 'Close entry form' : 'Add a new entry');
        $(".add_entry").slideToggle();
    })
    
    $(".buy_reward").click(function() {
        return confirm("Are you sure you want to buy this reward?");
    });
    

    jQuery('.sexy-bookmarks a.external').attr("target", "_blank");

    var sexyBaseHeight=jQuery('.sexy-bookmarks').height();
	var sexyFullHeight=jQuery('.sexy-bookmarks ul.socials').height();
    if (sexyFullHeight>sexyBaseHeight || 'a'=='a') {
		jQuery('.sexy-bookmarks-expand').hover(
			function() {
				jQuery(this).animate({
						height: 60+'px'
				}, {duration: 400, queue: false});
			},
			function() {
				jQuery(this).animate({
						height: 30+'px'
				}, {duration: 400, queue: false});
			}
		);
	}
    
    
			
	$("a").click(function() {
		$(this).blur();
	});
	
	$("ul#menu a").mouseenter(function() {	
		$(this).animate({'color' : '#FFFFFF'}, 350);
	});
	$("ul#menu a").mouseleave(function() {	
		$(this).animate({'color' : '#CBCBCB'}, 350);
	});
	
	$("#discover").mouseenter(function(e) {
		$("ul#discover_menu").show('fast');
		$("#discover").toggleClass("menu_open");
	});
	$("#discover_menu").mouseleave(function(e) {
		$("ul#discover_menu").hide('fast');
		$("#discover").toggleClass("menu_open");
	});

	$("ul#discover_menu").mouseup(function() {
		return false
	});
    

});

function serialize_order() {
    col_a = $('.cola').sortable('toArray');
    col_b = $('.colb').sortable('toArray');
    col_c = $('.colc').sortable('toArray');
    col_d = $('.cold').sortable('toArray');
    //cols = $('.widgetColumn').sortable('serialize', {connected: true});
    //col_b = $('#columnb').sortable('toArray');
    //col_c = $('#columnc').sortable('toArray');
    //col_d = $('#columnd').sortable('toArray');
    res = "col_a="+col_a+"&col_b="+col_b+"&col_c="+col_c+"&col_d="+col_d;
    //document.forms.serialist.serial.value = res;
    //document.forms.serialist.submit();
    //alert(res);
    $.post("/save_layout/", { serial: res } );
}

