$(document).ready(function(){

	$('a#lamp_switch').click(function() {
		if ($('div#lamp').css('display') != 'none') {
    		$('div#lamp').hide();
    		$('div#lamp_off').show();
    		$('div#zoltan').css({'background-position' : 'top right'});
    	} else {
    		$('div#lamp').show();
    		$('div#lamp_off').hide();
    		$('div#zoltan').css({'background-position' : 'top left'});
    	}
	});
	
	$('select#country').hide().parent().append('<div class="country"><span>Hungary</span><div class="options"><div class="textareabottom"></div></div></div>');
	
	$('select#budget-currency').hide().parent().append('<div class="budget-currency"><span>&euro; EUR</span><div class="options"><div class="budgetbottom"></div></div></div>');
	
	$('select#country option').each(function() {
		$('div.country div.options div.textareabottom').before('<div class="option" id="' + $(this).attr('value') + '">' + $(this).attr('text') + '</div>');
	});
	
	$('select#budget-currency option').each(function() {
		$('div.budget-currency div.options div.budgetbottom').before('<div class="option" id="' + $(this).attr('value') + '">' + $(this).attr('text') + '</div>');
	});
	
	$('div.country, div.budget-currency').click(function() {
		if ($(this).find('div.options').css('display') == 'none') {
			$('div.options').hide();
			$(this).find('div.options').slideDown('fast');
		} else {
			$(this).find('div.options').slideUp('fast');
		}
	});
	
	$('div.country div.options div.option').click(function() {
		id = $(this).attr('id');
		$('select#country').attr('value', id);
		$('div.country span').html($(this).html());
		$(this).parent().slideUp('fast');
	});
	
	$('div.budget-currency div.options div.option').click(function() {
		id = $(this).attr('id');
		$('select#budget-currency').attr('value', id);
		$('div.budget-currency span').html($(this).html());
		$(this).parent().slideUp('fast');
	});
	
	$('span.wpcf7-list-item input').each(function() {
		$(this).css({'width' : 0, 'opacity' : 0, 'margin' : 0}).parent().parent().prepend('<span class="checkbox"></span>');
		if ($(this).attr('checked')) $(this).parent().parent().addClass('checked');
		$(this).change(function() {
			//if ($(this).attr('checked')) $(this).parent().parent().find('span.checkbox').addClass('checked');
			if ($(this).attr('checked')) $(this).parent().parent().addClass('checked');
			else $(this).parent().parent().removeClass('checked');
		});
	});
	
	$('span.checkbox').click(function() {
		if (!($(this).parent().hasClass('checked'))) {
    		$(this).parent().addClass('checked');
    		$(this).parent().find('input').attr('checked', true);
    	} else {
    		$(this).parent().removeClass('checked');
    		$(this).parent().find('input').attr('checked', false);
    	}
   
	});
	
	$('div.portfolio div.gallery img').each(function() {
		$(this).attr('title', '').parent().attr('title', '');
		var cnts = $(this).parent().contents();
		$(this).parent().replaceWith(cnts);
	});

	var index = 0;
	var indit;
	var tarolo = $('div.showcase div.gallery');
	var next = index + 1;
	var prev = $(tarolo).children().size() - 1;
	var valtido = 8000;
	var animido;
	
	function Kepvalt(index) {
	
		clearInterval(indit);
		
		if (index >= $(tarolo).children().size()) {
			index = 0;
			next = index + 1;
			prev = $(tarolo).children().size() - 1;
		} else {
			if (index < 0) { index = $(tarolo).children().size() - 1; }
			prev = index - 1;
			next = index + 1;
		}
		
		
		var pos = index * 920;
		var maxpos = $(tarolo).children().size() * 920;
		
		if (pos < maxpos) { pos = (index * - 920); animido = 1000; } else { pos = 0;  animido = 1500;}
		
		var active = index + 1;
		var name = $(tarolo).find('img').eq(index).attr('alt');
		
		$('a#show_name').fadeOut('fast', function() { $('a#show_name').html(name).fadeIn('normal'); });

		$(tarolo).animate({ left: pos }, animido);
		
		indit = setInterval(function() { Kepvalt(next); }, valtido);
		
		$('ul.controller li a').removeClass('on');
		$('ul.controller li a#work_' + active).attr('class', 'on');
		
		
	}
	
	indit = setInterval(function() { Kepvalt(next); }, valtido);
	
	$(tarolo).mouseover(function() { clearInterval(indit); }).mouseout(function() { indit = setInterval(function() { Kepvalt(next); }, 3000); });
	$('div.showcase div.topshadow').mouseover(function() { clearInterval(indit); }).mouseout(function() { indit = setInterval(function() { Kepvalt(next); }, 3000); });
	
	$('a#next').click(function () {
		clearInterval(indit);
		Kepvalt(next);
	});
	
	$('a#prev').click(function () {
		clearInterval(indit);
		Kepvalt(prev);
	});
	
	$('ul.controller li a').click(function() {
		var href = $(this).attr("id").replace(/work_/g, '');
		href--;
		Kepvalt(href);
	});
	
	
	$('a.shadow').click(function() {
		var picture = $(this).attr('href');
		var pp = $(this).attr('id').replace(/pp_/, '');
		$('body').append('<div class="fadegallery"></div>');
		$('div.fadegallery').append('<a class="close">Click to close</a>').append('<div class="shade"></div>').find('div.shade').css({'opacity' : '0.6'}).parent().append('<img src="' + picture + '" alt="" />');
		$('div.fadegallery img').css({'top': '1450px', 'width' : 0, 'height' : 0, 'margin-left' : (5 - pp) * -1 * 100});
		$('div.fadegallery').fadeIn('fast', function() {
			$(this).find('img').animate({
				'height': '400px',
				'width': '800px',
				'opacity' : 1,
				'top' : '1250px',
				'margin-left' : '-400px'
			}, 200);
			$('div.fadegallery a.close').css({'top': '1230px'}).fadeIn('slow');
			$('div.fadegallery').attr('title', 'Click anywhere to close');
		});
		return false;
	});
	
	
	$('div.fadegallery, a.close').live('click', function() {
		$('div.fadegallery img').animate({'height' : 0, 'width' : 0, 'top' : '1450px', 'margin-left' : '-250px'});
		$('div.fadegallery').fadeOut('fast', function() {
			$('div.fadegallery').remove();
		});
	});
	

	$('div.dribbbleinfo').each(function() {
		var id = $(this).attr('id');
		if (id != 'dribbble_')
			$.getJSON('http://api.dribbble.com/shots/' + $('div#' + id).html() + '?callback=?', function(json) {
				var created = new Date(json.created_at);
				if ( json.likes_count != '') {
					var cmonth = created.getMonth() + 1; if (cmonth < 10) cmonth = '0' + cmonth;
					var cday = created.getDate(); if (cday < 10) cday = '0' + cday;
					$('div#' + id).html('<span class="author"><a href="' + json.player.url + '" class="player"><img src="' + json.player.avatar_url + '" alt="Avatar of ' + json.name + '" /><strong>' + json.player.name + '</strong></a><div class="clear"></div></span><span class="upload"><strong>Uploaded</strong> ' + created.getFullYear() + '. ' + cmonth + '. ' + cday + '.</span><span class="likes"><strong>Likes</strong> ' + json.likes_count + '</span><span class="views"><strong>Views</strong> ' + json.views_count + '</span><span class="comments"><strong>Comments</strong> ' + json.comments_count + '</span>').show();
				}
			});
		else
			$(this).hide();
	});
	
	$('div.faq strong').click(function() {
		$(this).parent().find('p').slideToggle();
	});
	
	if (window.location.hash.replace(/_[0-9]/g, '') == '#faq') {
		var openfaq = window.location.hash.replace(/#/g, '');
		$('div#' + openfaq).css({'border-bottom' : '1px solid rgba(255, 255, 255, 0.4)', 'border-top': '1px solid rgba(0, 0, 0, 0.5)', 'background' : 'rgba(0, 0, 0, 0.2)', 'padding' : '10px 15px 5px 15px', '-moz-border-radius' : '7px', '-webkit-border-radius' : '7px'});
		$('div#' + openfaq + ' p').slideDown();
	}
	
	if (window.location.hash.replace(/_[0-9]/g, '') == '#coffee') {
		$('div#coffee').css({'border-bottom' : '1px solid rgba(255, 255, 255, 0.4)', 'border-top': '1px solid rgba(0, 0, 0, 0.5)', 'background' : 'rgba(0, 0, 0, 0.2)', 'padding' : '10px 15px 5px 15px', '-moz-border-radius' : '7px', '-webkit-border-radius' : '7px'});
	}
	
	if ($('strong#downloads').length) {
		$.get('http://www.zoltanhosszu.com/wordpress/wp-content/themes/zoltanhosszu/gaget_getdownloads.php', function(data) {
			$('strong#downloads').html(data);
		});
	}
	
	if ($('div.column.left.twitter').length) {
		$.get('http://www.zoltanhosszu.com/my-latest-tweet/', function(data) {
			$('div.column.left.twitter').html(data);
		});
	}
	
});

