var autosave = true; 
var autoload = true;

$(document).ready(function(){
    if (autoload) {
        if (window.baseWidgetConf !== undefined) {
            var data = baseWidgetConf;
            loadWidgetsByConf(data);
        } else {
            wStr = "/widgets/getConf/";
            if (window.customWidgetConf !== undefined) wStr+=customWidgetConf+"/";
            $.getJSON(wStr,loadWidgetsByConf);
        }
    }
    $("#widget_plus").click(function(){if ($("#indicator.dark").length != 0) {$(this).removeClass("close");$("#indicator").removeClass("dark");} else {$(this).addClass("close");$("#indicator").addClass("dark");}$("#widget_layer").slideToggle();});
    $("#id_quicksearch").focus(function(){if ($(this).val()=="Suche") $(this).val("");});
    $("#id_quicksearch").blur(function(){if ($(this).val()=="") $(this).val("Suche");});
    $("#quicksearch_submit").click(submitQuickSearch);
    
    $(".eventset_item.detail .img a").lightBox({
        txtImage:"",txtOf:"von",overlayBgColor: "#fff",imageBlank: '/media/img/lightbox/lightbox-blank.gif',
        imageLoading: '/media/img/lightbox/lightbox-ico-loading.gif',
	imageBtnClose: '/media/img/lightbox/lightbox-btn-close.gif',
	imageBtnPrev: '/media/img/lightbox/lightbox-btn-prev.gif',
	imageBtnNext: '/media/img/lightbox/lightbox-btn-next.gif'
    });
});
function getById(id) {
    $("#result").hide().html("<img src='/media/img/loading.gif' alt='please wait'/>").fadeIn();
    $.getJSON("/events/"+id+".json/",function(data) {
        $("#result").hide().html(data[0].fields["title"]).fadeIn();
    })
}
function loadWidget(id,target,position) {
    var loading = getLoadingWidget();
    $("div#col_"+target).append(loading);
    $.get('/widgets/'+id+'/',function(data){
        loading.replaceWith(data);
        //if ($.browser.msie) {
          //$("#grid-item_"+id+" .grid-item-content").css("position","relative");
          //$("#grid-item_"+id+" .grid-item-content div.corner").css({'background-color' : 'yellow', 'width' : '40px','height':'40px'})
        //}
    });
}
function loadWidgetsByConf(data) {
    var col;var pos;
    for (col=0;col<data.length-1;col++) {
        if ($("#col_"+(col+1)).length == 0) continue;
        thisCol = data[col];
        for (pos=0; pos<thisCol.length;pos++) {
            loadWidget(thisCol[pos],col+1,pos);
        }
    }
}
function getWidget(id,replace_element) {
    $.get('/widgets/'+id+'/',function(data){
        replace_element.replaceWith(data);
        updateConf();
    });
}
function getLoadingWidget() {
    return $("<div class='grid-item loading'><h2 class='grid-item-header loading'>&nbsp;</h2><img src='/media/img/loading_circle.gif' alt='bitte warten'/></div>");
}
function getWideLoadingWidget() {
    return $("<div id='col_wide' class='grid-item grid-col loading'><h2 class='grid-item-header loading'>&nbsp;</h2><br/><img src='/media/img/loading_circle.gif' alt='please wait'/></div>");
}
function updateConfOverall(ev,ui) {
    $("#indicator").addClass("loading");
    $.post("/widgets/setConfOverall/",{ "conf" : getOrder(),"title":window.prompt("Name für Konfiguration") },function(data) {
        $("#indicator").removeClass("loading");
        alert("Neue Konfiguration wurde gespeichert.");
    });
}
function addedUpdateConf(ev,ui) {
    //var element_id = $(ui.item).attr("id").split("inactive_")[1];
    //var element_id = $(this).attr('id').split("inactive_")[1];
    var widget_id = element_id.split("_")[1];
    var loading = getLoadingWidget()
    //$(ui.item).replaceWith(loading);
    //$(this).replaceWith(loading);
    getWidget(widget_id,loading);
}
function receiveUpdateConf(ev,ui) {
    var element_id= $(ui.item).attr("id");
    var type = element_id.split("_")[0];
    if (type == "grid-item") return;
    var widget_id = element_id.split("_")[2];
    var loading = getLoadingWidget();
    //$("#"+element_id).replaceWith(loading);
    $(this).find("#"+element_id).replaceWith(loading);
    getWidget(widget_id,loading);
}
function updateConf(ev,ui) {
    if (autosave==false) {return;}
    $("#indicator").addClass("loading");
    $.post("/widgets/setConf/",{ "conf" : getOrder() },function(data) {
        $("#indicator").removeClass("loading");
    });
}
function getOrder() {
    var orderInfo = "";
    $(".grid-col").each(function(){
        var order = $(this).sortable("serialize");
        orderInfo = orderInfo + order.split("&") + ";";
    });
    return orderInfo;
}
function switchLayoutWide() {
    $("#content_widgets").attr("id","content_wide").prepend("<div id='col_wide'/>");
    $("#col_1, #col_2").remove();
}
function switchLayoutWidePrepend(data) {
    $("#content_widgets").attr("id","content_wide");
    $("#col_1, #col_2").remove();
    $("#content_wide").prepend(data);
}
function submitQuickSearch() {
    if ($("#id_quicksearch").val() == "Suche") return false;
    if ($("#id_quicksearch").val() == "") return false;
    if ($("#col_wide").length==0) switchLayoutWide();
    $("#col_wide").replaceWith(getWideLoadingWidget());
    $.get("/search/quick/",$("#quicksearch").serialize(),function(data){
        $("#col_wide").replaceWith(data);
    });
    return true;
}

