$(document).ready(function(){ 
	$(".content-bottom2").height($(".mask").height());
	$("#searchAux").stop().animate({opacity: 0}, 1)
	$("#searchAux").hover(sobreAux, foraAux);
	$("#clientsLog").stop().animate({opacity: 0}, 1)
	$("#clientsLog").hover(sobreAuxReg, foraAuxReg);
	
	$(".scroll-bar").draggable({ 
		containment: ".scroll-move", 
		axis: "x",
		start: 	function(event, ui) { 
					arrossegarScroll=true;
				},
		stop: 	function(event, ui) { 
					arrossegarScroll=false;
					if(!arrossegarScroll){
						outScroll();
					}
				},
		drag: 	function(event, ui) { 
					moventScroll(event, ui);
				}
	});
	$(".scroll-bar").hover(overScroll, outScroll);
	$(".scroll-move").click(function(e){
			  var offset = $('.scroll-move').offset();
			  var x = e.pageX - offset.left -23; //23 per centrar
			  if(x<0){ x=0; }
			  if(x>634){ x=634; }
			var ample_spots_restant=$(".mask").width()-750;
			$(".mask").animate({"left": -ample_spots_restant*x/634}, 500, "easeOutQuad");
			$(".scroll-bar").animate({"left": x}, 500, "easeOutQuad");
		});
		
		
	$("#overVideo").css({ opacity: 0.75 });
	$("#videoContent").hover(sobreVideo, foraVideo);
	overVideo=setTimeout("amagarCartell()",2000);
	
	$(".linkVideo").click(function(e){
		e.preventDefault();
		urlPopUp = e.target.toString();
		window.open(urlPopUp,'','height=720,width=810,toolbar=no,scrollbars=yes,modal=yes');
	});
		
}); 

var overSearchAux=false;
var arrossegarScroll=false;
var sobreScroll=false;
var overVideo;

function sobreVideo(){
	clearTimeout(overVideo);
	$("#overVideo").stop().animate({opacity: .75}, 500)
}

function foraVideo(){
	overVideo=setTimeout("amagarCartell()",2000);	
}

function amagarCartell(){
	$("#overVideo").stop().animate({opacity: 0}, 500);
}

function inSearch(frase){
	if(document.formSearch.searchBox.value==frase){
		document.formSearch.searchBox.value="";	
	}
	$("#searchAux").show();
	$("#searchAux").stop().animate({opacity: 1}, 300);
}
function outSearch(frase){
	if(!overSearchAux){
		if(document.formSearch.searchBox.value==""){
			document.formSearch.searchBox.value=frase;	
		}
		$("#searchAux").stop().animate({opacity: 0}, 300).hide();
	}
}
function foraAux(){
	overSearchAux=false;
}
function sobreAux(){
	overSearchAux=true;
}

function inResgister(){
	$("#clientsLog").show();
	$("#clientsLog").stop().animate({opacity: 1}, 300);
	$("#zonaClientsBtn").removeClass("out").addClass("in");
	$("#zonaClientsArrow").attr("src","img/arrow-white.jpg");
}

function sobreAuxReg(){}
function foraAuxReg(){
	$("#zonaClientsBtn").removeClass("in").addClass("out");
	$("#zonaClientsArrow").attr("src","img/arrow-grey.jpg");
	$("#clientsLog").stop().animate({opacity: 0}, 300);
}


function overScroll(){
	sobreScroll=true;
	$(".scroll-bar").css({"backgroundPosition": "-17px left"});
}
function outScroll(){
	sobreScroll=false;
	if(!arrossegarScroll){
		$(".scroll-bar").css({"backgroundPosition": "top left"});
	}
}
function moventScroll(event, ui){
	var pos=ui.position;
	var ample_spots_restant=$(".mask").width()-750;
	$(".mask").css({"left": -ample_spots_restant*pos.left/634});
}
//0 to 634

function fScrollLeft(){
	var pos = $(".scroll-bar").position();
	pos=pos.left;
	pos-=100;
	if(pos<0){ pos=0;}
	var ample_spots_restant=$(".mask").width()-750;
	$(".mask").animate({"left": -ample_spots_restant*pos/634}, 500, "easeOutQuad");
	$(".scroll-bar").animate({"left": pos}, 500, "easeOutQuad");
		
}
function fScrollRight(){
	var pos = $(".scroll-bar").position();
	pos=pos.left;
	pos+=100;
	if(pos>634){ pos=634;}
	var ample_spots_restant=$(".mask").width()-750;
	$(".mask").animate({"left": -ample_spots_restant*pos/634}, 500, "easeOutQuad");
	$(".scroll-bar").animate({"left": pos}, 500, "easeOutQuad");
		
}

var videoAnterior=0;
function canviarVideo(id){
	if(videoAnterior!=0){
		$("#thumb"+videoAnterior).css({"border": "5px solid #e4e4e4"});
	}
	
	var _cos = '<object id="videoDiv" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="710" height="395">\
					<param name="movie" value="player.swf?spot=info/videos/'+id+'.flv" />\
               		<param name="wmode" value="transparent"></param>\
        			<!--[if !IE]>--><object type="application/x-shockwave-flash" data="player.swf?spot=info/videos/'+id+'.flv" width="710" height="395" wmode="transparent"><!--<![endif]-->\
					<div><p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p></div>\
					<!--[if !IE]>--></object><!--<![endif]-->\
				</object>';
	
	$("#video").html(_cos)	
	/*swfobject.registerObject("videoDiv", "10.0.0", "expressInstall.swf");*/
				swfobject.embedSWF("player.swf?spot=info/videos/"+id+".flv", "videoDiv", "710", "395", "10.0.0");
		
	$("#thumb"+id).css({"border": "5px solid #CB351B"});
	videoAnterior=id;
}

function contacto(){
	if((document.formulari.nom.value=="")||(document.formulari.email.value=="")){
		alert(_fillFields);
	}else{
		//enviar formulari
		$("#enviar").hide();
		$("#enviant").show();
		$.post("http://www.dcrimagenes.com/php_aux/email_contacte.php", { nom: document.formulari.nom.value, cognoms: document.formulari.cognoms.value, email: document.formulari.email.value, telefon: document.formulari.telefon.value, comentaris: document.formulari.comentaris.value },
		function(data){
			$("#enviant").hide();
			if(data=="OK"){
				$("#enviatOK").show();				
			}else{
				$("#enviatKO").show();	
			}
			setTimeout("reiniciarForm()", 4000);
		});	
	}
}

function reiniciarForm(){
	document.formulari.nom.value="";
	document.formulari.cognoms.value="";
	document.formulari.email.value="";
	document.formulari.telefon.value="";
	document.formulari.comentaris.value="";	
	$("#enviatOK").hide();	
	$("#enviatKO").hide();	
	$("#enviar").show();	
}


function checkUser(oEvento){ 
    var iAscii;      
    if (oEvento.keyCode) 
        iAscii = oEvento.keyCode; 
    else if (oEvento.which) 
        iAscii = oEvento.which; 
    else 
        return false;          
    if (iAscii == 13) {
		confirmUser();
        return false; 
	}
	return true;
}

function confirmUser(){
	$.post("http://www.dcrimagenes.com/php_aux/validateUser.php", { username: document.formUser.username.value, password: document.formUser.password.value},
	function(data){
		if(data=="OK"){
			document.location.href=document.formUser.action;
		}else{
			alert(_userKO);
			document.formUser.username.value="";
			document.formUser.password.value="";
		}
	});	
}
function sendSearch(){ 
	if(document.formSearch.searchBox.value!=""){
		return true;
	}else{
		return false;
	}
} 

function parentTo(url){
	window.opener.location.href = url;	
}

function newSearch(search_url, num_tags){
	paraules="";
	for(var i=0;i<num_tags;i++){
		if(document.tagsForm["tag_"+i].checked){
			paraules+=document.tagsForm["tag_"+i].value+" ";
		}
	}
	var url =search_url+"?searchBox="+paraules+"&DG=1&free=1&contem=1&histor=1";
	window.opener.location.href = url;	
}


function calculatePrice(id_video){
	$.post("http://www.dcrimagenes.com/php_aux/preu.php", { projecte: document.calculator.projecte.value, distribucio: document.calculator.distribucio.value, us: document.calculator.us.value, temps: document.calculator.temps.value, id_video: id_video},
	function(data){
		if(data=="0"){
			document.getElementById("priceKO").style.display="block";	
			document.getElementById("priceOK").style.display="none";	
		}else{
			$("#preu").html(data+" &euro;");	
			$("#priceOK").show();	
			$("#priceKO").hide();	
		}
	});	
}

function popUpPrivate(video) {	
	day = new Date();	
	id = day.getTime();	
	eval("page" + id + " = window.open('" + video + "', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=750,height=550,left = 200,top = 150');");
}
