var call = {Clear_Values:Clear_Values, Fade_Me_Out:Fade_Me_Out, Shadowbox_Me:Shadowbox_Me, Tab_Select:Tab_Select};

function areaincrease(thisTextareaID, add) {
    var thisTextarea = document.getElementById(thisTextareaID);
    var old_height   = thisTextarea.rows;
    if (3 < old_height) {
    } else {
        old_height = 3;
    }
	newHeight           = parseInt(old_height + add);
	thisTextarea.rows   = newHeight;
}

function areadecrease(thisTextareaID, subtract) {
	if (thisTextareaID) {
	    var thisTextarea = document.getElementById(thisTextareaID);
	    var old_height   = thisTextarea.rows;
		if (old_height > subtract) {
			newHeight                 = old_height - subtract;
		} else {
			newHeight = 3;
		}
		thisTextarea.rows = newHeight;
	}
}

function selectincrease(thisSelectID, add) {
    var thisSelect = document.getElementById(thisSelectID);
    var old_height   = thisSelect.size;
    if (5 < old_height) {
    } else {
        old_height = 5;
    }
    newHeight         = parseInt(old_height + add);
    thisSelect.size   = newHeight;
    thisSelect.className  = "active";
}

function selectdecrease(thisSelectID, subtract) {
	if (thisSelectID) {
	    var thisSelect = document.getElementById(thisSelectID);
	    var old_height   = thisSelect.size;
		if (old_height > subtract) {
			newHeight                 = old_height - subtract;
		} else {
			newHeight = 5;
		}
		thisSelect.size = newHeight;
		thisSelect.className = "active";
	}
}

function find_object (id_name, tag, doc) {
    if (doc) {
        var tag_elements = doc.document.getElementsByTagName(tag);
    } else {
        var tag_elements = document.getElementsByTagName(tag);
    }
    for (var i = 0; i < tag_elements.length; i++) {
        var tag_element = tag_elements[i];
        if (tag_element.id == id_name) {
            return tag_element;
        }
    }
    return false;
}

function submit_this_form(form_id) {
    var tag_elements = document.getElementsByTagName('form');

    for (var i = 0; i < tag_elements.length; i++) {
        var tag_element = tag_elements[i];
        if (form_id == tag_element.id
        ) {
            return tag_element.submit();
        }
    }
}

var Ajax_Error          = 'Sorry! Something went wrong!';
jQuery.fn.exists = function(){return jQuery(this).length>0;}
function Main(Site, myForm) {
    var my_form = '';
    if ('' != myForm) {
        my_form = $('#'+myForm).serialize();
    }
    var my_args = '';
    if (Main.arguments.length > 2) {
        for (var args_count = 0; args_count < Main.arguments.length; args_count++) {
            if (args_count > 1) {
                my_args += '&Param'+(args_count-1)+'='+Main.arguments[args_count];
            }
        }
    }
	$.ajax({type: "POST", async: true, url: Ajax_HandlerURL, dataType: "json",
		data: '&Mod=' + Site +  '&' + my_form+my_args,
		beforeSend: function() {
		},
		success: function(Data) {
			for (Block in Data.Block) {
				//> Block template Parsen
				var set = false;
				if ($('#'+Block).exists()) {
                    var myid = '#'+Block;
                    set = true;
				} else {
                    if ($("[name="+Block.replace('[', '\[').replace(']', '\]')+"]").exists()) {
                        var myid = $("[name="+Block.replace('[', '\[').replace(']', '\]')+"]").attr('id');
                        if ('' != myid) {
                            myid = '#'+myid;
                            set = true;
                        }
                    }
                    if (false == set) {
                        if ($(Block).exists()) {
                            var myid = Block;
                            set = true;
                        }
                    }
				}
				if (true == set) {
//				    alert(Block+'  '+myid);
    				if (Data.Block[Block].Data != undefined) {
            			$(myid).html('');
                        HTMLBlock = Data.Block[Block].Template;
    	    			$(myid).append(HTMLBlock);
    	    		}
    				if (Data.Block[Block].Append != undefined) {
                        HTMLBlock = Data.Block[Block].Template;
    	    			$(myid).append(HTMLBlock);
    	    		}
    				if (Data.Block[Block].Css != undefined) {
    				    $(myid).addClass(Data.Block[Block].Css);
    				}
    				if (Data.Block[Block].Cssre != undefined) {
    				    $(myid).removeClass(Data.Block[Block].Cssre);
    				}
    				if (Data.Block[Block].Jqueryre != undefined) {
    				    $(myid).unbind(Data.Block[Block].Jqueryre);
    				}
    				if (Data.Block[Block].Jquery != undefined) {
                        if (Data.Block[Block].Jqueryaddon != undefined) {
                            var code = eval('('+Data.Block[Block].Jqueryaddon+')');
    				        $(myid)[Data.Block[Block].Jquery](code);
    				    } else {
    				        $(myid)[Data.Block[Block].Jquery]();
    				    }
    				}
    				if (Data.Block[Block].Js != undefined) {
				        js_call(Data.Block[Block].Js, myid);
    				}
    				if (Data.Block[Block].Value != undefined) {
    				    $(myid).val(Data.Block[Block].Value);
    				    if ('hiddenarea' == $(myid).attr("class")) {
                			$(myid+'_show').html('');
    				        $(myid+'_show').append(Data.Block[Block].Value);
    				    }
    				}
    				if (Data.Block[Block].Href != undefined) {
    				    location.href = Data.Block[Block].Href;
    				}
    				if (Data.Block[Block].Reload != undefined) {
    				    location.reload();
    				}
    			}
			}
		}
	});
}

function js_call(p, arg) {
    if(typeof call[p] == 'function') call[p](arg);
}

function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

function Clear_Values(Block) {
    $(':input','#'+Block)
     .not(':button, :submit, :reset, :hidden')
     .val('')
     .removeAttr('checked')
     .removeAttr('selected');
}

function Fade_Me_Out (Block) {
    $('#'+Block).show();
    $('#'+Block).fadeOut(3000);
}

function Shadowbox_Me (Block) {
    Shadowbox.open({content:$('#'+Block).html(), player:'html'});
}

function Tab_Select(Block) {
    var tabContainers = $('div.tab-pane');
    tabContainers.find('div').removeClass('selected');
    tabContainers.find('div.tab-page').hide();
    location.hash = Block;
    $(Block).addClass('selected').show();
    tabContainers.find('a[href='+Block+']').parent().addClass('selected');
}

function js_check() {
    $('#error span.error_'+this.id).remove();
    if(this.value == '') {
        $('#error').show();
        $('#error span.error_'+this.id).remove();
        var my_error = '<span class="error_'+this.id+'"><b>$$$</b> darf nicht leer sein</span>';
        $('#error>span').append(my_error.replace('$$$', $('label[for='+this.id+']').html()));
    }
}

function beactive() {
    Main('isuser', 'me');
}

$().ready(function() {
    if($('#me').exists()) {
        setInterval(beactive, 300000);
    }

    if ($('#menuticker').exists()) {
        Main('ticker', 'me');
    }
});
