

$(document).ready(webinterface_init);

function webinterface_init() {

	/* tooltips initialisieren */
	$(".help").tiptip();
	
	/* standard accordion initialisieren */
	$(".accordion").accordion({
						autoHeight : false,
						header: 'h3',
						active: false,
						collapsible: true,
						icons: { 'header': 'ui-icon-plus', 'headerSelected': 'ui-icon-minus' }
					});


	/* accordion initialisieren */
	$("#accordion").accordion({
						autoHeight : false,
						header: 'legend',
						icons: { 'header': 'ui-icon-plus', 'headerSelected': 'ui-icon-minus' }
					});
	
	/* closed accordion initialisieren */
	$("#accordion_closed").accordion({
						autoHeight : false,
						header: 'legend',
						active: false,
						collapsible: true,
						icons: { 'header': 'ui-icon-plus', 'headerSelected': 'ui-icon-minus' }
					});

	$(".accordion").accordion({
		active: $("#active").index()
	});

	/* price tabs - intercept var */
	if ($("#prices").length)
	{
		if ($("#prices").attr("class").length)
		{
			var active_pricetab = '#prices_'+$("#prices").attr("class");
			$("#prices").tabs();
			$("#prices").tabs('select',active_pricetab);
		}
		else
		{
			$("#prices").tabs();
		}
	}
	/* tabs - intercept var */
	if ($("#tabs").length)
	{
		if ($("#tabs").attr("class").length)
		{
			var active_tab = $("#tabs").attr("class");
			$("#tabs").tabs();
			$("#tabs").tabs('select',active_tab);
		}
		else
		{
			$("#tabs").tabs();
		}
	}

	// datepicker initialisieren
	$(".datepicker").datepicker({ 
		dateFormat: 'yy-mm-dd', 
		buttonImage: '/img/date.gif',
		buttonImageOnly: true,
		//showOtherMonths: true,
		//selectOtherMonths: true,
		showButtonPanel: true,
		showWeek: true, 
		firstDay: 1
	});

	// external links handling as target _blank to avoid invalid xhtml - dirty hack!
	$('a[href^=http]').attr('target', '_blank');


	// IE fix for select box width
    if ($.browser.msie) 
	{
		$('select').focus(function() {
			var optionsLength = 0;
			$(this).children('option').each(function() {
				if ($(this).text().length > optionsLength) {
					optionsLength = $(this).text().length;
				}
			});
			if (optionsLength > 50)
			{
				$(this).parent().addClass('xlarge');
			}
		});
		$('select').blur(function() {
			$(this).parent().removeClass('xlarge');
		});
	}


	// startseite
	$("div.promoimage").mouseenter(function(){$(this).slideUp(100);});	
	$("div.text").mouseleave(function(){$(this).prev().slideDown(100);});

	// login form
	$('#login_username').focus(function(){ 
		$(this).prev().hide();	
	});
	$('#login_username').blur(function(){
		if ($('#login_username').val() == "") 
		{
			$(this).prev().show();	
		}
	});
	if ($('#login_username').val() != "") 
	{
		$('#login_username').prev().hide();
	}
	$('#login_password').focus(function(){ 
		$(this).prev().hide();	
	});
	$('#login_password').blur(function(){
		if ($('#login_password').val() == "") 
		{
			$(this).prev().show();	
		}
	});
	if ($('#login_password').val() != "") 
	{
		$('#login_password').prev().hide();
	}

	// whois feld auf focus leeren und auf blur wieder fuellen
	$('#whois_domain').focus(function(){ $(this).attr("value","");});
	$('#whois_domain').blur(function(){ 
		if ($(this).val() == "") 
		{
			$(this).attr("value","whois");
		}
	});

	// input felder mit * auf focus leeren
	$('input').focus(function(){ 
		if ($(this).val() == "*")
        {
			$(this).attr("value","");
		}
	});


	// gewisse objekte verstecken
	$('.js_hideme').hide();


	/* LISTEN - START*/

    
	/* create and hide check all button for zone-block */
	$('#zoneslegend').prepend('<span id="checkall" style="float:right;font-weight:normal;">check all</span>');
	$('#zoneslegend').find("#checkall").hide();
	
	$("legend").click(function(){
		if($("#zoneslegend").length) {
			if ($("#zoneslegend").hasClass('ui-state-active')) {
				$("#checkall").show();
			}
			else {
				$("#checkall").hide();		
			}
		}
	});

	// check all - if only one per page 
	$("#checkall").click(function(){
		if ($(this).hasClass('checked')) {
			$(this).removeClass('checked');
			$(".checkme").attr('checked','');
		}
		else {
			$(this).addClass('checked');
			$(".checkme").attr('checked','checked');
		}
    });
	
	// check all switches for pricing form due to more than one forms per page
	var services = ['trustee','application','certificate','mobile','server','subreseller','webspace','dns','ram','other'];
	for (var i in services) {
		var checkId = "#checkall_" + services[i];
		$(checkId).click(function(e){
			var checkClass = e.target.id.replace(/checkall/,".checkme");
			if ($(this).hasClass('checked')) {
				$(this).removeClass('checked');
				$(checkClass).attr('checked','');
			}
			else {
				$(this).addClass('checked');
				$(checkClass).attr('checked','checked');
			}
		});
	};


	// pageselector
	$('#pageselector').change(function() {this.form.submit();});
	/* LISTEN - ENDE */


	/* ADD FORMS - START */
	// additional contacts on add forms
	$('.additional').hide();
	if ($('#section').val() == "domain") 
	{
		$('.add_arrow').find("label").addClass('arrow_right');	
	}
	$('.arrow_right').click(toggleAdditionalContacts);
		$('span.infolink').hide();	

	// contact lists in add forms
	$('.contactlist').change(function() {
		$(this).prev().attr("value",$(this).val());
	});

	// use owner flag in add forms
	$('.use_owner').click(function() {
		if ($(this).is(':checked')) 
		{
			$(this).parent().prev().find("input").attr("value",$('#ownercontact0').val());
			$(this).parent().prev().find("input").attr("readonly","readonly");
		}
		else
		{
			$(this).parent().prev().find("input").attr("value","");
			$(this).parent().prev().find("input").attr("readonly","");
		}
	});

	if ($('#target_type').length) 
	{
		if($('#target_type').val() != "forwarding")
		{
			$('#target-web').parent().hide();
		}
		if($('#target_type').val() != "nameserver")
		{
			$('input[name*="nameserver"]').parent().hide();
		}
	};

	/* DOMAINWIZZARD - START */
	$('#target_type').change(function() {
		if ($(this).val() == "forwarding")
		{
			$('#target-web').parent().slideDown();
			$('input[name*="nameserver"]').parent().slideUp();
		}
		else if ($(this).val() == "NIL")
		{
			$('#target-web').parent().slideUp();
			$('input[name*="nameserver"]').parent().slideUp();
		}
		else
		{
			$('#target-web').parent().slideUp();
			$('input[name*="nameserver"]').parent().slideDown();
		}
	});
	/* DOMAINWIZZARD - ENDE */

	/* ADD FORMS - ENDE */

	// help in forms
	$('.info').hide();
	$('.info').append('<img src="/img/x.png" class="close help" alt="close help" title="close help">');
	$('.info').prev().append('<span class="infolink"><img src="/img/help.png" class="show_help" alt="show help"></span>');
	$('span.infolink').hide();
	$('.info').prev().mouseenter(function() { $(this).children('span:last').show();	});
	$('.info').prev().mouseleave(function() { $(this).children('span:last').hide();	});
	$('.show_help').click(function() {
		$(this).parent().parent().next().slideDown(100);
		$(this).parent().hide();
	});
	$('.close').click(function() { $(this).parent().slideUp(100);	});
	
	// multiline fields
	$('div.jshidden').hide();
	$('div.jshidden').append('<img src="/img/x.png" class="removebutton" alt="remove line">');
	if ($('div.jshidden').prev().hasClass('info'))
	{
		$('div.jshidden').prev().prev().append('<img src="/img/add.png" class="addbutton" alt="add line">');
	}
	else
	{
		$('div.jshidden').prev().append('<img src="/img/add.png" class="addbutton" alt="add line">');
	}
	
	$('.addbutton').click(function () {
		$(this).hide();
		$(this).prev('.removebutton').hide();
		$(this).parent().nextAll('div.jshidden:first').show();
	});
	
	$('.removebutton').click(function () {
		$(this).parent().hide();
		if ($('div.jshidden').prev().hasClass('info'))
		{
			$(this).parent().prev().prev().find('.addbutton').show();
			$(this).parent().prev().prev().find('.removebutton').show();
		}
		else
		{
			$(this).parent().prev().find('.addbutton').show();
			$(this).parent().prev().find('.removebutton').show();
		}
	});

	// country selectors
	$('#cc_selection').change(function() {
		$(this).prev().attr("value",$(this).val());
	});

	$('select[name*="country"]').each(function() {
		$(this).before('<input type="text" class="xsmall" name="'+$(this).attr('name')+'" style="margin-left:10px;" value="'+$(this).val()+'">');
		$(this).removeAttr('name');
		$(this).change(function() {
			$(this).prev().attr("value",$(this).val());
		});
	});
	$('input[name*="country"]').focus(function() {
		$('#'+$(this).attr('name')+'_warning').fadeOut().queue(function() {
				$(this).remove();
		});
	});
	$('input[name*="country"]').blur(function() {
		if ($('#' + $(this).attr('name') + ' option[value="' + $(this).val().toUpperCase() + '"]').length)
		{
			$(this).next('select').val($(this).val().toUpperCase());
		}
		else
		{
			$(this).parent().append('<span id="' + $(this).attr('name') + '_warning" class="red">"' + $(this).val() + '" is not a valid country code!</span>');
			$(this).attr('value','');
		}
	});

	// autocomplete for subreseller form
	if ($("input[name='subreseller']").length)
	{
		$("input[name='subreseller']").autocomplete({source: ac_data});
	}

	// toggle target for push action only when delete domain
	if ($("input[name='command']").val() == "deletedomain") {
		$("#target").parent().hide();
		$("#action").change(function() {
			if($("#action").val() == "PUSH")
			{
				$("input[name='target']").parent().show();
			}
			else
			{
				$("#target").parent().hide();
			}
		});
	}

	// request authcode 
	if ($("input[name='auth']").length) 
	{
		$("input[name='authaction']").parent().hide();
		$("input[name='type']").change(function() {
			if ($(this).val() == "1") {
				$("input[name='authaction']").parent().slideDown();
				$("input[name='authaction']").removeAttr("disabled");
			};
			if ($(this).val() == "2") {
				$("input[name='authaction']").parent().slideUp();
				$("input[name='authaction']").attr("disabled", true);
			};
		});
	}

	// pricing form

	if ($("button[name='add']").length) 
	{
		$('button[name="delete"]').click(function() {
			$(this).parent().parent().remove();
		});
		
		$('button[name="history"]').each(function() {
			$(this).remove();
		});
	}

	$("div.legend li.scale").prepend('<input class="checkbox" type="checkbox" checked="checked" id="hide_scale">&nbsp;');
	$("div.legend li.purchase").prepend('<input class="checkbox" type="checkbox" checked="checked" id="hide_purchase">&nbsp;');
	$("#hide_scale").click(function () {
		$("div.body li.scale").toggle();
		if ($(this).attr("checked")) {
			if ($("#hide_purchase").attr("checked")) {
            	$('div.pricescale .body div').css("height","52px");
            	$('div.pricescale .body .zone').css("margin-top","");
			}
			else {
            	$('div.pricescale .body div').css("height","38px");
            	$('div.pricescale .body .zone').css("cssText","margin-top: -7px !important; height: 38px");
			}
			$('button.history').show();
			$('button.delete').show();
         }   
         else {
			if ($("#hide_purchase").attr("checked")) {
            	$('div.pricescale .body div').css("height","38px");
            	$('div.pricescale .body .zone').css("cssText","margin-top: -7px !important; height: 38px");
				$('button.history').show();
				$('button.delete').show();
			}
			else {
            	$('div.pricescale .body div').css("height","30px");
				$('button.history').hide();
				$('button.delete').hide();
			}
         }
	});
	$("#hide_purchase").click(function () {
		$("div.body li.purchase").toggle();
		if ($(this).attr("checked")) {
			if ($("#hide_scale").attr("checked")) {
            	$('div.pricescale .body div').css("height","52px");
            	$('div.pricescale .body .zone').css("margin-top","");
			}
			else {
            	$('div.pricescale .body div').css("height","38px");
            	$('div.pricescale .body .zone').css("cssText","margin-top: -7px !important; height: 38px");
			}
			$('button.history').show();
			$('button.delete').show();
         }   
         else {
			if ($("#hide_scale").attr("checked")) {
            	$('div.pricescale .body div').css("height","38px");
            	$('div.pricescale .body .zone').css("cssText","margin-top: -7px !important; height: 38px");
				$('button.history').show();
				$('button.delete').show();
			}
			else {
            	$('div.pricescale .body div').css("height","30px");
            	$('div.pricescale .body .zone').css("margin-top","");
				$('button.history').hide();
				$('button.delete').hide();
			}
         }
	});

	$('#epp_list').change(function() {this.form.submit();});

	$('input.check_for_write').click(function () {
		if ($(this).attr("checked") && !$(this).parents('.options').find('input.check_for_read').attr("checked")) {
			$(this).parents('.options').find('input.check_for_read').attr("checked","checked");
		}
	} );

     //AUTO COLLAPSE'S TOGGLED ELEMENT
     $(".toggle_object").hide();
 
     //CHANGES THE BUTTON FROM OPEN TO CLOSED AND VIS-VERSA
     $("a.toggle_head").toggle(function(){
         $(this).addClass("active");
     }, function () {
         $(this).removeClass("active");
     });
 
     //SLIDES CONTAINER UP AND DOWN ON CLICK
     $("a.toggle_head").click(function(){
         $(this).parent().parent().next().find(".toggle_object").slideToggle("slow");
         $(this).next(".toggle_object").slideToggle("slow");
     });

	// ADDITIONAL LINES IN TABLES ARE SUPPOSED TO HAVE THE SAME COLOR AS THE ORIGINAL LINE
	$("tr.additional_table_row").each(function(){
		if ($(this).prev("tr").hasClass("odd")) {
			$(this).addClass("odd");
		};
	});	

	// CLEAN UP LIST SEARCH HEADER
    $('.cleanup_searchhead').css("width","370px");
	$('.cleanup_searchhead').append('<img src="/img/x.png" class="cleanup" alt="cleanup" title="cleanup">');
	$('img.cleanup').css("margin","6px 0 0 3px");
	$('img.cleanup').click(function() {
		$('select[name="type"]').attr("value","");
		$('#desc').attr("value","");
		$('form.listsearchhead').submit();
	});

	// IDN-CONVERT
	$('#domain_selection').change(function() {this.form.submit();});

	// PASSPHRASE
	$('#passphrase').parent().append('<div class="sameowner" id="optbox"><input type="checkbox" id="opt-out" class="restrict" /><label for="opt-out" style=" line-height: 16px;">OPT-OUT</label></div><div class="clear"></div>');
    $('#passphrase').css('float','left').css('margin','4px 10px 0 0');
    if ($('#passphrase').val() == 'OPTOUT') {
        $('#passphrase').hide();
        $('#opt-out').attr('checked','checked');
    }
    $('#opt-out').click(function(){
        if ($(this).attr("checked")) {
            $('#passphrase').attr('value','OPTOUT');
            $('#passphrase').hide();
        }
        else
        {
            $('#passphrase').show();
            $('#passphrase').attr('value','');
        }
    });
 
}

function toggleAdditionalContacts () {
	if ($(this).hasClass('arrow_right')) 
	{
		$(this).parent().nextAll('.additional:lt(2)').show();
	}
	else 
	{
		$(this).parent().nextAll('.additional:lt(2)').hide();
	}
	$(this).toggleClass('arrow_right');
	$(this).toggleClass('arrow_down');
}

