var profils = new Array();
profils[0] = "";
var errors = new Array();
var error = false;

function checkBroadcast(element){
	jQuery("#change-profils").attr('disabled', 'disabled');	
	
	var element = jQuery(element);
	element.val(jQuery.trim(element.val()));
	
	var tr	= jQuery(element).parents("tr");
	
	var broadcastID = element.val();
	var hosterID	= tr.attr("id");
	
	var image = jQuery("#check_" + hosterID);
	image.css("opacity", "1.0");
	image.attr({"src" : "http://broadcast.fulltimegeek.de/images/ajax-loader.gif", "title" : "checking"});
	
	if(profils[hosterID] == broadcastID){
		errors[hosterID] = false;
		image.attr({"src" : "http://broadcast.fulltimegeek.de/images/registration/bg-fieldset-good.png", "title" : "Thats your current Profil!"});
		checkForm();
		return true;
	}	
	
	
	jQuery.ajax({
		type: "POST",
		url: "http://broadcast.fulltimegeek.de/inc/functions/checkBroadcastID.php",
		dataType:"html",
		async: false,
		data: {hosterID: hosterID, broadcastID: broadcastID},
		success:function(data){
			if(data == 0) {
				//cant find channel
				errors[hosterID] = true;
				image.attr({"src" : "http://broadcast.fulltimegeek.de/images/registration/bg-fieldset-not-okay.png", "title" : "ID not avaible"});
			} else if (data == 1) {
				//taken by someone else
				errors[hosterID] = true;
				image.attr({"src" : "http://broadcast.fulltimegeek.de/images/registration/bg-fieldset-okay.png", "title" : "Taken by someone else!"});
			} else if (data == 2) {
				//all cool
				errors[hosterID] = false;
				image.attr({"src" : "http://broadcast.fulltimegeek.de/images/registration/bg-fieldset-good.png", "title" : "This works!"});
			}
		},
		error:function(xhr, ajaxOptions, thrownError){
			errors[hosterID] = true;
			image.attr({"src" : "http://broadcast.fulltimegeek.de/images/registration/bg-fieldset-not-okay.png", "title" : "Error! Script not avaible!"});
		}   
	});
	checkForm();
}

function storeProfil(element){
	var broadcastID = element.value;
	
	var tr			= jQuery(element).parents("tr");
	var hosterID	= tr.attr("id");
	
	profils[hosterID] 	= broadcastID;
	errors[hosterID] 	= false;
}

function changeStatus(element){
	var element 	= jQuery(element);
	var tr			= jQuery(element).parents("tr");
	
	var status		= element.prev();
	var img_class 	= element.attr("class");
	
	var desc = tr.find(".status-desc")
	
	if (img_class == "active") {
		element.attr({ "src" : "http://broadcast.fulltimegeek.de/images/page/square-red.png"});
		element.removeClass("active");
        element.addClass("inactive"); 
		desc.html("inactive");
		
		status.val("0");
	} else if(img_class == "inactive") {
		active = jQuery(".active");
		active.attr({ "src" : "http://broadcast.fulltimegeek.de/images/page/square-red.png"});
		active.removeClass("active");
        active.addClass("inactive");
		active.prev().val("0");
		active.parents("tr").find(".status-desc").html("inactive");

		element.attr({ "src" : "http://broadcast.fulltimegeek.de/images/page/square-green.png"});
		element.removeClass("inactive");
        element.addClass("active");
		desc.html("active");

		status.val("1");		
	}
	checkForm();
}

function checkForm(){
	var tmp = false;
	for(var i in errors){
		tmp = (tmp || errors[i]);
	}
	
	error = tmp;

	if(!error) {
		jQuery("#change-profils").attr('disabled', '');
	}
}


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function storeProfils(){
	errors[0] = false;
	
	var inputs = jQuery(".channelID input");
	for (var i=0; i<inputs.length; i++){
		storeProfil(inputs[i]);
	}
}

addLoadEvent(storeProfils);


$(document).ready(function() {
	// Show
	$('#toppanel').show();
	
	// Expand Panel
	$("#open").click(function(){
		$("div#panel").slideDown("slow");
	
	});	
	
	// Collapse Panel
	$("#close").click(function(){
		$("div#panel").slideUp("slow");	
	});		
	
	// Switch buttons from "Log In | Register" to "Close Panel" on click
	$("#toggle a").click(function () {
		$("#toggle a").toggle();
	});
	
	
	$("#change-profils").click(function() {
		jQuery("#change-profils").attr('disabled', 'disabled');
		
		if(!error){
			var post_string = jQuery("#profils-form").serialize();
			
			$.post("http://broadcast.fulltimegeek.de/inc/functions/profils.php", post_string, 
				function(data){
					popup('Your profil has been changed!');
					$("#change-profils").attr('disabled', '');
				}
			);
		}
	});
		
	$("a.toggler").click(function() {
		$(this.hash + "_TOG").slideToggle();
	});
	
});
