/*Cufon.replace('h1.cufontitle', { fontFamily: 'Rockwell' });
Cufon.replace('span.number', { fontFamily: 'TradeGothic LT CondEighteen' });
Cufon.replace('h1.themename', { fontFamily: 'TradeGothic LT CondEighteen' });*/

var loading_container = '<div class="loading"><img src="'+path+'/images/loader.gif" /></div>';
var loading_container2 = '<div class="loading" style="padding:4px 10px 5px"><img src="'+path+'/images/loader.gif" /></div>';
//var loading_container = '<div class="loading"><h1 class="Rockwell">Loading ... Please wait</h1></div>';

$(function(){
	// newsletter
	var btn_newsletter = $('#frm_newsletter .button span');
	
	$('#frm_newsletter').submit(function(e){
		e.preventDefault(); //don't submit the form
		
		$.ajax({
			url:		$(this).attr('action'),
			type:		'post',
			data:		$(this).serialize(),
			beforeSend:	function(){
				var email = $('input[name=email]');
				
				if(!checkEmail(email.val())){
					$(btn_newsletter).removeClass('loading');
					
					return false;
				}
				
				$(btn_newsletter).addClass('loading');
			},
			success:	function(data){
				$('.sub_status').removeClass('hide').html('<span>'+data+'</span>');
				
				$(btn_newsletter).removeClass('loading');
			}
		});
	});
	
	// twitter
	$("#twitter_updates").getTwitter({
		userName: 'themebaker',
		numTweets: 5,
		loaderText: "Loading tweets...",
		slideIn: true,
		slideDuration: 350,
		showHeading: false,
		headingText: "Latest Tweets",
		showProfileLink: true,
		showTimestamp: true
	});
	
	// colorbox
	$("a[rel='thumbnail_home'], a[rel='thumbnail']").colorbox({transition:"fade"});
	
	// menu - hover effect
	$(".menu li:not(.current_page_item) a").css('opacity', 0.3); // initial set the opacity
	$(".menu li:not(.current_page_item) a").hover(function() {
		$(this).stop().animate({ opacity:1 }, 300);
	}, function() {
		$(this).stop().animate({ opacity:0.3 }, 300);
	});
	
	//@start - page our theme - advanced filter
	var $theme_type_list = $('#theme_type li a');
	var $theme_tag_list = $('#theme_tag li a');
	
	$theme_type_list.click(function(e){
		var self = $(this);
		
		// remove all class
		$theme_type_list.each(function() {
			$(this).removeClass('active');
		});
		self.addClass('active');
		
		// ajax call
		load_theme_list();
		
		e.preventDefault();
	});
	
	$theme_tag_list.click(function(e){
		var self = $(this);
		
		// remove all class
		$theme_tag_list.each(function() {
			$(this).removeClass('active');
		});
		self.addClass('active');
		
		// ajax call
		load_theme_list();
		
		e.preventDefault();
	});
	
	$('#reset').click(function(e){
		// remove all class
		$('#theme_type li a, #theme_tag li a').each(function() {
			$(this).removeClass('active');
		});
		
		// hide more list
		$("#our_themes_more").slideToggle();
		
		// toggle text more/less
		$("span.more a").toggle();
		
		// set to 'all'
		$theme_type_list.eq(0).addClass('active'); // set 'all' to active only
		
		// ajax call
		load_theme_list();
		
		e.preventDefault();
	});
	
	// more/less in page our themes
	$("span.more").click(function(e) {
		$(this).find('a').toggle();
		$("#our_themes_more").slideToggle();
		e.preventDefault();
	});
	// @end - page our theme - advanced filter
	
	// start tb.search
	var w = $(document).width(), h = $(window).height();
	var tb_search = $("#tb_search"), tb_search_form = $("#tb_search_form"), tb_search_result = $("#tb_search_result");
	var tb_open_search = $("#tb_open_search");
	var tb_s = tb_search_form.find('#s');
	
	// initiate search input
	init_search_input();
	
	$("#tb_open_search, #tb_search a.close").click(function(e) {
		tb_search.slideToggle(300);
		tb_open_search.find('span').toggle();
		e.preventDefault();
	});
	
	tb_search_form.submit(function(e) {
		location.hash = 's=' + tb_search_form.find('input[name=s]').val();
		
		get_result( $(this).serialize() );
		
		e.preventDefault();
	});
	
	if(typeof getHashUrlVars()['s'] != 'undefined') {
		var s = window.location.hash.slice(1);
		
		tb_s.val( getHashUrlVars()['s'] ); // set value to input search
		
		if(getHashUrlVars()['hide'] == 1) {
			get_result(s);
		}
		else {
			tb_open_search.find('span').toggle();
			tb_search.slideToggle(300, function() { get_result(s) });
		}
	}
	
	function get_result(data) {
		// call ajax
		$.ajax({
			url:site_url+'/wp-admin/admin-ajax.php',
			type:'POST',
			data:'action=tb_ajax&a=search&' + data,
			beforeSend: function() {
				tb_search_result.html(loading_container2);
			},
			success:function(results)
			{
				tb_search_result.html(results);
			}
		});	
	}
	
	function init_search_input() {
		tb_s.val('Enter search keyword'); // initial set
		
		tb_s.bind("focus", function() {
			tb_s.val('');
		});
		
		tb_s.bind("blur", function() {
			if(tb_s.val()=='') {
				tb_s.val('Enter search keyword');
			}
		});
	}
	// @end tb.search
	
	// start trackback pagination
	if( $("ol.trackback li").length > 5 ) {
		$(".trackback_navigation").show();
		
		$('#trackback_container').pajinate({
			items_per_page : 8,
			item_container_id : '.trackback',
			nav_panel_id : '.trackback_navigation'
		});
	}
	// @end trackback pagination
	
	// start footer menu
	var footer_menu_total = $("ul.Voll li").length-1;
	
	$("ul.Voll li").each(function(i, v) {
		if(i%2 == 0) {
			$(this).addClass("first");
		}
		
		if(footer_menu_total==i) $("ul.Voll li:eq("+i+")").removeClass("first");
	});
	// @end footer menu
});

function checkEmail(email) {
	//var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	
	if (filter.test(email))
		testresults=true;
	else{
		alert("Please input a valid email address!");
		testresults=false;
	}
	
	return testresults;
}

// Read a page's GET URL variables and return them as an associative array.
function getHashUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.hash.slice(1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function load_theme_list() {
	var $theme_type = $('#theme_type li a.active').attr('rel');
	var $theme_tag = $('#theme_tag li a.active').attr('rel');
	if(typeof $theme_tag == 'undefined') $theme_tag='';
	
	var $theme_list = $("#theme_list");
	
	// call ajax
	$.ajax({
		url:site_url+'/wp-admin/admin-ajax.php',
		type:'POST',
		data:'action=tb_ajax&a=filter-theme&theme_type='+ $theme_type +'&theme_tag=' + $theme_tag,
		beforeSend: function() {
			$theme_list.html(loading_container);
		},
		success:function(results)
		{
			$theme_list.html(results);
		}
	});
}
