/* ----------------------------------------------------------------------------------------------------------
*
*	JAVASCRIPT INIT
*	Michal Pelech (www.michalpelech.cz, info@michalpelech.cz)
*
/* ---------------------------------------------------------------------------------------------------------- */

var addNew = false;
$(document).ready(function(){

  $('body').click(function(){
		$('#header .login-box').stop(true, true).fadeOut(400, function() { $(this).hide(); });
	});
	// Add new file inputs
	$('.files input:file').live('change', function(){
		checkFileInputs();
		
		if (addNew) {
			var lastItem = $('.files li:last i').html();
			var newItem = parseInt(lastItem) + 1;
			
			var item = "<li class=\"clearfix\"><i>" + newItem + "</i><input type=\"file\" id=\"my_file_element\" name=\"file_" + newItem + "\" /><a href=\"javascript:;\" class=\"info btn-grey\"><span>Podrobnosti</span></a><div class=\"info-panel clearfix\"><div class=\"comment\"><label for=\"comment" + newItem + "\">Komentář k souboru:</label><div class=\"textarea\"><textarea rows=\"10\" cols=\"20\" id=\"comment" + newItem + "\" name=\"comment[" + newItem + "]\"></textarea></div></div><span class=\"active\">&nbsp;</span></div></li>";
			
			$('.files ol').append(item);
			
			// Replace file input
			$('.files li:last input:file').filestyle({ 
				image: "/newcz/images/upload_cz.png",
				imageheight : 20,
				imagewidth : 94,
				width : 392
			});
		}
	});
	// Show login form
	$('#header .btn-login').hover(function(){
		$(this).find('.login-box').stop(true, true).fadeIn(400, function(){ $(this).show(); });
	}, function(){
		var hide = true;
		$('#header .login-box input').each(function(){
			if ($(this).is(':focus')) {
				hide = false;
			}
		});
		if (hide) {
			$(this).find('.login-box').stop(true, true).fadeOut(400, function() { $(this).hide(); });
		}
	}).click(function(e){
		e.stopPropagation();
	});
	// Show hide detailed file info
	$('.files ol li .btn-grey').live('click', function(){
		var infoBox = $(this).parent().find('.info-panel');
		if (infoBox.is(':visible')) {
			infoBox.hide();
		} else {
			infoBox.show();
		}
	});
	
	// Show hide new directory box
	$('.my-uploads .header .make-directory').live('click', function(){
		var directoryBox = $(this).parent().parent().find('.new-directory');
		if (directoryBox.is(':visible')) {
			directoryBox.hide();
		} else {
			directoryBox.show();
		}
		
		return false;
	});
	// Show/hide download panel
	$('.downloading .quick').click(function(){
		if($(this).hasClass('close')) {
			$(this).removeClass('close');
		} else {
			$(this).addClass('close');
		}
	});
	// Hide error message
	$('#iframe .downloading .error').click(function(){
		$(this).hide();
	});
	
	// Show / hide input value
$('input:text, textarea').not('#search-term, .banner').focus(function(){
		var title = $(this).val();
		if ($(this).attr('title') == '') {
			$(this).attr('title', title);
			$(this).val('');
		}
	}).blur(function(){
		var title = $(this).attr('title');
		if ($(this).val() == '') {
			$(this).val(title);
			$(this).attr('title', '');
		}
	});
	

  
  $('input#search-term').focus(function(){
  if ($(this).val() == 'hledané slovo') {    
  $(this).val('');
  } else {
  $(this).select();
  }
  });
  
  $('input#search-term').click(function(){
  if ($(this).val() == 'hledané slovo') {    
  $(this).val('');
  }
  });

  if($('input#search-term.autofocus').focus()){
  $('input#search-term.autofocus').val('hledané slovo');
  $('input#search-term.autofocus').select();
  };
   
   // Banner click
  $('textarea.banner').click(function(){
  $(this).select();
  });
  
	// Show new directory form
	$('#tab-upload .directory select').change(function(){
		if ($(this).val() == 'new'){
			$('#tab-upload .directory .new-directory').show();
			// TODO: delete this
			$('#tab-upload .directory .login').hide();
		} else {
			$('#tab-upload .directory .new-directory').hide();
			// TODO: delete this
			$('#tab-upload .directory .login').show();
		}
	});
	
	$('.tabs, #tabs, #results-all, #cities-tabs, #payments-tabs-czech, #payments-tabs-slovakia, #payments-tabs-poland, #payments-tabs-russia, #payments-tabs-other').tabs({ fx: { opacity: 'toggle' } });
	
	// Show start uploading - testing
	$('.start-uploading').click(function(){
		startUploading();
		
		return false;
	});
	
	// Hide uploading overlay on click
	$('#uploading-overlay').click(function(){
		$(this).hide();
		$('#uploading').hide();
	});
	
	// Add even class to table
	$('#uploading table tr:even, .results table tr:even, .downloads table tr:even, .credit table tr:odd, .payments-tabs table tr:odd').addClass('even');
	
	// MY UPLOAD TABLE
		var subdirectoryTr = $('.uploads-content table tr table tr').length;
		if (subdirectoryTr % 2) {
			$('.uploads-content > table > tbody > tr:odd').not('.directory, .subdirectory').addClass('even');
		} else {
			$('.uploads-content > table > tbody > tr:even').not('.directory, .subdirectory').addClass('even');
		}
		$('.uploads-content table tr table tr:even').addClass('even');
		// Hover on table
		$('.uploads-content table tr').not('.directory, .subdirectory').hover(function(){
			$(this).addClass('hover');
		}, function(){
			$(this).removeClass('hover');
		});
		
		// Hide / show subdirectory
		$('table tr.directory a.collapse').click(function(){
			var directoryId = $(this).parent().parent('.directory').attr('id');
			var subdirectoryId = 'sub_' + directoryId;
			var separatorId = 'sep_' + directoryId;
			var collapseLink = $(this);
			
			if ($('#' + subdirectoryId).is(':visible')) {
				$(collapseLink).removeClass('open');
				$('#' + subdirectoryId).hide();
				$('#' + separatorId).removeClass('sep-open');
			} else {
				$(collapseLink).addClass('open');
				$('#' + subdirectoryId).show();
				$('#' + separatorId).addClass('sep-open');
			}
			
			return false;
		});
		
	// Add links table even class
	$('.remote table tr:even, .ip table tr:even').addClass('even');
	
	// Make money toggle
	$('.make-money .box a.btn-money').click(function(){
		var how = $(this).parent().parent().find('.how');
		if (how.hasClass('open')) {
			how.addClass('close').removeClass('open');
		} else {
			how.addClass('open').removeClass('close');
		}
		
		return false;
	});
	
	// Remote
	$('.auth-login, .email-form').hide();
	$('.autorization .btn-grey').click(function(){
		var auth = $(this).parent().find('.auth-login');
		if (auth.is(':visible')) {
			auth.hide();
		} else {
			auth.show();
		}
		
		return false;
	});
	$('.send-links .btn-grey').click(function(){
		var auth = $(this).parent().find('.email-form');
		if (auth.is(':visible')) {
			auth.hide();
		} else {
			auth.show();
		}
		
		return false;
	});
	
	// Show detailed search
	$('a.show-detailed').click(function(){
		if ($('.detailed-search').is(':visible')) {
			$('.detailed-search').hide();
		} else {
			$('.detailed-search').show();
		}
		
		return false;
	});
	
	// Show / hide tooltip
	$('.btn-help').hover(function(){
		$(this).find('span').show();
	}, function(){
		$(this).find('span').hide();
	});
	
	// Hide messages
	$('.message a.btn-close').click(function(){
		$(this).parent().hide();
	});
	
	// Show payment choice
	$('.btn-recharge, .page-download').fancybox({
		'width'	 : 980,
		'height' : 850,
		'padding': 0,
		'type'   : 'iframe'
	});
	
	// Replace file input
	$('.files input[type="file"]').filestyle({ 
		image: "/newcz/images/upload_cz.png",
		imageheight : 20,
		imagewidth : 94,
		width : 392
	});
  
});

function checkFileInputs() {
	var item1 = false;
	var item2 = false;
	var item3 = false;

	$('.files input:file').each(function(index){
		if (index < 9) {
			if ($(this).val() != '') {
				if (index == 0) item1 = true;
				if (index == 1) item2 = true;
				if (index == 2) item3 = true;

				if (item1 && item2 && item3) {
					addNew = true;
				}
			} else {
				addNew = false;
			}
		} else {
			addNew = false;
		}
	});
}
// show uploading progress bar
function startUploading() {
	var windowWidth = $(document).width();
	var windowHeight = $(document).height();
	
	$('#uploading-overlay').css({
		width: windowWidth + 'px',
		height: windowHeight + 'px'
	}).show();
	
	var uploadingWidth = $('#uploading').width();
	var uploadingHeight = $('#uploading').height();
	
	$('#uploading').css({
		'margin-left': ((uploadingWidth / 2) * -1) + 'px',
		'margin-top' : ((uploadingHeight / 2) * -1) + 'px'
	}).show();
}

/* video img slideshow: */ 
var thumb_next,thumb_el,thumb_timer,thumb_gal;
function video_thumb_start(el,g,id){
    thumb_gal=g;
    thumb_id=id;
    thumb_next=1;
    thumb_el=el;
        // set image url
        for(i=1; i<=9; i++){
            var im=new Image();
            im.src=thumb_gal+"/"+i+"."+id+".jpeg";
            thumb_el.src = im.src;
        }
    video_thumb_image();
}
function video_thumb_stop(){
    if(thumb_timer!=null){
        clearTimeout(thumb_timer);
        thumb_timer=null;
    }
    var im=new Image();
    im.src=thumb_gal+"/1."+thumb_id+".jpeg";
    thumb_el.src = im.src;
}

function video_thumb_image(){   
    var im=new Image();
    im.src=thumb_gal+"/"+thumb_next+"."+thumb_id+".jpeg";
    thumb_next=(thumb_next)+1;
    if(thumb_next==9){ thumb_next = 1; }
    thumb_el.src=im.src;
    thumb_timer=setTimeout("video_thumb_image()",800);
}
/* /video img slideshow: */

function writeFB(FBlinka){
document.getElementById('iframeFB').innerHTML = '<iframe src="http://www.facebook.com/plugins/like.php?href='+FBlinka+'&amp;layout=button_count&amp;show_faces=true&amp;width=0&amp;action=like&amp;font=verdana&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:130px; height:21px;" ></iframe>';
}
