var periodicLoaded = false;
var toppromos = '';
var toppromosIdx = 0;
var speed = 10000;

$(document).ready(function() {
	
	$.getJSON('/json/toppromo.json', function(j) {
		toppromos = j;
		$('#toppromo').css('background-image', 'url("/img/toppromo/'+j[toppromosIdx].img+'")');
		$('#toppromo').html(j[toppromosIdx].html.replace('\\', ''));
		$('#toppromo').attr('href', j[toppromosIdx].link.replace('\\', ''));
		if(j[toppromosIdx].target==1){
			$('#toppromo').attr('target', '_blank');
		}
		else{
			$('#toppromo').attr('target', '_top');
		}
		$('#toppromo').fadeIn('slow', function(){
			if(j.length > 1) {
				setTimeout(toppromoRenew, speed);
			}
		});
	});
	
	$('#shortcuts > li > a').click(function(e){
		prvntDefault(e);
	});
    $('#shortcuts > li').bind('mouseover', shortcuts_open);
    $('#shortcuts').find('ul:first').css('visibility', 'visible');
    shortcutsitem = $('#shortcuts').find('ul:first');
	
	$('#pertbl').click(function(e){
		prvntDefault(e);
		if($('#periodic').css('display') == 'block') {
			$('#periodic').slideUp('slow');
			$('#pertbl').html('Periodic table +')
		}
		else {
				$('#periodic').slideDown('slow');
				if(! periodicLoaded) {
					$.get('/periodic', function(data) {
						  $('#periodic DIV').html(data);
						  $('#pertbl').html('Periodic table -')
						  periodicLoaded = true;
					});
				}
			
		}
	});
	
	$('#signin').click(function(e) {
		prvntDefault(e);
	});
	
	// TABS functionality
	var url=window.location;
	var anchor=url.hash; //anchor with the # character
	
	if(anchor != '') {
		$('.tab_cnt').hide();
		$(anchor).show();
		$('.tab').removeClass('active');
		$('[href="'+anchor+'"]').addClass('active');
	}
	else {
		$('.tab_cnt').hide();
		id = $('#tabs .active').attr('id');
		$($('#tabs .active').attr('href')).show();
	}

	$('.tab').click(function(event) {
		prvntDefault(event);
		$('.tab_cnt').hide();
		$($(this).attr('href')).show();
		$('.tab').removeClass('active');
		$(this).addClass('active');
	});
	
	//MESSAGES functionality
	if($('.success').length > 0) {
		window.setTimeout(function(){
			$('.success').fadeOut('slow');
		}, 5000);
	}
	
});

function switchTab(tab) {
	//alert($(tab).attr('href'));
	$('.tab_cnt').hide();
	$(tab).show();
	$('.tab').removeClass('active');
	$(tab).addClass('active');
}

function toppromoRenew() {
	
	if(toppromosIdx < (toppromos.length - 1)){
		toppromosIdx++;
	}
	else {
		toppromosIdx = 0;
	}
	
	$('#toppromo').animate({
		'height': 0, 'top': 101
	}, 1500, function(){
		$('#toppromo').css('background-image', 'url("/img/toppromo/'+toppromos[toppromosIdx].img+'")');
		$('#toppromo').html(toppromos[toppromosIdx].html.replace('\\', ''));
		$('#toppromo').attr('href', toppromos[toppromosIdx].link.replace('\\', ''));
		if(toppromos[toppromosIdx].target==1){
			$('#toppromo').attr('target', '_blank');
		}
		else{
			$('#toppromo').attr('target', '_top');
		}
		$('#toppromo').animate({
			'height': 42, 'top': 59
		}, 1500, function(){
			setTimeout(toppromoRenew, speed);
		});
	});
}

function signin() {
	$('#frmLogon').submit();
}

function ajaxGetReplace(sPath, sObj, sLoader) {
	
	if(sLoader!=''){
		$('#'+sLoader).css('visibility', 'visible');
	}
	
	$.get(sPath, function(data) {
		$('#'+sObj).html(data);
		if(sLoader!=''){
			$('#'+sLoader).css('visibility', 'hidden');
		}
	});
}

var shortcutsitem = 0;

function shortcuts_open(){
	shortcuts_close();
	shortcutsitem = $(this).find('ul').css('visibility', 'visible');
	$(this).css('background-color', '#E0E0E0');
	shortcutsitem.css('background-color', '#E0E0E0');
}

function shortcuts_close(){
    if(shortcutsitem){
    	shortcutsitem.css('visibility', 'hidden');
    	shortcutsitem.parent().css('background-color', 'transparent');
    	shortcutsitem.parent().css('color', '#fff');
    }
}
function shortcuts_hover(){
	$(this).prevAll('a').css('background-color', '#fff');
	$(this).prevAll('a').css('color', '#000');
}

function trhover(obj) {
	$(obj).addClass('trhover');
	$(obj).mouseout(function(){
		$(obj).removeClass('trhover');
	});
}

function prvntDefault(e) {
	
	var evt = e || window.event; // IE compatibility

	if(evt.preventDefault){
		evt.preventDefault();
	}
    else{
    	evt.returnValue = false;
        evt.cancelBubble = true;
    }
}

function selectMulti(clicked, select){
	if($('#' + clicked + ':checked').length>0){
		$('#' + select + ' option').attr('selected','selected');
	}
	else{
		$('#' + select + ' option').removeAttr('selected');
	}
}

function goLoc(url) {
	location = url;
}

function confirmGo(e, q, url) {
	
	prvntDefault(e);
	
	if(confirm(q)) {
		location = url;
	}
}

function closeCatFish()
{
	$('#catfish').css('display', 'none');
	$.cookie('catfish', '0', { expires: 7, path: '/' });
}
