// JavaScript Document

/*
$(document).ready(function(){
	
	alert($(".article .a1101imagetext").length);
	
	$(".article .a1101imagetext").css( "color", "red" );	
});*/

function toggleFontSize(el) {
	var article_el = $("#article");
	var largeFontTxt = 'Stor Skrift';
	var size;
	if ( el.html() == largeFontTxt ) {
		el.html('Alm. Skrift');	
		article_el.attr( 'className', 'article large-font' );
		size = 'large';
	} else {
		el.html( largeFontTxt );
		article_el.attr( 'className','article' );
		size = 'normal';
	}
	
	$.get( 'toggleFontSize.dsp', { size: size } );
}

function awardResetAll() {
	show_status();
	
	var frm = document.forms['award_form'];
	
	var yearSel = frm.elements['year'];
	yearSel.selectedIndex = 0;
	
	var priceSel = frm.elements['award'];
	priceSel.selectedIndex = 0;
	
	var stringField = frm.elements['search_string'];
	stringField.value = 'Skriv her';
	
}



function awardSearchString( obj, root_id) {
	var str = obj.value;
	awardResetAll();
	$("#award-search-result").load( 'searchString.dsp', { root_id: root_id, search_string: str } );
}

function awardSearchStringOnKeypress(obj, e,root_id ) {
	if ( e ) {
		if(window.event) {
         	key = window.event.keyCode;     //IE
		} else {
			key = e.which;     //firefox
		}
		
		if(key == 13) {
			var str = obj.value;
			awardResetAll();
			
			$("#award-search-result").load( 'searchString.dsp', { root_id: root_id, search_string: str } );
			return false;
		} else {
			return true;	
		}
	} 
}

function awardSearchYear(obj,root_id) {
	var year = obj.options[ obj.selectedIndex ].value;
	if ( year != -1 ) {
		awardResetAll();
			
		$("#award-search-result").load( 
			'searchYear.dsp', { 
				root_id	: root_id, 
				year	: year 
			}
		);
	}
}

function show_status() {
	$("#award-search-result").html( '<div class="status" style="float:left;clear:both;width:100%">Henter data...</div>' );
}

function awardSearchPrize(obj,root_id) {
	var award_id = obj.options[ obj.selectedIndex ].value;
	if ( award_id != -1 ) {
		awardResetAll();
		
		$("#award-search-result").load(
			'searchAward.dsp', {
				root_id 	: root_id,
				award_id 	: award_id
			}
		);
	}
}

function awardSearchCompetitor(obj_id, competitor_id,root_id) {
	awardResetAll();
	
	$("#award-search-result").load(
		'searchCompetitor.dsp', {
			root_id	: root_id,
			competitor_id : competitor_id
		}
	);
}

function updateCompetitorList(obj,init,root_id) {
	if ( init ) {
		section_id = init
	} else {
		section_id = obj.options[ obj.selectedIndex ].value;
	}
	
	var listLayer = $("#competitorList");
	listLayer.html('<div class="competitors-name">Henter data...</div>');
	
	$("#competitorList").load(
		'updateCompetitorList.dsp', {
			root_id		: root_id,
			section_id	: section_id
		}
	);
}

function award_exp_row(row_id) {
	
	// if current row is not equal to the current expanded row
	if ( row_id != document.selected_award_row_id ) {
		
		// and there is a current expanded row
		if ( document.selected_award_row_id ) {
			
			// collapse the current expanded row
			award_toggle_row( document.selected_award_row_id );		
			
			// and instead expand the current row
			award_toggle_row( row_id );
		} else {
			
			// just expand the current row
			award_toggle_row( row_id );
			
		}
		
		// Set the current row to be the currently expanded row
		document.selected_award_row_id = row_id;
		
		
		
	} else {
		
		// Collapse the current row if it is already expanded
		award_toggle_row( document.selected_award_row_id );	
		
		// Mark that no row is currently expanded	
		document.selected_award_row_id = null;
	}
	
	
}

function award_toggle_row( row_id ) {
	var layer = $( '#' + row_id );
	if ( layer.length > 0 ) {
		var page_layer = $( '#' + row_id + '_extra' );
		
		
		if ( (layer.attr('myActivated') == 'no' ) || ( layer.attr('myActivated') == null ) ) {
			layer.attr('myActivated', 'yes' );
			layer.attr('className', 'row-active' );	
			
			$.get( 'get_award_page.dsp', {
					row_id : row_id
				},
				function( t ) {
					var result = t;
					var params = new Array();
					params = result.split(/<SPLIT>/);
					var layer_id = params[ 0 ];
					var data = params[ 1 ];
					var extra_layer = $('#' + layer_id + '_status' );
					
					extra_layer.html( data );	
					page_layer.slideDown( 250, function() {
						if ( ( $(".article .a1101imagetext").length ) > 0 ) {	
							$(".article .a1101imagetext").css( "display", "block" );
						}	
					});	
				}
			);
		
		} else {
			layer.attr( 'myActivated', 'no' );
			layer.attr( 'className', 'row' );	
			
			page_layer.slideUp( 250 );
		}
		
		
	} 
	
	
}


function award_mark_row( row_id ) {
	var layer = $( '#' + row_id );
	if ( row_id != document.selected_award_row_id ) {
		if ( ( layer.attr('myHovered') == 'no' ) || ( layer.attr('myHovered') == null ) ) {
			layer.attr( 'myHovered', 'yes' );
			layer.attr( 'className', 'row-hovered' );	
		} else {
			layer.attr( 'className', 'row' );
			layer.attr( 'myHovered', 'no' );	
		}	
	}	
}
