// JavaScript Document

function ajaxRefreshed () {
	positionFooter();
	stretchOverlay();
}

function slideNewFeedEntry(latestEntry) {
 $('.' + latestEntry).css('display', 'none');
 $('.' + latestEntry).slideDown('slow');
}

function positionFooter() {
	var windowH = $(window).height();
	var containerH = $('#container').height();
	var footerH = 80;
	var totalH;
	var yOffset;
	
	if ((containerH + footerH) <= windowH) {
		$('#footer').css('top', windowH - 200);
	} else {
		$('#footer').css('top', containerH - 120);
		$('#footer').removeClass('footer-fixed');
	}
}

function stretchOverlay() {
	var documentH = $(window).height();
	$('#overlay').height(documentH + 80);
}

function textfieldHideFake(textfieldFake) {
	textfieldFake.hide();
	textfieldFake.next().show();
	textfieldFake.next().focus();
	return(textfieldFake.next());
}

function textfieldShowFake(textfieldReal) {
	textfieldReal.hide();
	textfieldReal.prev().show();
	textfieldReal.prev().blur();
	return(textfieldReal.prev());
}

function collapseStatsDetails (currentIcon) {
	var currentTbody = currentIcon.parent().parent().parent();
	currentTbody.children('.stats-details').fadeOut('fast');
	currentIcon.hide();
	currentTbody.children('tr').children('th').children('.collapsed').show();
	
	var currentSubtotal = currentTbody.children('tr').children('.stats-subtotal-number').html();
	currentTbody.children('tr').children('.stats-subtotal-th').html(currentSubtotal);
}

$(window).load(function() {
	positionFooter();
	$('#footer').css("visibility", "visible");
	stretchOverlay();
});

$(document).ready(function(){

	if (jQuery.browser.msie) {
		if(parseInt(jQuery.browser.version) == 6) {
			// do nothing
		}
	}

	if ($('table.visualize-data').html() != null) {
		$('.visualize-data').visualize({type: 'pie', height: '200px', width: '200px', appendTitle: false, appendKey: false});
	};

	$(window).resize(function() {
		positionFooter();
		stretchOverlay();
	});
	
	$('.navi-item').hover(
		function(){
			if (!$(this).hasClass('navi-item-selected')) {
				$(this).children('.navi-heading').addClass('navi-heading-selected');
			}
			//display the submenu, if it exsists
			if($(this).children('.navi-submenu').html() != null) {				
				$(this).children('.navi-submenu').css('display', 'block');
			}
		},
		function() {
			if (!$(this).hasClass('navi-item-selected')) {
				$(this).children('.navi-heading').removeClass('navi-heading-selected');
			}
			
			//remove the submenu
			if($(this).children('.navi-submenu').html() != null) {
				$(this).children('.navi-submenu').css('display', 'none');
			}
		}
	);
	
	//takes care of behaviour for the log in textfields
	$('.value-disappear-textfield').focus(
		function() {
			var currentTextfield = $(this);

			if ($(this).hasClass('textfield-fake')) {
				currentTextfield = textfieldHideFake($(this));
			}

			//if the current value in the text field is "Email" or "Password", empty the text field and add the focus class
			var currentValue = currentTextfield.val();
			if ((currentValue == currentTextfield.attr('title')) &&
				!(currentTextfield.hasClass('textfield-focus'))) {
				currentTextfield.val('');
				currentTextfield.addClass('textfield-focus');
			}
		}
	);
	$('.value-disappear-textfield').blur(
		function() {
			var currentValue = $(this).val();

			//if the textfield is empty, return it to the original value and remove the focus class
			//if the current text field is the password textfield, returning to the original means hiding the real password textfield and showing the fake one
			if (currentValue == '') {
				var currentTextfield = $(this);
				
				if ($(this).prev().hasClass('textfield-fake')) {
					currentTextfield = textfieldShowFake($(this));
				}
				
				currentTextfield.val(currentTextfield.attr('title'));
				$(this).val($(this).attr('title'));
				currentTextfield.removeClass('textfield-focus');
				$(this).removeClass('textfield-focus');
			}
		}
	);
	
	$(".image-button").hover(
		 function(){
		 	var src = $(this).attr('src').substr(0, $(this).attr('src').length - 4);
		 	$(this).attr('src', src + '-hover.png');
		 },
		 function(){
			 var src = $(this).attr('src').substr(0, $(this).attr('src').length - 10);
		 	$(this).attr('src', src + '.png');
		 }
	)
	
	//make the tips appear when a textarea has focus
	$('.info textarea').focus(
		function() {
			$(this.parentNode.parentNode).children('.tip').css('visibility', 'visible');
			$(this.parentNode.parentNode).addClass('info-and-tip-item-active');
		}
	);
		
	$('.info textarea').blur (
		function() {
			$(this.parentNode.parentNode).children('.tip').css('visibility', 'hidden');
			$(this.parentNode.parentNode).removeClass('info-and-tip-item-active');
		}
	);
	
	$('#reward-display-add-new').click(
		function() {
			if ($('#reward-display-none-message').css('display') != 'none') {
				$('#reward-display-none-message').css('display', 'none');
			}
			$(this).css('display', 'none');
			$('#reward-display-add-form').css('display', 'block');
			positionFooter();
		}
	);
	
	$('#add-new-reward').live('click', function() {
		$('#reward-display-items').prepend("\<div class=\"reward-display-item\"\>" +
										"\<div class=\"reward-display-item-options\"\>" +
										"\<a href=\"\"\>Edit\</a\> " +
										"\<a href=\"\"\>Delete\</a\>" +
										"\</div\>"+
										"\<img src=\"images/img.png\" alt=\"\" /\>" +
										"\<div\>This is my xBox that i got from rewards1. I love it!!!!! You can get it too if you sign up!\</div\>" +
										"\</div\>");
		$('#reward-display-add-form').css('display', 'none');
		$('#reward-display-add-new').css('display', 'block');
	});
	
	$('.reward-display-item').live('mouseenter', function() {
		$(this).children('.reward-display-item-options').css('display', 'block');
	});
	
	$('.reward-display-item').live('mouseleave', function() {
		$(this).children('.reward-display-item-options').css('display', 'none');
	});
	
	$('.stats-table tr').hover(
		function() {
			$(this).children('.stats-description').removeClass('hidden');
			$(this).addClass('stats-selected-row');
		},
		function() {
			$(this).children('.stats-description').addClass('hidden');
			$(this).removeClass('stats-selected-row');
		}
	);
	
	$('.sort-inactive').hover(
		function() {
			$(this).attr('src', 'images/sort-ascending.png');
		},
		function() {
			$(this).attr('src', 'images/sort-inactive.png');
		}
	);
		
	$('.collapse-all').click(
		function() {
			var currentSection = $(this).parent().parent();
			currentSection.children('table').children('tbody').children('tr').children('th').children('.collapse-text').each(function() {
				collapseStatsDetails($(this));
			});
			positionFooter();
		}
	);
	
	$('.expand-all').click(
		function() {
			var currentSection = $(this).parent().parent();
			currentSection.children('table').children('tbody').children('tr').children('th').children('.expand-text').each(function() {
				expandStatsDetails($(this));
			});
			positionFooter();
		}
	);
	
	$('.collapse-text').click(
		function () {
			collapseStatsDetails($(this));
			positionFooter();
		}
	);
	
	$('.expand-text').click(
		function () {
			expandStatsDetails($(this));
			positionFooter();
		}
	);
	
	$('#how-it-works-more').click(
		function() {
			var currentState = $('#how-it-works-details').css('display');
			if (currentState != 'none') {
				$('#how-it-works-details').slideUp('fast', function() {
					positionFooter();
				});
				$(this).html('more...');
			}
			else {
				$('#how-it-works-details').slideDown('fast', function() {
					positionFooter();
				});
				$(this).html('less');
			}
		}
	);
	
	$('.beginners-guide-block a').hover(
		function() {
			$(this).children('.beginners-guide-arrow').attr('src', 'images/goto-beginners-guide-icon-hover.png');
		},
		function () {
			$(this).children('.beginners-guide-arrow').attr('src', 'images/goto-beginners-guide-icon.png');
		}
	);
	
	$('edit-description-button').click(
		function() {
			var currentDescription = $('.description-plain').html();
			$('.description').html('\<textarea class=\"tracking-id-description\"\>\</textarea\>');
		}
	);
	
	$('.question').click(
		function() {
			if($(this).next().hasClass('collapsed-answer')){
				$(this).next().slideDown('fast', function() {
					$(this).removeClass('collapsed-answer');
					$(this).addClass('expanded-answer');
					positionFooter();
				});
			}
			else {
				$(this).next().slideUp('fast', function() {
					$(this).addClass('collapsed-answer');
					$(this).removeClass('expanded-answer');
					positionFooter();
				});
			}
		}
	);
	
	$('.collapse-all-faq').click(
		function() {
			$(this).parent().parent().parent().children('.content-block').children('.expanded-answer').each(function() {
				$(this).slideUp('fast', function() {
					$(this).addClass('collapsed-answer');
					$(this).removeClass('expanded-answer');
					positionFooter();
				});
			});
		}
	);
	
	$('.expand-all-faq').click(
		function() {
			$(this).parent().parent().parent().children('.content-block').children('.collapsed-answer').each(function() {
				$(this).slideDown('fast', function() {
					$(this).removeClass('collapsed-answer');
					$(this).addClass('expanded-answer');
					positionFooter();
				});
			});
		}
	);
	
	$('#error, #success').slideDown('slow', function() {
		var currentNotification = $(this);
		setTimeout(function() {
			if (currentNotification.css('display') != 'none') {
				currentNotification.slideUp('fast');
			}
			clearTimeout();
		}, 10000);
		
	});
	
	$('#error, #success').click(
		function() {
			$(this).slideUp('fast');
		}
	);
});

function collapseStatsDetails (currentIcon) {
	var currentTbody = currentIcon.parent().parent().parent();
	currentTbody.children('.stats-details').fadeOut('fast', function() {
		positionFooter();
	});
	currentIcon.hide();
	currentTbody.children('tr').children('th').children('.expand-text').show();
	
	var currentSubtotal = currentTbody.children('tr').children('.stats-subtotal-number').html();
	currentTbody.children('tr').children('.stats-subtotal-th').html(currentSubtotal);
}

function expandStatsDetails (currentIcon) {
	var currentTbody = currentIcon.parent().parent().parent();
	currentTbody.children('.stats-details').fadeIn('fast', function() {
		positionFooter();
	});
	currentIcon.hide();
	currentTbody.children('tr').children('th').children('.collapse-text').show();
	currentTbody.children('tr').children('.stats-subtotal-th').html('');
}



function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}
loadjscssfile('livechat/livechat.css','css');	
loadjscssfile('ape-jsf/Build/uncompressed/apeClientJS.js','js');
loadjscssfile('livechat/livechat.js','js');

if(typeof confirmSubmit != 'function') {
	function confirmSubmit(msg) {
	    var agree=confirm(msg);
	    if (agree)
		return true ;
	    else
		return false ;
	}
}
