// <![CDATA[
$(function() {
	
	// border-radius
	$('p.tags_line a').css({ "border-radius":"5px", "-moz-border-radius":"5px", "-webkit-border-radius":"5px" });
	
	// main_menu lavaLamp
	$('.main_menu ul').lavaLamp();
	
	// scroll select menu
	$('body').click(function (event) {
        if (event.target.nodeName != 'A') {
		    $('ul.list_slide').slideUp('fast');
        }
	});
	$('a[rel="slide_menu"], span[rel="slide_menu"]').click(function (event) {
		var thisObj = $(this).parent("div.slide_select").children("ul");
		if (thisObj.css('display') == 'none') {
			$('ul.list_slide').slideUp('fast');
			thisObj.slideDown();
		} else {
			$('ul.list_slide').slideUp('fast');
		}
        event.preventDefault();
		return false;
	});	
	$('ul.list_slide li').click(function (event) {
            if ($(this).hasClass('fake')) {
                event.preventDefault();
            } else {
		var parentObjUl = $(this).parent('ul.list_slide');
		var parentObj = parentObjUl.parent("div.slide_select");
		parentObjUl.children('li').removeClass('active');
		$(this).addClass('active');
		parentObj.children('ul').slideUp('fast');
		parentObj.children('input:hidden').val($(this).children('span').attr('rel'));
		parentObj.children('span[rel="slide_menu"]').text($(this).children('span').text());
            }
	});
	
	// ToolTips
	$("#submit_form :input").tooltip({
		// place tooltip on the right edge
		position: "center right",
		// a little tweaking of the position
		offset: [-2, 10],
		// use the built-in fadeIn/fadeOut effect
		effect: "fade",
		// custom opacity setting
		opacity: 0.9
	});
	$("#addcomment label").tooltip({
		// place tooltip on the right edge
		position: "center right",
		// a little tweaking of the position
		offset: [-2, 10],
		// use the built-in fadeIn/fadeOut effect
		effect: "fade",
		// custom opacity setting
		opacity: 0.9
	});

	// Caption thumbs
	$('.thumb').mouseenter(function () {
		$(this).find('.thumb_caption div').animate({
			top: '0px'
		});
	}).mouseleave(function () {
		$(this).find('.thumb_caption div').animate({
			top: '480px'
		});
	});
    
    $('.thumb_caption').click(function(event) {
        var href = $(this).attr('data-href');
        var nodeName = event.target.nodeName;
        if (href && nodeName != 'A') {
            location.href = href;
        }
    });

});

// like it
function like_it(id, type) {
    $("#loading").ajaxStart(function(){
        $(this).show();
    }).ajaxComplete(function(){
        $(this).hide();
    });

    $.post("/", {
        ajax: "like",
        like_page_id: id,
        like_type_id: type
    },
    function(data) {
        $('div.error').remove();
        $('div.good').remove();
        if('error' == data.status) {
            $('div.content').prepend('<div class="error">'+data.message+'</div>');
            align_messages();
            $("#loading").hide();
        } else  if ('success' == data.status) {
            $('div.content').prepend('<div class="good">'+data.message+'</div>');
            align_messages();
            if (type == 1) {
                $('#a_like_site_'+id).html(data.likes_count);
            } else if(type == 2) {
                $('a#a_like_comment_'+id).html(data.likes_count);
            } else if (type == 3) {
                $('#a_like_site_'+id).html(data.likes_count);
            } else if(type == 4) {
                $('a#a_like_comment_'+id).html(data.likes_count);
            } else if (type == 5) {
                $('a#a_like_file').html("<strong>"+data.likes_count+" Likes</strong>");
            }
            $("#loading").hide();
        }
    }, "json");

}

//save it
function save_it(id) {
    $('#save_website_id').val(id);
    $("#modal").modal({
        overlayClose:true,
        onShow: function() {
            //alert(collection_id());
            $("select#collection_id").val(collection_id());
        }
    });
}
function collection_id() {
    return window.localStorage.getItem("last_collection_id");
}
function set_collection_id(collection_id) {
    window.localStorage.setItem("last_collection_id", collection_id);
    return collection_id;
}
$(function() {
    $("#collection_id").change(function() {
        //alert($("select#collection_id option:selected").val());
        set_collection_id($("select#collection_id option:selected").val());
    });
});

function save_it_action(id) {
    $.post("/", {
        ajax: "save",
        save_page_id: id,
        save_collection_id: $("select#collection_id").val()
      },
      function(data) {
        $('div.error').remove();
        $('div.good').remove();
        $('div.content').prepend('<div class="'+(data.status == 'success' ? 'good' : 'error')+'">'+data.message+'</div>');
        align_messages();
        //$("#loading").hide();
        $.modal.close();
      }, "json");
}

align_messages = function() {
    var initialTop = 5;
    var windowScroll = 0;
    windowScroll = $(window).scrollTop();
    if (windowScroll < 177) { windowScroll = 0 } else { windowScroll = windowScroll - 177 };
    var top = windowScroll + initialTop;
    $('.error').css('top', top + 'px' );
    $('.good').css('top', top + 'px' );
}
$(function() {
    $(window).scroll(align_messages);
    $('.content').change(align_messages);
});

//save it
function remove_it(id) {
    $("#loading").ajaxStart(function(){
        $(this).show();
    }).ajaxComplete(function(){
        $(this).hide();
    });

    $.post("/", {
        ajax: "remove",
        remove_id: id
      },
      function(data) {
        $('div.error').remove();
        $('div.good').remove();
        $('div.content').prepend('<div class="'+(data.status == 'success' ? 'good' : 'error')+'">'+data.message+'</div>');
        align_messages();
        if (data.status == 'success') {
            $('#collection_item_'+id).hide();
        }
        $("#loading").hide();
      }, "json");
}

// ]]>

