/**
 * @author joolss
 */

/* ========================  ======================== */
/* ------------------------  ------------------------ */
/* ........................  ........................ */

/* ------------------------ Global namespace ------------------------ */

var volvoPoll = {};

/* ------------------------ Query ------------------------ */

volvoPoll.query_init = function() {
	$('.query_alts td').hover(function(){
		$(this).addClass('sys_query_alt_hover');
	},function(){
		$(this).removeClass('sys_query_alt_hover');
	});
}


/* ------------------------ result ------------------------ */

volvoPoll.queryResult_init = function() {
	$('.query_result h3').each(function(){
		var resultPrc = $('span',this).text();
		resultPrc = parseInt(resultPrc);
		
		$(this).after('<div class="resultView"><div data-lbi-pollResult="'+resultPrc+'"></div></div>');
	});
	
	$('.resultView div').each(function(){
		$(this).animate({
			width: $(this).attr('data-lbi-pollResult')+'%'
		},800); 
	});
	
	//volvoPoll.parentIframe_setHeight(40);
}


/* ------------------------ archive ------------------------ */


volvoPoll.textOverFlowChk = function(jQSel) {
	$(jQSel).each(function(){
		if (!this.dataLbiTextCompare) {
			var textTotal = $(this).text();
			textTotal = textTotal.split(' ');
			var textCompare = [];
			
			for (var i = 0, il = textTotal.length; i < il; i++) {
				if (i == 0) {
					textCompare[i] = textTotal[i] + ' ';
				} else {
					textCompare[i] = textCompare[i-1] + textTotal[i] + ' ';
				}
			}
			
			this.dataLbiTextCompare = textCompare;
		}

		this.counter = 0;
		this.ovFloChk = $('#sys_ovFloChk').height();
		this.ovFloMaxIndex = this.dataLbiTextCompare.length;
		while ((this.counter >= 0) && (this.counter < this.ovFloMaxIndex)) {
			$(this).html(this.dataLbiTextCompare[this.counter]);
			if ($(this).height() <= this.ovFloChk) { // if the content fits in one row, proceed
				this.counter++;
			} else { // if not, go back to last fitting sentence and put in ellipsis, and stop loop
				$(this).text(this.dataLbiTextCompare[this.counter-1] + '...');
				if ($(this).height() <= this.ovFloChk) { // do extra check, so that the ellipsis doesn't cause overflow
					this.counter = -1;
				} else {
					$(this).text(this.dataLbiTextCompare[this.counter-2] + '...');
					this.counter = -1;
				}
				
			}
		}
		
	});
}


/* ------------------------ set height of parent iframe ------------------------ */

volvoPoll.parentIframe_setHeight = function(heightInt) {
	if (window != top) {
		var parentIframe = $('iframe', parent.document).eq(0);
		//var parentIframe = false;
		/* $('iframe', parent.document).each(function() {
			if (!parentIframe && (location.href.indexOf(this.src) >= 0)) {
				parentIframe = this;
				alert(this);
			}
		}); */

		$(parentIframe).attr('scrolling', 'no');
		
		var currentHeight = $('form').height();
		var newHeight = currentHeight + heightInt;

		$(parentIframe).attr('scrolling', 'auto');
		$(parentIframe).css('height', newHeight + 'px');
	}
}



// ------------------------ auto-trig for select boxes ------------------------

volvoPoll.init_selectboxAutoTrig = function(trigClassName,callBack) {
	var userAgent = navigator.userAgent;
	var uACheck = userAgent.match('AppleWebKit');
	
	var selectElems = document.getElementsByTagName('select');
	for (var i = 0, il = selectElems.length; i < il; i++) {
		if (selectElems[i].className.indexOf(trigClassName) > -1) {
			var selectBox = selectElems[i];
			if (uACheck != null) {
				selectBox.onchange = selectChangedSafari;
				selectChangedSafari.reassign = callBack;
			} else {
				selectBox.changed = false;
				selectBox.onfocus = selectFocused;
				selectBox.onchange = selectChanged;
				selectBox.onkeydown = selectKeyed;
				selectBox.onclick = selectClicked;
			}
			selectBox.selectboxAutoTrig = callBack;
		}
	}
	
	return true;
}

function selectChangedSafari(){
	if (!this.selectboxAutoTrig) {
		this.selectboxAutoTrig = selectChangedSafari.reassign; // if assignment is lost during navigation
	}
	this.selectboxAutoTrig(); // trigger action of selectbox
} 

function selectChanged(theElement) {
	var theSelect;
	if (theElement && theElement.value) {
		theSelect = theElement;
	} else {
		theSelect = this;
	}
	if (!theSelect.changed) {
		return false;
	}
	
	theSelect.selectboxAutoTrig(); // trigger action of selectbox
	return true;
}

function selectClicked() { this.changed = true; }

function selectFocused() {
	this.initValue = this.value;
	return true;
}

function selectKeyed(e) {
	var theEvent;
	var keyCodeTab = "9";
	var keyCodeEnter = "13";
	var keyCodeEsc = "27";
	
	if (e) {
		theEvent = e;
	} else {
		theEvent = event;
	}
	
	if (theEvent.keyCode == keyCodeEnter) {
		this.changed = true;
		selectChanged(this);
	} else if (theEvent.keyCode == keyCodeEsc) {
		this.value = this.initValue;
	} else {
		this.changed = false;
	}
	return true;
}

// ------------------------ go to URL by select box ------------------------ 

volvoPoll.selectbox_goToUrl = function() {
	if(this.value.length > 0) {
		location.href = this.value; 
	}
}

// ------------------------ post page ------------------------ 

volvoPoll.post = function() {
	$('form')[0].submit();
}


/* ======================== DOM Ready ======================== */

$(document).ready(function(){
	$('form').append('<div id="sys_ovFloChk">&nbsp;</div>');
	
	volvoPoll.init_selectboxAutoTrig('trig_selectPost',volvoPoll.post);
	
	volvoPoll.query_init();
	
	volvoPoll.queryResult_init();
	
	volvoPoll.textOverFlowChk('.archive_text');
	
	$(window).resize(function(){
		if (!volvoPoll.resizeOverflow) {
			volvoPoll.resizeOverflow = setTimeout(function(){
				volvoPoll.textOverFlowChk('.archive_text');
				clearTimeout(volvoPoll.resizeOverflow);
				 delete volvoPoll.resizeOverflow;
			},250);
		}
	});
});

