// JavaScript Document

// DE AICI EDITEZI CATEGORIILE

if( window.location.href == "http://bogdanciobanu.ro/" ) window.location.replace("http://www.bogdanciobanu.ro");


var categoria1 = 'nunta';
var categoria2 = 'nunta*';
var categoria3 = 'eveniment';
var categoria4 = 'special';

// DE AICI NU MAI UMBLI

$(".content").hide(0);
$(".home").hide(0);

var pos = 1, cat_i = 0, scrollShow = 0, firstScroll = 0, stTop = 0 ;
var sending = false;
var url = '';
var mv_mat = new Array();

$(document).ready(function() {
	var height = $(window).height();
	var margin = Math.floor( (height - 666) / 2 );
	if( (height - 666 ) < 0 )
		margin = 0;
	$(".margin").css("height", margin + "px");
	$(".content").show();
	start_func();
	$.ajax({
		type: "GET",
		url: "movies.xml",
		dataType: "xml",
		success: function(xml) {
			$(xml).find('movie').each(function(){
				mv_mat[cat_i] = new Array();
				mv_mat[cat_i][0] = $(this).find('cat').text();
				mv_mat[cat_i][1] = $(this).find('title').text();
				url = $(this).find('url').text();
				mv_mat[cat_i][2] = "http://vimeo.com/moogaloop.swf?clip_id=" + url + '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=&amp;fullscreen=1';
				mv_mat[cat_i][3] = $(this).find('desc').text();
				if( mv_mat[cat_i][3] == 'n/a' )
					mv_mat[cat_i][3] = '';
				mv_mat[cat_i][4] = $(this).find('alt').text();
				$("#data").append('<img src="' + mv_mat[cat_i][4] + '" />');
				$(".gallery").html('<div class="cats"><div class="cat1">' + categoria1 + '</div><div class="cat2">' + categoria2 + '</div><div class="cat3">' + categoria3 + '</div><div class="cat4">' + categoria4 + '</div></div><div class="active_spacer">&nbsp;</div><div class="cat_scroll"><div class="cat_content">&nbsp;</div></div><div class="scroll_area"><div class="active_spacer">&nbsp;</div><div class="active_scroll_area"><img class="scrollbary" src="http://www.bogdanciobanu.ro/images/bar.png" /></div</div>');
				cat_i++;
			});
		}
	});
});

$(window).resize(function() {
	$(".content").hide();
	var height = $(window).height();
	var margin = Math.floor( (height - 666) / 2 );
	if( (height - 666 ) < 0 )
		margin = 0;
	$(".margin").css("height", margin + "px");
	$(".content").show();
});

function call_page(val) {
	switch(val)
		{
			case 1: change_main(); break;
			case 2: change_gal(); break;
			case 3: change_cont(); break;
		}
}

function start_func() {
	$(".home").hide();
	$(".active_cont").animate({
        width: "10%",
        opacity: 0.2
      }, 0 );
	$(".active_cont").animate({
        width: "100%",
        opacity: 1
      }, 4500, function() {
				$(".home").animate({opacity: 0.0}, 0 );
				$(".home").show();
				$(".home").animate({opacity: 1.0}, 1500 );
	  		}
		  );
}

function change_main() {
	if( scrollShow == 1 )
		{
			$(".scrollbary").animate({opacity: 0.0}, 500);
			$(".scroll_area").animate({opacity: 0.0}, 500, function()
						{
							$(".scrollbary").hide();
							$(".scroll_area").hide();
							scrollShow = 0
						});
		}
	switch(pos)
		{
			case 1:
				chamge_none();
				break;
			case 2:
				$(".gallery").animate({opacity: 0.0}, 1500, function()
					{
						$(".gallery").hide();
				$(".gallery").html('<div class="cats"><div class="cat1">' + categoria1 + '</div><div class="cat2">' + categoria2 + '</div><div class="cat3">' + categoria3 + '</div><div class="cat4">' + categoria4 + '</div></div><div class="active_spacer">&nbsp;</div><div class="cat_scroll"><div class="cat_content">&nbsp;</div></div><div class="scroll_area"><div class="active_spacer">&nbsp;</div><div class="active_scroll_area"><img class="scrollbary" src="http://www.bogdanciobanu.ro/images/bar.png" /></div</div>');
						$(".home").animate({opacity: 0.0}, 0 );
						$(".home").show();
						$(".home").animate({opacity: 1.0}, 1500 );
				  	}
				);
				break;
			case 3:
				$(".contact").animate({opacity: 0.0}, 1500, function()
					{
						$(".contact").hide();
						$(".contact_form").html('');
						$(".contact_sending").html('');
						$(".contact_error").html('');
						$(".contact_sent").html('');
						$(".home").animate({opacity: 0.0}, 0 );
						$(".home").show();
						$(".home").animate({opacity: 1.0}, 1500 );
				  	}
				);
				break;
		}
	pos = 1;
}

function change_gal() {
	switch(pos)
		{
			case 1:
				$(".home").animate({opacity: 0.0}, 1500, function()
					{
						$(".home").hide();
						$(".gallery").animate({opacity: 0.0}, 0 );
						$(".gallery").show();
						$(".gallery").animate({opacity: 1.0}, 1500, function()
							{
								$(".cat1").click( function() {
									$(".cat_content").animate({opacity: 0.0}, 1500, function()
										{
											$(".cat_content").html('');
											$(".cat_content").animate({opacity: 1.0}, 0 );
											call_gallery(1);
										}
									);
								});
								$(".cat2").click( function() {
									$(".cat_content").animate({opacity: 0.0}, 1500, function()
										{
											$(".cat_content").html('');
											$(".cat_content").animate({opacity: 1.0}, 0 );
											call_gallery(2);
										}
									);
								});
								$(".cat3").click( function() {
									$(".cat_content").animate({opacity: 0.0}, 1500, function()
										{
											$(".cat_content").html('');
											$(".cat_content").animate({opacity: 1.0}, 0 );
											call_gallery(3);
										}
									);
								});
								$(".cat4").click( function() {
									$(".cat_content").animate({opacity: 0.0}, 1500, function()
										{
											$(".cat_content").html('');
											$(".cat_content").animate({opacity: 1.0}, 0 );
											call_gallery(4);
										}
									);
								});
								$(".cat_content").html('');
								call_gallery(1);
							}
						);
				  	}
				);
				break;
			case 2:
				change_none();
				break;
			case 3:
				$(".contact").animate({opacity: 0.0}, 1500, function()
					{
						$(".contact").hide();
						$(".contact_form").html('');
						$(".contact_sending").html('');
						$(".contact_error").html('');
						$(".contact_sent").html('');
						$(".gallery").animate({opacity: 0.0}, 0 );
						$(".gallery").show();
						$(".gallery").animate({opacity: 1.0}, 1500, function()
							{
								$(".cat1").click( function() {
									$(".cat_content").animate({opacity: 0.0}, 1500, function()
										{
											$(".cat_content").html('');
											$(".cat_content").animate({opacity: 1.0}, 0 );
											call_gallery(1);
										}
									);
								});
								$(".cat2").click( function() {
									$(".cat_content").animate({opacity: 0.0}, 1500, function()
										{
											$(".cat_content").html('');
											$(".cat_content").animate({opacity: 1.0}, 0 );
											call_gallery(2);
										}
									);
								});
								$(".cat3").click( function() {
									$(".cat_content").animate({opacity: 0.0}, 1500, function()
										{
											$(".cat_content").html('');
											$(".cat_content").animate({opacity: 1.0}, 0 );
											call_gallery(3);
										}
									);
								});
								$(".cat4").click( function() {
									$(".cat_content").animate({opacity: 0.0}, 1500, function()
										{
											$(".cat_content").html('');
											$(".cat_content").animate({opacity: 1.0}, 0 );
											call_gallery(4);
										}
									);
								});
								$(".cat_content").html('');
								call_gallery(1);
							}
						);
				  	}
				);
				break;
		}
	pos = 2;
}

function change_cont() {
	if( scrollShow == 1 )
		{
			$(".scrollbary").animate({opacity: 0.0}, 500);
			$(".scroll_area").animate({opacity: 0.0}, 500, function()
						{
							$(".scrollbary").hide();
							$(".scroll_area").hide();
							scrollShow = 0
						});
		}
	$(".contact_form").show();
  	$(".contact_sending").hide();
  	$(".contact_sent").hide();
	switch(pos)
		{
			case 1:
				$(".home").animate({opacity: 0.0}, 1500, function()
					{
						$(".home").hide();
						$(".contact").animate({opacity: 0.0}, 0 );
						contact_create();
						$(".contact").show();
						$(".contact").animate({opacity: 1.0}, 1500 );
				  	}
				);
				break;
			case 2:
				$(".gallery").animate({opacity: 0.0}, 1500, function()
					{
						$(".gallery").hide();
				$(".gallery").html('<div class="cats"><div class="cat1">' + categoria1 + '</div><div class="cat2">' + categoria2 + '</div><div class="cat3">' + categoria3 + '</div><div class="cat4">' + categoria4 + '</div></div><div class="active_spacer">&nbsp;</div><div class="cat_scroll"><div class="cat_content">&nbsp;</div></div><div class="scroll_area"><div class="active_spacer">&nbsp;</div><div class="active_scroll_area"><img class="scrollbary" src="http://www.bogdanciobanu.ro/images/bar.png" /></div</div>');
						$(".contact").animate({opacity: 0.0}, 0 );
						contact_create();
						$(".contact").show();
						$(".contact").animate({opacity: 1.0}, 1500 );
				  	}
				);
				break;
			case 3:
				chamge_none();
				break;
		}
	pos = 3;
}

function change_none() {
}

function call_gallery(cat) {
	var categ, elem;
	var nbr = 0;
	var l = mv_mat.length;
	var rows = 0;
	if( scrollShow == 1 )
		{
			$(".scrollbary").animate({opacity: 0.0}, 500);
			$(".scroll_area").animate({opacity: 0.0}, 500, function()
						{
							$(".scrollbary").hide();
							$(".scroll_area").hide();
							scrollShow = 0
						});
		}
	switch(cat)
		{
			case 1: categ=categoria1; break;
			case 2: categ=categoria2; break;
			case 3: categ=categoria3; break;
			case 4: categ=categoria4; break;
		}
	for( elem=0; elem<l; elem++ )
		if(mv_mat[elem][0] == categ)
			{
				$(".cat_content").append('<div class="clip" id="clp' + nbr + '"><div class="clip_bottom"><a class="inline2" href="#data" title="' + mv_mat[elem][1] + '">' + mv_mat[elem][1] + '</a></div><div class="clip_top" id="clp_' + nbr + '"><a class="inline1" href="#data" title="' + mv_mat[elem][1] + '"><img class="elem_img" id="img_clp' + nbr + '" src="' + mv_mat[elem][4] + '" alt="' + mv_mat[elem][1] + '" title="' + mv_mat[elem][1] + '"  /></a></div><div class="clip_spacer">&nbsp;</div><div id="transport' + nbr + '" class="' + elem + '"> </div></div>');
				nbr++;
				if( nbr % 3 == 0 )
					{
						$(".cat_content").append('<br /><div class="active_spacer">&nbsp;</div>');
						rows = rows + 1;
					}
			}
	if( nbr % 3 == 0 )
		rows = rows - 1;
	if( nbr == 0 )
		rows = 0;
		else
			rows = rows + 1;
	rows = rows * 130;
	show_imgs(0, nbr, rows);
}

function show_imgs(ind, nr, rws)
	{
		var ind_mov = $("#transport" + ind).attr("class");
		$("#clp" + ind).animate({opacity: 0.0}, 0);
		$("#clp" + ind).show();
		var tz = ind;
		$("#clp_" + tz).hover(
			function () {
				$("#img_clp" + tz).animate({
					opacity:1.0,
					width:"120px",
					height:"90px",
					marginTop:"0px",
					marginLeft:"0px"}, 500);
					$("#data").html('<div class="header_spacer">&nbsp;</div><object class="movedright" width="520" height="416"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="' + mv_mat[ind_mov][2] + '" /><embed src="' + mv_mat[ind_mov][2] + '" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="520" height="416"></embed></object><p class="movie_description">' + mv_mat[ind_mov][3] + '</p>');
			},
			function () {
				$("#img_clp" + tz).animate({
					opacity:0.7,
					width:"100px",
					height:"75px",
					marginTop:"7px",
					marginLeft:"10px"}, 500);
			}
		);
		$("#clp" + ind).animate({opacity: 1}, 300, function() {
			$("#img_clp" + ind).animate({opacity: 0.7}, 300);
			ind++;
			if(ind < nr)
				show_imgs(ind, nr, rws);
				else
					start_inline(rws);
		});
	}
	
function start_inline(rs) {
	$("a.inline1").fancybox({
			'hideOnContentClick': true,
			'frameWidth': 540,
			'frameHeight': 540
		});
	$("a.inline2").fancybox({
			'hideOnContentClick': true,
			'frameWidth': 540,
			'frameHeight': 540
		});
	if( rs > 400 )
		start_scroll(rs);
}

function start_scroll(rs) {
	var top = $("#scrollbary").offset().top;
	$(".scrollbary").animate({opacity: 0.0}, 0 );
	$(".scroll_area").animate({opacity: 0.0}, 0 );
	$(".scrollbary").show();
	$(".scroll_area").show();
	$(".scrollbary").animate({opacity: 1.0}, 500);
	$(".scroll_area").animate({opacity: 1.0}, 500, function () {
		$(".scrollbary").draggable({
			axis: 'y',
			containment: 'parent',
			cursor: 'pointer',
			opacity: 0.7,
			start: function(event, ui) {
				if( firstScroll == 0 )
					stTop = $(".scrollbary").offset().top;
				firstScroll = 1;
			},
			drag: function(event, ui) {
				top = $(".scrollbary").offset().top - stTop;
				scroll_movies(top, rs);
			}
		});														 
	});
	scrollShow = 1;
}

function scroll_movies(y, h) {
	var k = (h - 400) / 290;
	var top = (-1) * Math.ceil(k*y);
	$(".cat_content").css("marginTop", top);
}

function contact_create() {
	$(".contact_form").html('<p class="titlu_formular"><strong>Formular de contact:</strong></p><form action="index.php" id="ajaxForm"><div class="container_contact"><div class="form_spacer">&nbsp;</div><label for="name" class="lab">Nume: </label><input class="inp" type="text" name="name" id="name" size="30" value="" /><div class="error_name"><p class="error1">Acest camp este obligatoriu</p></div><label for="mail" class="lab">E-mail: </label><input class="inp" type="text" name="mail" id="mail" size="30" value="" /><div class="error_mail"><p class="error2">Acest camp este obligatoriu</p><p class="error3">Acest camp nu este completat corespunzator</p></div><label for="phon" class="lab">Telefon: </label><input class="inp" type="text" name="phon" id="phon" size="30" value="" /><div class="error_phone"><p class="error4">Acest camp este obligatoriu</p><p class="error5">Acest camp nu este completat corespunzator</p></div><label for="text" class="lab">Mesaj: </label><textarea class="area" name="text" id="text" rows="4" cols="30"></textarea><div class="error_text"><p class="error6">Acest camp este obligatoriu</p></div><input class="subbut" type="submit" value="Trimite" /></div></form>');
	$(".contact_sending").html('<img src="http://www.bogdanciobanu.ro/images/loading.gif" alt="Bogdan Ciobanu - filmari nunti si evenimente iasi - trimitere formular" title="Bogdan Ciobanu - filmari nunti si evenimente iasi - trimitere formular" class="loading" />');	
	$(".contact_error").html('<div class="mail_spacer">&nbsp;</div><div class="mail"><div class="mail_pic"><img src="http://www.bogdanciobanu.ro/images/mailfail.png" alt="Eroare la trimiterea mesajului" title="Eroare la trimiterea mesajului" /></div><div class="mail_text"><p class="mail1">A fost receptionata o eroare de comunicare!</p><p class="mail2">Va rugam reincercati sa trimiteti mesajul!</p></div></div>');
	$(".contact_sent").html('<div class="mail_spacer">&nbsp;</div><div class="mail"><div class="mail_pic"><img src="http://www.bogdanciobanu.ro/images/mailsent.png" alt="Mesaj trimis cu succes" title="Mesaj trimis cu succes" /></div><div class="mail_text"><p class="mail1">Mesajul dvs. a fost trimis cu succes!</p><p class="mail2">Va voi raspunde cat de repede cu putiinta!</p></div></div>');
	$("input[name='name']").val("");
	$("input[name='mail']").val("");
	$("input[name='phon']").val("");
	$("textarea[name='text']").val("");
	$("#ajaxForm").submit(function() {
		if(sending == false)
			{
				sending = true;
				validate_form();
			}
	    return false;
    });
}

function validate_form() {
	var str = $("#ajaxForm").serialize();
	var name = $("input[name='name']").val();
	var mail = $("input[name='mail']").val();
	var phon = $("input[name='phon']").val();
	var text = $("textarea[name='text']").val();
	var valid = true;
	
	$(".error1").hide();
	$(".error2").hide();
	$(".error3").hide();
	$(".error4").hide();
	$(".error5").hide();
	$(".error6").hide();
	
	var i=0;
	
	if( text == "" )
		{
			$(".error6").show();
			$("textarea[name='text']").focus();
			valid = false;
		}
		
	if( phon == "" )
		{
			$(".error4").show();
			$("input[name='phon']").focus();
			valid = false;
		}
		else
			{
				if( phon.length != 10 )
					{
						$(".error5").show();
						$("input[name='phon']").focus();
						valid = false;
					}
				for( i=0; i<phon.length; i++ )
					if( isNaN(phon[i]) )
						{
							$(".error5").show();
							$("input[name='phon']").focus();
							valid = false;
							alert(phon[i] + i);
						}
			}
			
	if( mail == "" )
		{
			$(".error2").show();
			$("input[name='mail']").focus();
			valid = false;
		}
		else
			{
				if( mail.indexOf("@") == -1 || mail.indexOf("@") != mail.lastIndexOf("@") || mail.indexOf(".") == -1 || mail.indexOf(".") != mail.lastIndexOf(".") || mail.indexOf(".") - mail.indexOf("@") <= 2 || mail.indexOf("@") == 0 || mail.indexOf(".") == 0 )
					{
						$(".error3").show();
						$("input[name='mail']").focus();
						valid = false;
					}
			}
			
	if( name == "" )
		{
			$(".error1").show();
			$("input[name='name']").focus();
			valid = false;
		}
		
	if( valid == true )
		callAjax(str);
	
	sending = false;
	return false;
}

function callAjax(str)
	{
		$(".contact_form").hide(0);
		$(".contact_sending").show(0);
		$(".loading").show(0);
		$.ajax({
			type: "POST",
			url: "sendpost.php",
			data: str,
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				$(".contact_form").fadeOut(0);
				$(".contact_form").hide(0);
				$(".loading").fadeOut(500);
				$(".contact_sending").fadeOut(500, function() {
					$(".contact_error").fadeOut(0);
					$(".contact_error").show(0);
					$(".loading").hide(0);
					$(".contact_sending").hide(0);
			    	$(".contact_error").fadeIn(500); 
			    });
			},
			success: function() {
				$(".loading").fadeOut(500);
				$(".contact_sending").fadeOut(500, function() {
					$(".contact_sent").fadeOut(0);
					$(".contact_sent").show(0);
					$(".loading").hide(0);
					$(".contact_sending").hide(0);
			    	$(".contact_sent").fadeIn(500); 
			    });
			}   
		}); 
	}