//flash
$(document).ready(function() {   
   var flash = $(".flash");
   if(flash.html()) flash.slideDown();
});
$(".flash").live("click", function(){$(this).fadeOut('slow');});

//dialogboxes
$(".dbox_delete_confirm").live("click", DeleteConfirmDialog);
function DeleteConfirmDialog(Event){
    Event.preventDefault();
    action = $(this).attr("action");
    formname = $(this).attr("formname");
    $("form[name="+formname+"] input[name=action]").val(action);
    $(".dialogbox").hide();
    if($("#dboxes_wrapper").css("display")=="none"){
        $("#dboxes_wrapper").css("display", "table");}
    $("#delete_confirm_dialog a.delete_apply").attr("href","javascript:document."+formname+".submit()");
    $("#delete_confirm_dialog input.delete_apply").attr("onClick","javascript:document."+formname+".submit()");
    $("#delete_confirm_dialog").show();
}
$(".cancel_delete_confirm").live("click", function(){
    $("input[name=action]").val("");
    $("#delete_confirm_dialog .delete_apply").attr("href","");
    $(".dialogbox").hide();
    $("#dboxes_wrapper").hide();
});

// helper
$(".helper").live("mouseenter mouseleave", SwitchHelperContent);
function SwitchHelperContent(Event){
    $(this).children(".helper_content").toggle();
}

// select widget
$(".choose_item").live("click", function(){
    itemvalue = $(this).attr("value");
    valuetarget = $(this).attr("valuetarget");
    itemname = $(this).attr("name");
    nametarget = $(this).attr("nametarget");
    $(this).parent().children(".choose_item").removeClass("active");
    $(this).addClass("active");
    $("#"+nametarget).html(itemname);
    $("#"+valuetarget).val(itemvalue);
    submenu = $(this).parent().parent().children(".choose_submenu");
    submenu.children().hide();
    submenu.children("#"+itemvalue+"_submenu").show();
});

//sections_menu
$("li.opened div.switcher").live("click", function(Event){
    Event.preventDefault();
    $(this).parent().children("ul").stop(true,true).slideUp("fast");
    $(this).parent().removeClass("opened").addClass("closed");
});
$("li.closed div.switcher").live("click", function(Event){
    Event.preventDefault();
    $(this).parent().children("ul").stop(true,true).slideDown("fast");
    $(this).parent().removeClass("closed").addClass("opened");
});
$(document).ready(function(){
    $("li.opened").children("ul").hide().show();
});

//carousel
$(function(){
    var obj = $(".carousel");
    var wrapper = $(".carousel .carousel_wrapper");
    var content = $(".carousel .carousel_content");
    var content_height = content.height();
    var wrapper_height = wrapper.height();

    if (wrapper_height < content_height){
        var active_link = $(".carousel .active");
        if (active_link.length > 0) {
        var top_offset = active_link.offset().top - wrapper.offset().top;
            if (top_offset > wrapper_height/2) {
                if (content_height - top_offset - active_link.height()/2 < wrapper_height/2) {
                    content.css("top",wrapper_height - content_height);
                }
                else {
                    content.css("top", wrapper_height/2 - top_offset - active_link.height()/2);
                }
            }
        }
        $(".carousel .button_down").bind("mouseenter", function(){
            content.stop().animate({top : wrapper_height-content_height}, (content_height-wrapper_height+parseInt(content.css("top")))*8);
        });
        $(".carousel .button_up").bind("mouseenter", function(){
            content.stop().animate({top : 0}, -parseInt(content.css("top"))*8);
        });
        $(".carousel .button_up, .carousel .button_down").bind("mouseleave", function(){
            content.stop();
        });
    }
    else {
        wrapper.css("height",content_height);
        $(".carousel .button_up, .carousel .button_down").hide();
    }
});

/* thing_preview */
$(function(){
    $("#photos_line div.image_preview").click(function(){
        url = $(this).attr("href");
        $("#thing_image img").attr("src", url);
    });
});

/* counter */
$(".counter .plus").live("click",function(Event){
    Event.preventDefault();
    var target = $(this).prev("input");
    var value = Number(target.val());
    if (isNaN(value)) {
        target.val("1");
    }
    else {
        if (value < 1) {
            target.val("1");
        }
        else {
            target.val(value+1);
        }
    }
});
$(".counter .minus").live("click",function(Event){
    Event.preventDefault();
    var target = $(this).next("input");
    var value = Number(target.val());
    if (isNaN(value)) {
        target.val("1");
    }
    else {
        if (value < 2) {
            target.val("1");
        }
        else {
            target.val(value-1);
        }
    }
});
$("input.int_count").live("keyup change", function(){
    intval = $(this).val().replace(/[^0-9]/gi,"");
    if (intval > 1) {
        if ($(this).val()!=intval) {
            $(this).val(intval);
        }
    }
    else {
        $(this).val(1);
    }
});

/* enter_to_chat */
$("#enter_to_chat").live("click", function(e){
    e.preventDefault();
    var chat_url = $(this).attr("href");
    window.showModalDialog(chat_url,"chat","dialogWidth=600px; dialogHeight=400px; center=yes; resizable=no");
});

