jQuery(document).ready(function($) {

	$('.webtvsearch #mainSearch').bind('submit', function(){
		return false;
	});

	$('#statchan .top *').click(function(){
		if($(this).parents('.top').hasClass('open')){
			$(this).parents('.top').removeClass('open');
			$(this).parents('.top').next().hide().next().hide();
		} else {
			$(this).parents('.top').addClass('open');
			$(this).parents('.top').next().show().next().show();
		}
		return false;
	});
	
	$('#statchan .top .btn_toggle').click();
	
	var t;
	
	$('#googlemap_location_str').keyup(function(){
		clearTimeout(t);
		if($(this).val().length > 3){
			t = setTimeout(function(){
				$('#googlemap_findLocation').click()	
			},500);
		}
	});
	
	$('.webtvsearch #search').val('Videos suchen');
	
	$('.webtvsearch #search').bind('click focus', function(){
		if($(this).val() == 'Videos suchen'){
			$(this).val('');
		}
	});
	
	$('.webtvsearch #search').bind('blur', function(){
		if($(this).val() == ''){
			$(this).val('Videos suchen');
		}
	});
	
	$('.webtvsearch #send').bind('click', function(){
		changeSearch();
		return false;
	});
    
    $('#btn_dashboard').click(function(){
    	if($('#header').hasClass('loggedin')){
	    	if($(this).hasClass('closed')) {
	    		$('#dashboard').animate({
					height: 96
				}, 500, function(){
					$('#btn_dashboard').removeClass('closed');
					$('#userinfo_menu').fadeTo(500,1);
				});
	    	} else {
	    		$('#userinfo_menu').fadeTo(500,0, function(){
	    			$('#dashboard').animate({
						height: 29
					}, 500, function(){
						$('#btn_dashboard').addClass('closed');
					});
	    		});
	    	}
	    } else {
	    	openLogin();
	    }
	    return false;
    });
    
    $('.btn_login').click(function(){
    	openLogin();
    	return false
    });
    
    /* $('.import input[type=button]').click(function(){
    	load();
    });*/
    
    
    $('select').uniform();
    
//    $('#btn_dashboard').click();
    
    //$('#media-list .image img').reflect({height: 1, opacity: 0.3});
    
    $('.contestdetail .iframe .nav .item').click(function(){
    	var newitem = $(this);
    	var newrel = newitem.attr('rel');
    	var oldrel = $('.contestdetail .iframe .nav .act').attr('rel');
    	var olditem = $('.contestdetail .iframe .window.'+oldrel);
    	if(oldrel != newrel){
    		$('.contestdetail .iframe .nav .'+oldrel).removeClass('act');
    		$('.contestdetail .iframe .nav .'+newrel).addClass('act');
    		olditem.stop().fadeTo(500,0,function(){
    			$('.contestdetail .iframe .window.'+newrel).stop().fadeTo(500,1);
    		});
    	}
    	return false;
    });
    
     
    
    $('.upload input[type=text]').bind('click focus',function(){
    	if($(this).val() == 'Dein Youtube / Vimeo-Link'){
    		$(this).val('');
    	}
    });
    
    $('.upload input[type=text]').bind('blur',function(){
    	if($(this).val() == ''){
    		$(this).val('Dein Youtube / Vimeo-Link');
    	}
    });
    
    $('.videoranking .title').click(function(){
    	if(!$(this).parent().hasClass('open')){
    		var rel = $(this).parents('.column').attr('class').split(' ');
	    	var rels = '.'+rel[0]+'.'+rel[1];
	    	$('.videoranking '+rels+' .open .txt').animate({
	    		height: '0px'
	    	}, 200, function(){
	    		$(this).parents('.image').removeClass('open');
	    	});
	    	$(this).siblings('.txt').css({
	    		height: '0px',
	    		display: 'block'
	    	});
	    	$(this).parents('.image').addClass('open');
	    	$(this).siblings('.txt').animate({
	    		height: '45px'
	    	}, 200);
    	}
    });
    
    $('.videoranking .viewed .image:first .title').click();
    $('.videoranking .liked .image:first .title').click();
    $('.videoranking .hype .image:first .title').click();
    
    $('#media-filter.uploads li a').click(function(){
    	$('#media_upload .info').show();
    	var form = $(this).attr('rel');
    	$('#media-filter.uploads li').removeClass('act');
    	$('#media_upload .form').removeClass('open');
    	$('#media-filter.uploads li.'+form).addClass('act');
    	$('#media_upload .'+form+'_form').addClass('open');
    	if(form == 'info'){
    		$('#media_upload .info').hide();
    	}

		var mediatype = 0;
		switch (form) {
			case 'vimeo':
				mediatype = 6;
				break;
			case 'youtube':
				mediatype = 4;
				break;
			case 'embed':
				mediatype = 5;
				break;
			default:
				mediatype = 0; // upload and catch-all
				break;
		}

		$('#media_mediatype').val(mediatype);

    	return false;
    });
       
    // hand events over to input-field
    $('.required span').bind('click', function(){
    	if (console) console.debug('Event label->input');
    	var label = $(this).siblings('label');
    	var name = label.attr('for');
    	var input = $('input[id='+name+'], textarea[id='+name+']');
    	input.focus();
    });
    
    
    
    // new playground stefan@codeon.eu
	jQuery('input[type=text], input[type=password], textarea').bind('blur focus', function(e){
		if (this.name && (this.name.indexOf('signin') != -1)) return;
	
		var id = this.id; //get('id');
		var label = $('label[for='+id+']');
		var labelRegion = label;
	
		// hide whole paragraph for required labels (contain span with text "reqqired also")
		if (label.parent().hasClass('required'))
			labelRegion = label.parent();
	
		//if (console) console.debug(id, e.type);
	
		if (e.type == 'focus') {
			//labelRegion.hide();
			labelRegion.css('visibility', 'hidden');
			labelRegion.css('z-index', '-20');
		} else if (e.type == 'blur') {
			if (this.value == '') {
				labelRegion.css('visibility', '');
				labelRegion.css('z-index', '20');
			}
		} 
	});
	
	// set initially
    refreshInputfields();
    
    if (jQuery('#googlemap_location_str').val() != ''){
   		jQuery('label[for=googlemap_location_str]').hide();
   	}
});

function refreshInputfields() {
	//jQuery('input[type=text], input[type=password], textarea').focus();
	
	//hack for sweepstakes label disappear
	jQuery('#sweepstakes_name').focus();
	jQuery('#sweepstakes_email').focus();
	//hack for search
	jQuery('#search_title').focus();	
	jQuery('#search_description').focus();
	jQuery('#search_tags').focus();
	//hack for profile
	jQuery('#profile_email').focus();
	jQuery('#profile_username').focus();
	jQuery('#profile_firstname').focus();
	jQuery('#profile_lastname').focus();
	jQuery('#profile_city').focus();
	
	
	jQuery('input[type=password]').focus();
    jQuery('input[type=text], input[type=password], textarea').blur();    
}

/* jQuery(window).load(function(){
	 load();
}); */


function openLogin(){
	if(jQuery('.void').hasClass('closed')){
		jQuery('.void').animate({
			height: 120
		}, 1000, function(){
			jQuery('.void').removeClass('closed');	
		});
	} else {
		jQuery('.void').animate({
			height: 0
		}, 1000, function(){
			jQuery('.void').addClass('closed');
		});
	}
}

function doUpload(){
	if (window.location.href.indexOf('youtube') != -1){
		var urlParts = window.location.href.split('/');
		var url = urlParts[urlParts.length-1];
		if(decodeURI(url) != '1'){
			jQuery('.youtube_form').addClass('open');
			jQuery('#media_youtube_id').val(decodeURI(url));
			jQuery('#media-filter li.youtube').addClass('act');
			jQuery('#media_mediatype').val(4);
		} else {
			jQuery('.info_form').addClass('open');
			jQuery('#media-filter li.info').addClass('act');	
		}	
	} else if(window.location.href.indexOf('vimeo') != -1){
		var urlParts = window.location.href.split('/');
		var url = urlParts[urlParts.length-1];
		if(decodeURI(url) != '1'){
			jQuery('.vimeo_form').addClass('open');
			jQuery('#media_vimeo_id').val(decodeURI(url));
			jQuery('#media-filter li.vimeo').addClass('act');
			jQuery('#media_mediatype').val(6);
		} else {
			jQuery('.info_form').addClass('open');
			jQuery('#media-filter li.info').addClass('act');
		}
		
	} else { // not 100% correct, since embed & file-upload are thrown together..
		jQuery('.info_form').addClass('open');
		jQuery('#media-filter li.info').addClass('act');
	}
}

