
//var numInputTexts = 0;
var inputsValues = {
};

//var moduleToOpen = 1;
//var isOpened = false;

$(function(){
	
	$('.STVMenuOption').mouseover(function(event){
		$(this).removeClass('STVMenuOptionOut');
		$(this).addClass('STVMenuOptionOver');
		event.preventDefault();
	});
	
	$('.STVMenuOption').mouseout(function(event){
		if($(this).attr('isOpened') == 'true') return;
		$(this).removeClass('STVMenuOptionOver');
		$(this).addClass('STVMenuOptionOut');
		event.preventDefault();
	});
	
	$('#subeTuVideoOption').click(function(event){
		// ini stats
		var i = document.createElement("img");
		i.src = "/media/img/stats/"+G_RN_IDLANGUAGE+"/upload1.gif";
		// end stats
		//open
		if ($(this).attr('isOpened') != 'true') {
			$(this).attr('isOpened', 'true');
			openFormModule1();
			/*if(moduleToOpen == 1) openFormModule1();
			else openFormModule2();*/
			event.preventDefault();
		}
		//close
		else {
			$(this).attr('isOpened', 'false');
			closeFormModules();
		}
	});
	
	$('#showNextModule').click(function(event){
		// ini stats
		var i = document.createElement("img");
		i.src = "/media/img/stats/"+G_RN_IDLANGUAGE+"/upload2.gif";
		// end stats
		openFormModule2();
		event.preventDefault();
	});
	
	$('#sendForm').click(function(event){
		sendForm();
		event.preventDefault();
	});
	
	$('.STVBtn').mouseover(function(){
		$(this).find('.STVBtnName').css('color', '#e9ef21');
		$(this).find('img').attr('src', '/media/img/common/stv/btnNextOver.png');
		//$(this).animate( {marginRight:2}, 10);
	});
	
	$('.STVBtn').mouseout(function(){
		$(this).find('li').css('color', '#FFF');
		$(this).find('img').attr('src', '/media/img/common/stv/btnNext.png');
		//$(this).animate( {marginRight:-2}, 10);
	});
	
	
	$('#openForm').click(function(event){
		$('#dialog').jqmHide();
		openFormModule2();
		event.preventDefault();
	});
	
	$('#uploadForm').submit(function(event){
		event.preventDefault();
	});
	
	
	
	/*
	 * FORM VALUES FILL
	 */
	$('input').focus(function(event){
		var hasToChange = (inputsValues[$(this).attr('name')] == $(this).val()) ? true : false;
		if(hasToChange) $(this).val('');
		$(this).removeClass('errorSkin');
	});
	
	$('input').blur(function(event){
		if($(this).val()=='') $(this).val(inputsValues[$(this).attr('name')]);
	});
	
	//prefill values data structures
	for(var k = 0; k < $('input').length; k++){
		if($('input')[k].type == 'text' && $('input')[k].value != ''){
			inputsValues[$('input')[k].name] = $('input')[k].value;
		}
	}
	
	$('#dialog').jqm({modal:true, overlay:30});
	
	

	$('.STVCLoseAlert').click(function(event){
		$('#dialog').jqmHide();
		event.preventDefault();
	});

	var win = getParameter('win');
	if(win == 1) showModalWindow(1);
	else{
		if(win == 2) showModalWindow(2, 0);
		if(win == 3) showModalWindow(2, 1);
		if(win == 4) showModalWindow(2, 2);
	}
	
	var layer = getParameter('layer');
	if (layer == 'video_upload') {
		var status = getParameter('status');
		showModalWindow(2, status);
	}
	
	
	
});



function showModalWindow(type, status){
	$('.windowContent').hide();
	if(type == 1){
		$('#msg1_Sending').show();
	}else if(type == 2){
		if (status < 0 || status > 2 || status == undefined || status == null || status == '') status = 0;
		$('#msg2_Response_' + status).show();
	}
	
	$('#dialog').jqmShow();
}



function closeFormModules(){
	$('#STVModule1').hide();
	$('#STVModule2').hide();
	$("#subeTuVideo").animate({ height: 396 }, 100 );
}

function openFormModule1(){
	//moduleToOpen = 1;
	$("#subeTuVideo").animate({ height: 615 }, 300, showFormModule1 );
}

function openFormModule2(){
	//moduleToOpen = 2;
	$('#STVModule1').hide();
	$("#subeTuVideo").animate({ height: 625 }, 100, showFormModule2 );
}


function showFormModule1(){
	$('#STVModule1').show();
}

function showFormModule2(){
	$('#STVModule2').show();
}




function sendForm(){
	for(var k = 0; k < $('input').length; k++){
		if($('input')[k].type == 'text' && $('input')[k].value == inputsValues[$('input')[k].name]){
			$('input')[k].value = '';
		}
	}
	$('#uploadForm').submit();
}


function getParameter(name){
    var loc = location.search;
    var pos = loc.indexOf(name);
    if (pos < 0) return null;
    var len = pos+name.length+1;
    var param = loc.substring(len);
    if (param.indexOf('&') > 0) param = param.substring(0, param.indexOf('&'));
	return param;
}











