/*
 * Javascript functions for "Different template"
 * Author: Dmytro Danylov
 */
function dmValidateField(elm)
{	
	if (elm.hasClass('frm_required'))
	{
		if (elm.val() == '')
		{
			elm.addClass('frm_error');
			return false; // error
		}
		else
		{
			elm.removeClass('frm_error');
			return true; // no error
		}
	}
	return true; // no error
}

function dmValidateForm(frm)
{	
	var frmElements = frm.find('input,textarea');
	
	frmElements.each(function() {
		$(this).bind('blur', function() {
			var elm = $(this);
			dmValidateField(elm);
		});
	});

	var wait = false;

	frm.bind('submit', function() {
		var error = false;

		if (wait == true)
			return false;

		wait = true;
		
		frmElements.each(function() {
			var elm = $(this);
			if (!dmValidateField(elm))
				error = true;
		});

		wait = false;
		
		if (error == false)
			return true
		else
			return false;
	});
}

function switch_style(css_title)
{
  var i, link_tag ;
  for (i = 0, link_tag = document.getElementsByTagName("link"); i < link_tag.length; ++i)
  {
    if ((link_tag[i].rel.indexOf("stylesheet") != -1) && link_tag[i].title == "main")
    {
      link_tag[i].href = "css/" + css_title + ".css";
    }
  }
}

$(document).ready(function()
{
	/* Front page slideshow */
	var header = $('#header');
	if (header.hasClass('slideshow'))
	{
		$("#slideshow").slideshow({
			speed: 900, // milliseconds
			autoScrollInterval: 3000, // 3 seconds
			autoScroll: true
		});
	}
	
	/* Activate strips slideshow */
	$('.small_slideshow').slideshow2({
		animation: 'fade', /* Choose between: random, fall, fade, fadeStrips, fadeRandomStrips, curtain */
		speed: 500, // milliseconds
		autoScrollInterval: 8000,
		autoScroll: true // or true
	});
	
	/* Activate search form */
	$('.form_search').each(function() {
		$(this).find('input').bind('focus', function() {
			var elm = $(this);
			if (elm.val() == 'Search something...')
				elm.val('');
		}).bind('blur', function() {
			var elm = $(this);
			if (elm.val() == '')
				elm.val('Search something...');
		});
	});
	
	/* animate navigation */
	$("#nav li").each(function()
	{
		var $this = $(this);
		var sub = $this.find('ul:first');
		
		$this.hover(function()
		{
			sub.stop().css({overflow:"hidden", height:"auto", display:"none"}).slideDown(200, function()
			{
				$(this).css({overflow:"visible", height:"auto"});
			});
		},
		function()
		{
			sub.stop().slideUp(200, function()
			{	
				$(this).css({overflow:"hidden", display:"none"});
			});
		});
	});
	
	/* Activate tabs */
	$('.tabs').tabs({
		speed: 200, // milliseconds
		startTab : 0,
		tabActiveClass : "tabs_active"
	});
	
	/* Twitter widget */
	$('#twitter_handle').each(function()
	{
		var tweetsNum = 5; // Number of tweets to show
		var userId = "44438376"; // Your twitter account ID
		
		var handle = $(this);
		var twitterWidget = $('#twitter_widget');
		twitterWidget.css('opacity', 0);
		
		var twitterLoaded = false;
		var wait = false;
		
		function parseDate(date)
		{
			var dateArray = date.split(' ');
	      	return new Date(
	      		Date.parse(dateArray[0] + ', ' + dateArray[2] + ' ' + dateArray[1] + ' ' + dateArray[3] + ' ' 
	      			+ dateArray[5].substring(0,4))
	        );
		}
		
		handle.hover(function ()
		{
			var handle = $(this);
			
			twitterWidget.stop().css('display', 'block').animate({opacity: 1}, 400);
			
			if (twitterLoaded == false && wait == false)
			{
				wait = true;
				$.ajax({  
			        url: "http://twitter.com/statuses/user_timeline/" + userId + ".json?count=" + tweetsNum + "&callback=?",
			        dataType: "json",
			        timeout: 15000,
			  
			        success: function(data)
			        {
						var li = null;
						var ul = $('<ul>');
						
						for (i = 0; i < data.length; ++i)
						{
							var when = parseDate(data[i].created_at);
							var month = when.getMonth() + 1;
					        	month = (month < 10) ? '0'+month : month;
					        
							li = document.createElement('li');
							
							// Create html
							li.innerHTML = '<a href="http://twitter.com/' + data[i].user.screen_name
								+ '"><img class="frame left" src="' + data[i].user.profile_image_url
								+ '" alt="no image" /></a>' + '<span class="tweet_date">'
								+ when.getFullYear() +'-'+ month +'-'+ when.getDate() +' at '+ when.getHours()
					            +':'+ when.getMinutes() +':'+ when.getSeconds() 
								+ '</span>' + '<span class="tweet_text">' + data[i].text + '</span>';
							
							ul.append(li);
						}
						
						ul.insertBefore($('#twitter_widget_inner').children('a:first'));
						twitterLoaded = true;
						wait = false;
			        },
			        
			        error: function()
			        {
			        	wait = false;
			        }
			    });
			}
		},
		function()
		{
			twitterWidget.stop().animate({opacity: 0}, 400, function() {
				$(this).css('display', 'none');
			});
		});
	});
});
