$(document).ready(function() {

	$("#top_line").wiggletize({
		elements	: 6,
		colors		: [ '#66ae00', '#b8cfed', '#a90097', '#e1eb08', '#66ae00', '#f0f0f0' ],
		alignment	: [ 30, 25, 20, 10, 10, 5]
	});
		
	$(".content_open_box").hide();
	
	$(".content_open_box_link").click(function() {
		
		var relation = $( this ).attr( 'rel' );
		$( '#content-box-' + relation ).slideToggle();			
		return false;
	});
	
	
	$(".lang img").hover(
		function(){
			$("."+$(this).attr("alt")).show();
		},
		function() {
			$("."+$(this).attr("alt")).hide();
		}
	);
	
});

function clearForm(oForm) {
		var elements = oForm.elements;
		oForm.reset();
		for(i=0; i<elements.length; i++) {
		field_type = elements[i].type.toLowerCase();
		switch(field_type) {
		  case "text":
		  case "password":
		  case "textarea":
		        case "hidden":  
		    elements[i].value = "";
		    break;
		  case "radio":
		  case "checkbox":
		     if (elements[i].checked) {
		        elements[i].checked = false;
		    }
		    break;
		  case "select-one":
		  case "select-multi":
		              elements[i].selectedIndex = -1;
		    break;
		  default:
		    break;
		}
    }
}

function reloadTimeList(nid,id) {
	$.post("/si/ajax/", { get_time_list: nid },
	function(data){
		//alert("Data Loaded: " + data);
		$("#term_time_"+id).html(data);
	});	
}


function NewsDivHtml() {
	if ($("#alert_news").size() > 0) {
		$("#alert_news").html('<img src="/static/images/ajax-loader.gif" />');
	}
	else {
		$("li[class='input']").append('<span id="alert_news"><img src="/static/images/ajax-loader.gif" /></span>');
	}
}

function NewsLogin(lang_id) {
	NewsDivHtml();
	$.post("/"+lang_id+"/ajax/", { news_login: $("input[name='news']").val() },
	function(data){
		$("#alert_news").html(data);	
	});	
}

function NewsLogout(lang_id) {
	NewsDivHtml();
	$.post("/"+lang_id+"/ajax/", { news_logout: $("input[name='news']").val() },
	function(data){
		$("#alert_news").html(data);	
	});	
}

function AppendTerm() {
	var content = "";

	var counter = ($("li[rev='clone']").size()/2) + 1;
	
	//alert(counter);
	var border = 1;
	$("li.copy-li").each(
		function() {
			var style="";
			if (border == 0) {
				style="style=\"border:none; padding-top:0px\"";
			}	
			
			content += "<li class=\"clontext\" rel=\""+counter+"\" rev=\"clone\" "+style+">";
			$formObjClone= $(this).clone(true);
			$formObjClone.find("li.copy-li").each(
				function(){
					$(this).attr("class","");		
							
				});
		
			$formObjClone.find("b").each(
				function(){
					$(this).show();			
				});	
				
			$formObjClone.find(".term-date").each(
				function(){
					//alert($(this).attr("onchange"));
					//alert(counter);
					//$(this).attr("onchange", "function onchange(event) { reloadTimeList(this.value, "+counter+"); }");
					//$(this).attr("onchange", "blabla");
					//$(this).attr("name", "tesssssss");
					$(this).attr("id", "term_date_"+counter);
					$(this).attr("rel", counter);
				});	
			
			$formObjClone.find(".term-time").each(
				function(){
					$(this).attr({"id"       : "term_time_"+counter});	
				});	
				
			$formObjClone.find("b a").each(
				function(){
					$(this).attr("rel", counter);	
					$(this).attr("href", "javascript: RemoveTerm("+counter+")");
				});
				
			$(".clone b").show();				
			//$(this).clone() = $(this).attr("class","");
			content += $formObjClone.html();
			content += "</li>";
			border = 0;
		});
	
	$("li[rev='clone']:last").after(content);	
	
	$(".remove-term").show();
	
	$(".append-new").hide();
	$(".append-new:last").show();
	
	
	$(".b-hide-line").hide();
	$(".b-hide-line:last").show();
	
	
	$(".term-date").change(
		function(){
			reloadTimeList($(this).val(),$(this).attr("rel"));
		});
	
	//alert($(".term-date:last").val());
	//alert($(".term-time:last").attr("rel")); 	
	reloadTimeList($(".term-date:last").val(),$(".term-date:last").attr("rel"));
	//alert(content);
}

function RemoveTerm(id) {
	//var classDiv  = $("li[rel='"+id+"']").attr("class");
	$("li[rel='"+id+"']").remove();	
	/*if (classDiv == "copy-li") {
		$("li[rev='clone']:first").attr({"class":"copy-li", "rel" : "1"})
		 						  .find(".remove-term").each(
								   function(){
										//$(this).attr({"rel":"1", "href": "javascript:RemoveTerm(1)"});
									});
		$("li[rev='clone']:second").attr({"class":"copy-li", "rel" : "1"})
		 						   .find(".remove-term").each(
									function(){
										//$(this).attr({"rel":"1", "href": "javascript:RemoveTerm(1)"});
									});
		
		
	}*/
	//alert(id);
	
	//alert($("li[class='clone']").size());	
	if ($("li[rev='clone']").size() == 2) {
		$(".b-hide").hide();
	}
	
	
	var counter = 1;
	var counterAll = 0;
	
	$("ul").find("li[rev='clone']").each(
		function(){
			if (counter == 1) {
				$(this).attr("class", "copy-li");
			}
			else {
				$(this).attr("class", "clontext");	
				
				if (counterAll%2 != 0) {
					$(this).css({"border":"none",
								 "padding-top":"0px"});
				}
				
			}
			$(this).attr("rel", counter);
			$(this).find(".b-hide").each(
				function(){
					$(this).attr("href","javascript:RemoveTerm("+counter+")");
				});
			
			$(this).find(".term-date").each(
				function(){
					$(this).attr({"id"  : "term_date_"+counter,
								  "rel" : counter});	
				});	
			
			$(this).find(".term-time").each(
				function(){
					$(this).attr({"id"       : "term_time_"+counter});	
				});	
				
				
			///alert(counter);
				
			
			counterAll++;
			if (counterAll%2 == 0) {
				counter++;
			}
		});
	
	$(".append-new").hide();
	$(".append-new:last").show();
	
	/*$(".append-new").hide();
	$(".append-new:last").show();*/
	
	$(".b-hide-line").hide();
	$(".b-hide-line:last").show();
	
	$(".remove-term").show();
	if ($("li[rev='clone']").size() > 2) {
		$(".append-new:first").hide();
		$(".b-hide-line:first").hide();
	}
	else {
		$(".remove-term").hide();
		$(".b-hide-line").hide();
		
	}	
}

$(function() {
	$(".term-date").change(
		function(){
			reloadTimeList($(this).val(),$(this).attr("rel"));
		});
});


/**
 * iframe auto height
 */
 /*
$(document).ready(function()
	{
		// Set specific variable to represent all iframe tags.
		var iFrames = document.getElementsByTagName('iframe');

		// Resize heights.
		function iResize()
		{
			// Iterate through all iframes in the page.
			for (var i = 0, j = iFrames.length; i < j; i++)
			{
				// Set inline style to equal the body height of the iframed content.
				iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';
			}
		}

		// Check if browser is Safari or Opera.
		if ($.browser.safari || $.browser.opera)
		{
			// Start timer when loaded.
			$('iframe').load(function()
				{
					setTimeout(iResize, 0);
				}
			);

			// Safari and Opera need a kick-start.
			for (var i = 0, j = iFrames.length; i < j; i++)
			{
				var iSource = iFrames[i].src;
				iFrames[i].src = '';
				iFrames[i].src = iSource;
			}
		}
		else
		{
			// For other good browsers.
			$('iframe').load(function()
				{
					// Set inline style to equal the body height of the iframed content.
					this.style.height = this.contentWindow.document.body.offsetHeight + 'px';
				}
			);
		}
	}
);
 */












