//gloabal variables
var big_gallery_index=1;

var menu;
var sub_menu;
var pic_from;
var pic_to;
var pic_of;
var page;

var username_available = false;

var time = 500;

var image_to_del = new Object;

$(function(){


																										/* NAVIGATION BAR */

	$('.top_nav_link').hover(

			function(){

							var el = this;
	
							var el_cl = $(el).parent().parent().attr('class');
							var body_id = $('body').attr('id');

							if( el_cl != body_id )
							{
								var src1 = 'images/layout/topnav_yellow_left_m.gif';
								var src2 = 'images/layout/topnav_yellow_right_m.gif';

								$(el).parent().css('background-color','#FFCC00'); //yellow
								$(el).css('font-size','17px');

								$(el).parent().prev().css('backgroundImage','url(' + src1 +')');
								$(el).parent().next().css('backgroundImage','url(' + src2 +')');
							}

						},
			function(){
							var el = this;
	
							var el_cl = $(el).parent().parent().attr('class');
							var body_id = $('body').attr('id');
	
							if( el_cl != body_id )
							{
								
								if($.browser.msie || $.browser.version == '6.0')
								{
									var src1 = 'images/layout/topnav_dark_left(or).gif';
									var src2 = 'images/layout/topnav_dark_right(or).gif';
								}
								else
								{
									var src1 = 'images/layout/topnav_dark_left.gif';
									var src2 = 'images/layout/topnav_dark_right.gif';
								}
								
								
		
								if($(el).hasClass('end_menu_left'))
								{
									src1 = 'images/layout/topnav_endmenu_left.gif';
								}
								else if($(el).hasClass('end_menu_right'))
								{
									src2 = 'images/layout/topnav_endmenu_right.gif';
								}
		
								$(el).parent().css('background-color','');
								$(el).css('font-size','14px');
		
								$(el).parent().prev().css('backgroundImage','url(' + src1 +')');
								$(el).parent().next().css('backgroundImage','url(' + src2 +')');
							}
						}

	);

	//take the thumbnail in the homepage and go straight to the relative gallery with that image as a main image
	$('body#home #imgs_gallery_l_mod img, body#home #jigsaws_gallery_l_mod img, body#home #fav_jigsaws_gallery_l_mod img, body#home #most_viewed_jigsaws_l_mod img').click(function(){

		var pic = this;

		pic_id = $(pic).attr('id');
		
		//console.log(pic_id.indexOf('_'));
		
		var pic_id = pic_id.split('_',2);
		
		pic_id = pic_id['1']
		
		//console.log(pic_string['1']);

		//pic_id = pic_id.replace('id_','');
		pic_src = $(pic).attr('src');

		var page;

		(pic_src.indexOf('jigsaws') == 17)? page = 'jigsaw_gallery.php': page = 'image_gallery.php';

		//$.post('php-script/change_working_image.php',{id_pic: pic_id},function(){
		$.ajax({
			type: "POST",
			url: "php-script/change_working_image.php",
			data: 'id_pic=' + pic_id,
			async: false,
			success: function(response){
					location.href = page;
				}
		});
				//location.href = page;
			//});

		
	});


															/* QUICK GALLERY LINK */
$('.quick_gallery_link').click(function(){
	var this_el = this;
	var nam = $(this_el).attr('name');

	this_el = nam.split('-');

	go_to_proper_gallery(this_el['0'],'most_viewed',this_el['1'],1);

});


															/* MODIFY MAIN IMAGE ON THUMBNAIL CLICK */
	$('body#home #little_gallery .homepage_lead_right_frame img,body#image_gallery #big_gallery_container .gallery_thumb img, body#jigsaw_gallery #big_gallery_container .gallery_thumb img').click(function(){

		var pic = this;
		var pic_file_name = $(pic).attr('name');
		var pic_id = $(pic).attr('id');
		var pic_title = $(pic).attr('alt');
		var pic_src = $(pic).attr('src');

		var modify_jigsaw_det;
		
		if( $('body').attr('id') == 'jigsaw_gallery')
		{
			$('#lead_form .two').slideDown('2000');
			var path = 'images/galleries/jigsaws/medium/';
			var path2 = 'images/galleries/jigsaws/large/';
			modify_jigsaw_det = true; //modify jigsaw details
		}
		else if(pic_src.indexOf('jigsaws') == 17 )
		{
			var path = 'images/galleries/jigsaws/medium/';
			var path2 = 'images/galleries/jigsaws/large/';
		}
		else
		{
			var path = 'images/galleries/images/medium/';
			var path2 = 'images/galleries/images/large/';
			modify_jigsaw_det = false;
		}
	
		var pre_id = 'main_image_id_';

		$('#slide_show img').attr({
											src: path + pic_file_name,
											name: pic_file_name
											});

		pic_id = pic_id.replace('id_','');

		$('#slide_show a').attr({
										href: path2 + pic_file_name,
										title: pic_title/*,
										id: pre_id + pic_id*/
										});
		
		$('#lead_form .three .step1').hide();
		$('#lead_form .three .step3').hide();
		$('#lead_form .three .step2').show();
		$.post('php-script/change_step.php',{step: "2"});
		
		//pic_id = pic_id.replace('id_','');
		
		var j_size;
		var j_pieces;
		
		if( modify_jigsaw_det == true && data['pictures'] != null )
		{
			$.each(data['pictures'],function(){
			if(this.image_id == pic_id)
			{
				j_size = this.size;
				j_pieces = this.pieces;

				//changing the details
				$('#info-payment_jigsaw_gallery .info li.one span.jigsaw_details').text(pic_title);
				$('#info-payment_jigsaw_gallery .info li.two span.jigsaw_details').text(j_pieces);
				$('#info-payment_jigsaw_gallery .info li.three span.jigsaw_details').text(j_size);
			}
			});
		}
		else if(modify_jigsaw_det == true)
		{
			$.post('php-script/get_jigsaw_details.php',{id_pic: pic_id},function(response){
																										var jigsaw_size_details = response.split(',');
																										j_size = jigsaw_size_details['0'];
																										j_pieces = jigsaw_size_details['1'];
														//changing the details
														$('#info-payment_jigsaw_gallery .info li.one span.jigsaw_details').text(pic_title);
														$('#info-payment_jigsaw_gallery .info li.two span.jigsaw_details').text(j_pieces);
														$('#info-payment_jigsaw_gallery .info li.three span.jigsaw_details').text(j_size);

			},'text');
		}
		
		if( modify_jigsaw_det == true && $('#info-payment_jigsaw_gallery .info').css('left') != 0 )
		{
					$('#info-payment_jigsaw_gallery .info').animate({left:'0'},1500);
					$('#info-payment_jigsaw_gallery .payment').animate({left:'260px'},1500);
		}

		//change the name of the picture into the paypal form
		$('#paypal_item_descr').attr('value',pic_id + ' - ' + pic_file_name);
		
		$('#image_title-not_edit').attr('value',pic_title);

		$.post('php-script/change_working_image.php',{id_pic: pic_id});
	
	});
	
																										/* IMAGE GALLERY PAGE */
	
														// FORM
	
	$('#advance_without_uploading').click(function(){

		var page = $('body').attr('id');
		
		if( page == 'home' )
		{
			$.post('php-script/change_step.php',{step: "2"},function(){
				location.href = 'image_gallery.php';
			});
			
			//$('#lead_right .two .step1').hide();
			//$('#lead_right .two .step3').hide();
			//$('#lead_right .two .step2').show();
		}
		else
		{
			$('#lead_form .three .step1').hide();
			$('#lead_form .three .step3').hide();
			$('#lead_form .three .step2').show();
			$.post('php-script/change_step.php',{step: "2"});
		}


	});
	
	$('#lead_right .two .homepage_back_to-step-1').click(function(){
		
		$('#lead_right .two .step3').hide();
		$('#lead_right .two .step2').hide();
		$('#lead_right .two .step1').show();
		
		$.post('php-script/change_step.php',{step: "1"});
		
	});
	
	$('#back_to-step-1').click(function(){
		
		$('#lead_form .three .step2').hide();
		$('#lead_form .three .step1').show();
		
		$.post('php-script/change_step.php',{step: "1"});
		
	});
	
	$('#back_to-step-2').click(function(){
	
		$('#lead_form .three .step3').hide();
		$('#lead_form .three .step2').show();
		
		var fn = $('#slide_show img').attr('name'); //get the filename
		
		$('#slide_show a').attr('href','images/galleries/images/large/' + fn );
		$('#slide_show img').attr('src','images/galleries/images/medium/' + fn/* + '?' + rnd() */);
		
		$.post('php-script/change_step.php',{step: "2"});
	
	});

																											/* Jigsaw gallery page */
	$('#side_order-jigsaw_mod .order_button .order_now_button').click(function(){
		$('#info-payment_jigsaw_gallery .info').animate({left:'-260px'},1500);
		$('#info-payment_jigsaw_gallery .payment').animate({left:'0'},1500);
	});
	
	$('#back_to_jigsaw_details').click(function(){
		$('#info-payment_jigsaw_gallery .info').animate({left:'0'},1500);
		$('#info-payment_jigsaw_gallery .payment').animate({left:'260px'},1500);
	});

																											/* F.A.Q. page */

$('body#faq h3.faq a').toggle(function(){
	//console.log('first');
	$(this).parent().next().show();
								},function(){
	//console.log('second');
	$(this).parent().next().hide();
});



																											/* GENERAL */

																						/* FANCY BOX PLUGIN - light box like */
	$('#slide_show a').fancybox(	{
											hideOnContentClick:true,
											overlayShow: true,
											overlayOpacity: 0.3
											});


																						/* image view counter */
	$('#slide_show a').click(function(){

      var str = $(this).attr('href');
      var words = new Array();
      words = str.split('/');
      var fn = words[4];

		$.post('php-script/increase_image-view_counter.php',{filename: fn});

	});

																						/* HOVER PULSE PLUGIN */

	//$('div.thumb img').stop().hoverpulse({
	//$('body#home div.thumb img, body#image_gallery div.thumb img, body#jigsaw_gallery div.thumb img').hoverpulse({
	
	//for some weird reason does not work always properly, so it has been substituied with "HOVER IMAGE POP UP"
	/*$('div.thumb img').hoverpulse({
        size: 11  // number of pixels to pulse element (in each direction)
        //speed: 300 // speed of the animation
    });*/

																						/* HOVER IMAGE POP UP */
$('div.thumb img').stop().hover(function(){
		
		$(this).css('z-index',100);
		
		//console.log(this);
		$(this).animate({	
								top: "-10px",
								left: "-10px",
								width: "140px",
								height: "110px"
								});

								},function(){

		$(this).css('z-index',1);

		$(this).animate({	
								top: "0",
								left: "0",
								width: "120px",
								height: "90px"
								});

});
//$('div.thumb img').unbind("mouseover");

																						/* footer jigsaw link tag */

$('#tag_box .tags a.image_link').click(function(){
	
	var pic_id = this.name;

	$.post('php-script/change_working_image.php',{id_pic: pic_id},function(){
		location.href = 'jigsaw_gallery.php';
	});
});



																												/* SIGN OUT event - function continue down */

	$('#sign_out_link,#sign_out_link_mod').click(function(){
		sign_out()
	});


																												/* SIGN IN - FORM (a sign_in function continue down) */

	$('.sign_in').click(function(){

		login_pop_up();
		
	});

	$('.sign-in_small_mod').click(function(){																//click sign_in module button
		sign_in_mod_anim();
	});
	
	$('.register_small_mod').click(function(){ 															//click register module button
		register_mod_anim();
	});


																													/* change password */
$('#change_password_click').click(function(){

	$.post('php-script/get_user_details.php',function(response){

		if( response == 'false')
		{
			info_message('sign in first, thanks');
			return;
			
		}

		// clear out plugin default styling 
		$.blockUI.defaults.css = {};

		$.blockUI({
						fadeIn: 400,
						message: $('#change_password_mod'),
						showOverlay: true,
						css: {	top:  ($(window).height() - 250) /2 + 'px',
									left: ($(window).width() - 228) /2 + 'px',
									width: '228px',
									height: '250px'
								}
					});
			$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
	});

});


																													/* EDIT MY PROFILE - POP UP */
$('#edit_my_profile_click').click(function(){

	$.post('php-script/get_user_details.php',function(response){

		if( response == 'false')
		{
			info_message('sign in first, thanks');
			return;
			
		}

		$arr = response.split(',');
		
		$('#e_reg_name').val($arr['1']);
		$('#e_reg_surname').val($arr['2']);
		$('#e_reg_email').val($arr['5']);
		$('#e_reg_username').val($arr['3']);
		$('#e_reg_password').val('      ');
		$('#e_street_address').val($arr['7']);
		$('#e_town-city').val($arr['8']);
		$('#e_post_code').val($arr['9']);

		// clear out plugin default styling 
		$.blockUI.defaults.css = {};

		$.blockUI({
						fadeIn: 400,
						message: $('#edit_my_profile_mod'),
						showOverlay: true,
						css: {	top:  ($(window).height() - 510) /2 + 'px',
									left: ($(window).width() - 228) /2 + 'px',
									width: '228px',
									height: '510px'
								}
					});
			$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);

	});



});


																														/* My image gallery link */

$('.my_image_gallery_link').click(function(){

	signed_in(); //set the variable signed...

	if(signed)
	{
		go_to_proper_gallery('image','my_pictures','',0);
	}

});

$('.my_jigsaw_gallery_link').click(function(){

	signed_in(); //set the variable signed...

	if(signed)
	{
		go_to_proper_gallery('jigsaw','my_pictures',1);
	}

});




																														/* BIG IMAGE GALLERY */

																//navigation gallery arrows - starting point

	if( !($('#big_gallery_container .three .menu .second .to').text() < $('#big_gallery_container .three .menu .second .of').text()) )
	{
		//console.log('in');
		$('#big_gallery_container div.three .menu a.big_gallery_next').css('color','#999999');
		$('#big_gallery_container div.three .menu img.next_page').attr('src','images/layout/nextarrow_grey_right.gif');
		$('#big_gallery_container div.three .menu a.big_gallery_last').css('color','#999999');
		$('#big_gallery_container div.three .menu img.last_page').attr('src','images/layout/lastarrow_grey_right.gif');
		
	}


	$('#big_gallery_container div.one a ').click(function(){

		var this_el = this;

		var menu = $(this_el).text();
		
		menu = menu.toLowerCase();
		menu = menu.replace(' ','_');
		
		//console.log(menu);

		$('#big_gallery_container .one li').removeClass('active');
		$(this_el).parent().parent().addClass('active');

		get_big_gallery_info();

		send_big_gallery_info(page,'menu',menu,sub_menu,pic_from,pic_to,pic_of);

	});
	
	$('#big_gallery_container .two span.cat a').click(function(){

		$('#big_gallery_container .two span.cat a').removeClass('active');
		$(this).addClass('active');
		
		get_big_gallery_info();
		
		send_big_gallery_info(page,'sub_menu',menu,sub_menu,pic_from,pic_to,pic_of);
		
	});


	$('.big_gallery_first').click(function(){
		get_big_gallery_info();
		
		if( pic_from == 1 )
		{return false;}
		
		send_big_gallery_info(page,'first',menu,sub_menu,pic_from,pic_to,pic_of);
		
	});

	$('.big_gallery_prev').click(function(){
		get_big_gallery_info();
		
		if( pic_from == 1 )
		{return false;}
		
		//var page = $('body').attr('id');

		send_big_gallery_info(page,'prev',menu,sub_menu,pic_from,pic_to,pic_of);
		
	});
	
	$('.big_gallery_next').click(function(){
		get_big_gallery_info();

		if( pic_to == pic_of )
		{return false;}

		//var page = $('body').attr('id');

		send_big_gallery_info(page,'next',menu,sub_menu,pic_from,pic_to,pic_of);
	});

	$('.big_gallery_last').click(function(){
		get_big_gallery_info();
		
		send_big_gallery_info(page,'last',menu,sub_menu,pic_from,pic_to,pic_of);
		
		if( pic_to == pic_of )
		{return false;}
		
	});

//current page
page = $('body').attr('id');

$('.gallery_thumb .options .public_private .butt_two a').click(function(){ //change picture stat (public/private)

	var el = this;
	var image_numb;
	var picture_state;
	
	image_numb = $(el).parent().parent().parent().attr('name');
	
	if( $(el).text() == 'Public' )	picture_state = 'Private';
	else										picture_state = 'Public';
	
	if( $(el).text() == 'Public' || $(el).text() == 'Private')
	{

		$.post('php-script/public-private_change.php', {image: image_numb},
				function(data){
					if(data == true)
					{
						$(el).text(picture_state);
						picture_state = picture_state.toLowerCase();
						info_message('the picture is now ' + picture_state);
					}
				});
	}
	

});

$('.gallery_thumb .options .delete_pic .butt_two a').click(function(){ //delete picture

	var el = this;
	el = $(el).parent().parent().parent();

	image_to_del.numb = el.attr('name');
	
	el = el.prev().prev();

	image_to_del.id = el.parent().attr('id');
	el = el.find('img');

	image_to_del.src = el.attr('src');
	image_to_del.alt = el.attr('alt');

	$('#del_pic_confirmation img').attr({
													'src':image_to_del.src,
													'alt':image_to_del.alt
													});

	// clear out plugin default styling
	$.blockUI.defaults.css = {};

	$.blockUI({
					fadeIn: 400,
					/*fateOut: 1000,*/
					message: $('#del_pic_confirmation'),
					showOverlay: true,
					css: {	top:  ($(window).height() - 228) /2 + 'px',
								left: ($(window).width() - 200) /2 + 'px',
								/*border: 'none',
								opacity: .95,
								padding: '0',
								margin: '0',*/
								width: '228px',
								height: '200px'//,
								//backgroundColor: 'red'
							}
				});
		$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
		$('#n_d_p').attr('title',"I don't want to delete it").click($.unblockUI);


});
																	/* registration - username availability */
$('#l_reg_username').keyup(function(){

		if( $('#l_reg_username').val().length > 2)
		{
	
			var str = $('#l_reg_username').val();
			$.ajax({
					type: "POST",
					url: "php-script/username_availability.php",
					data: "username=" + str,
					success: function(response){
						if(response == 'OK'){
							$('#desired_login_name_span').text('available...');
							username_available = true;
						} else	{
								$('#desired_login_name_span').text('NOT available...');
								username_available = false;
								}
					}
			}); //$.ajax closure
		}//if closure
		else
		{
			$('#desired_login_name_span').text('');
		}
		
	});

});

function change_password(){

	var values = $('#change_password_mod form').serialize();

	$.post('php-script/change_password.php',values,function(response){
		info_message(response);
	});

	

}


function go_to_proper_gallery(page,menu,submenu,kind_of_request)
{

	var str = 'page=' + page + '&menu=' + menu + '&submenu=' + submenu + '&kind_of_req=' + kind_of_request;

	//console.log('string: ' + str);

	if(page == 'jigsaw')
	{
		php_page = 'jigsaw_gallery.php';
	}
	else
	{
		php_page = 'image_gallery.php';
	}

	$.post('php-script/big_gallery_properties.php',str,function(){
		location.href = php_page;
	});

/*	$.ajax({
		type: "POST",
		url: "php-script/big_gallery_properties.php",
		data: str,
		async: false,	//set to syncronous to wait for the response...
		success: function(response){
			location.href = php_page;
		}
	});*/

}

var signed;

function signed_in(){

	$.ajax({
		type: "POST",
		url: "php-script/signed_in.php",
		async: false,	//set to syncronous to wait for the response...
		success: function(response){

			if(response == 'false')
			{
				info_message('sign in first, thanks');
				signed = false;
			}
			else
			{
				signed = true;
			}

		}
	});

}

function edit_my_details(){

	var f_name = $('#e_reg_name').val();
	f_name = f_name.replace(',',' ');

	var surname = $('#e_reg_surname').val();
	surname = surname.replace(',',' ');

	var street = $('#e_street_address').val();
	street = street.replace(',',' ');

	var town = $('#e_town-city').val();
	town = town.replace(',',' ');

	var post_code = $('#e_post_code').val();
	post_code = post_code.replace(',',' ');

	var arr;
	arr =  f_name + ',';
	arr += surname + ',';
	arr += street + ',';
	arr += town + ',';
	arr += post_code;

	$.post('php-script/edit_user_details.php',{user_details: arr},function(response){
		
		if(response == 'true')
		{
			info_message('details changed...');
		}
		else
		{
			info_message('sorry, try again later');
		}
		
	});

	$.unblockUI();

}


function del_pic(el){

	$.post('php-script/delete_pic.php', 
				{image: el},
				function(data){
					if(data == 1)
					{

						info_message('The image titled ' + image_to_del.alt + ' has been deleted');

						get_big_gallery_info();

						send_big_gallery_info(page,'delete',menu,sub_menu,pic_from,pic_to,pic_of);

					}
					else
					{
						info_message('Deletion failed');
					}
	});

}


var data = new Object; //to make data a global variable

function send_big_gallery_info(page,func,menu,sub_menu,pic_from,pic_to,pic_of){

	var str = "page=" + page + "&gallery_index=" + big_gallery_index + "&func=" + func + "&menu=" + menu + "&sub_menu=" + sub_menu + "&from=" + pic_from + "&to=" + pic_to + "&of=" + pic_of ;

	$.ajax({
		type: "POST",
		url: "php-script/big_gallery_engine.php",
		data: str,
		success: function(response){
				eval(response);

				$('#big_gallery_container .four div').show();

				big_gallery_index = data['gallery_index'];

				if( page == 'image_gallery' )
				{
					var path = 'images/galleries/images/thumbnails/';
					var imgs = 15;
				}
				else
				{
					var path = 'images/galleries/jigsaws/thumbnails/';
					var imgs = 20;
				}

				$('#big_gallery_container .three .menu .second .from').text(data['from']);
				$('#big_gallery_container .three .menu .second .to').text(data['to']);
				$('#big_gallery_container .three .menu .second .of').text(data['of']);


								//color the arrows - active color (green) or inactive (grey)
				//change the two left-arrows
				if( data['from'] != 1 && data['from'] != 0 )
				{
					$('#big_gallery_container div.three .menu a.big_gallery_first').css('color','#66a334');
					$('#big_gallery_container div.three .menu img.first_page').attr('src','images/layout/firstarrow_green_left.gif');
					$('#big_gallery_container div.three .menu a.big_gallery_prev').css('color','#66a334');
					$('#big_gallery_container div.three .menu img.prev_page').attr('src','images/layout/prevarrow_green_left.gif');
				}
				else
				{
					$('#big_gallery_container div.three .menu a.big_gallery_first').css('color','#999999');
					$('#big_gallery_container div.three .menu img.first_page').attr('src','images/layout/firstarrow_grey_left.gif');
					$('#big_gallery_container div.three .menu a.big_gallery_prev').css('color','#999999');
					$('#big_gallery_container div.three .menu img.prev_page').attr('src','images/layout/prevarrow_grey_left.gif');
				}

				//change the two right-arrows
				if( data['to'] == data['of']  )
				{
					$('#big_gallery_container div.three .menu a.big_gallery_next').css('color','#999999');
					$('#big_gallery_container div.three .menu img.next_page').attr('src','images/layout/nextarrow_grey_right.gif');
					$('#big_gallery_container div.three .menu a.big_gallery_last').css('color','#999999');
					$('#big_gallery_container div.three .menu img.last_page').attr('src','images/layout/lastarrow_grey_right.gif');
				}
				else
				{
					$('#big_gallery_container div.three .menu a.big_gallery_next').css('color','#66a334');
					$('#big_gallery_container div.three .menu img.next_page').attr('src','images/layout/nextarrow_green_right.gif');
					$('#big_gallery_container div.three .menu a.big_gallery_last').css('color','#66a334');
					$('#big_gallery_container div.three .menu img.last_page').attr('src','images/layout/lastarrow_green_right.gif');
				}

				var id=1;

				if( data['pictures'] != null )
				{
					$('.no_images_big_gallery').hide();
				}
				else
				{
					$('.no_images_big_gallery').show();
				}

				var cont;

				for ( var i in data['pictures'] )
				{

					$('#id_' + id + ' div img').attr({
																src:	path + data['pictures'][i].filename,
																id:	'id_' + data['pictures'][i].image_id,
																name:	data['pictures'][i].filename,
																alt:	data['pictures'][i].title
																});
					$('#id_' + id + ' div.thumb_descr span').text( data['pictures'][i].title );
					
					if(menu == 'my_pictures') //if the menu is 'my_pictures' add the image_id otherwise empty it
					{
						$('#id_' + id + ' div.options').attr('name',data['pictures'][i].image_id);

						//console.log( data['pictures'][i].publ );

						if( data['pictures'][i].publ == 1 )
						{
							cont = 'Public';
						}
						else
						{
							cont = 'Private';
						}

						$('#id_' + id + ' div.options div.public_private a').text(cont);

					}
					else
					{
						$('#id_' + id + ' div.options').attr('name','');
					}
					
					
					id++;
				}

				for(id; id <= imgs; id++)
				{
					$('#id_' + id).hide();
				}
				
				if( menu == 'my_pictures' )
				{
					$('#big_gallery_container .four .gallery_thumb div.options').show();
				}
				else
				{
					$('#big_gallery_container .four .gallery_thumb div.options').hide();
				}

		},
		error: function(msg){
			alert("SERVER ERROR, sorry about that... please try again later.");
		}
	});

}

function get_big_gallery_info(){

	$('#big_gallery_container .one .big_gallery_text').each(function(i){ //get the menu value and put it in the var 'menu'
		var menu_el = this;

		if( $(menu_el).parent().hasClass('active') )
		{
			menu = $(menu_el).text();
		}
	});

	menu = menu.toLowerCase();
	menu = menu.replace(' ','_');
	//console.log('menu: ' + menu);
	
	$('#big_gallery_container .two .cat a').each(function(){ //get the sub_menu value and put it in the var 'sub_menu'
		var sub_menu_el = this;

		if( $(sub_menu_el).hasClass('active') )
		{
			sub_menu = $(sub_menu_el).attr('name');
		}
	});

	pic_from = $('#big_gallery_container .three .second .from:first').text();
	//console.log(pic_from);
	
	pic_to = $('#big_gallery_container .three .second .to:first').text();
	//console.log(pic_to);
	
	pic_of = $('#big_gallery_container .three .second .of:first').text();
	//console.log(pic_of);
	
	//console.log(47%15);

}

function create_jigsaw(){

	var n_of_pieces = $('#jigsaw_pieces').val();
	
	//$.post('php-script/create_jigsaw.php',{pieces: n_of_pieces});
	str = 'pieces=' + n_of_pieces;
	
	$.ajax({
		type: "POST",
		url: "php-script/create_jigsaw.php",
		data: str,
		success: function(response){

				$('#lead_form .three .step2').hide();
				$('#lead_form .three .step3').show();

				response = response.split(',');
				//console.log(n_of_pieces);
				if( n_of_pieces == 'b')
				{
					$('#paypal_selection .big').attr('selected','selected');
				}
				else if( n_of_pieces == 'm')
				{
					$('#paypal_selection .medium').attr('selected','selected');
				}
				else if( n_of_pieces == 's')
				{
					$('#paypal_selection .small').attr('selected','selected');
				}


				$('#slide_show a').attr('href','images/galleries/jigsaws/large/' + response['1'] );
				$('#slide_show img').attr('src','images/galleries/jigsaws/medium/' + response['1'] + '?' + rnd() );

				$('#paypal_item_descr').attr('value',response['0'] + ' - ' + response['1']);
				
				

		},
		error: function(msg){
					alert("SERVER ERROR!!! " + msg);
					}
	});

}

function sign_in_mod_anim(){												// sign_in mod animation
	if( $('#side_sign-in_mod').css('display') == 'none' )
	{
		$('#side_registration_mod').slideUp(time);
		//$('#side_sign-in_mod').css('margin','15px auto 40px auto').slideDown(time);
		$('#side_sign-in_mod').css('margin','40px auto 40px auto').slideDown(time);
	}
	else
	{
		$('#side_sign-in_mod').slideUp(time);
	}
}


function register_mod_anim(){
	if( $('#side_registration_mod').css('display') == 'none' )
	{
		$('#side_sign-in_mod').slideUp(time);
		//$('#side_registration_mod').css('margin','15px auto 40px auto').slideDown(time);
		$('#side_registration_mod').css('margin','40px auto 40px auto').slideDown(time);
	}
	else
	{
		$('#side_registration_mod').slideUp(time);
	}
}




																												/* INFO BOX */

function info_message(message){

	$('#info_box div.two span').text(message);

	//console.log($('#info_box div.two').width());

	$.blockUI.defaults.css = {};

	//$.blockUI.defaults.css.backgroundColor = ''; //reset the standard background

	$.blockUI({
						/*centerY: 0,*/
						fadeIn: 400,
						fateOut: 1000,
						timeout: 3000,
						showOverlay: false,
						message: $('#info_box'),
						css: { 	top: '30px',
									left: '',
									right: '30px',
									border: '0',
									opacity: .95,
									padding: '0',
                				color: '#FFCC00'
                			}
					});

	//var div_width = $('#info_box div.two').width();

	//$('#info_box div.center').width(div_width - 51);

	$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
	
	//setTimeout($.unblockUI, 20000);

}

																												/* SIGN_IN functions */
function login_pop_up(){

	// clear out plugin default styling 
	$.blockUI.defaults.css = {};

	$.blockUI({
					fadeIn: 400,
					/*fateOut: 1000,*/
					message: $('#side_sign-in_mod'),
					showOverlay: true,
					css: {	top:  ($(window).height() - 228) /2 + 'px',
								left: ($(window).width() - 292) /2 + 'px',
								/*border: 'none',
								opacity: .95,
								padding: '0',
								margin: '0',*/
								width: '228px',
								height: '292px'
							}
				});
		$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);

}


function sign_in(){

	var login_username = $('#username').val();
	var login_password = $('#sign-in_password').val();
	
	var str = "username=" + login_username + "&psw=" + login_password;
	
	$.ajax({
		type: "POST",
		url: "php-script/sign_in.php",
		data: str,
		success: function(response){
			if(response == 'true')
			{
				//info_message('good');
				$('#sign_in_link_container').html("<span class='white_14_b' > Welcome " + login_username + "</span><span class='white_14_b' > - </span><a id='sign_out_link' href='javascript:void(0);' class='white_14_b_link white_14_b' > SIGN OUT </a>");

				$('#side_reg-sign-in-status_mod .two').html("<span class='white_14_b' > Welcome " + login_username + " </span><br /><br /><a id='sign_out_link_mod' href='javascript:void(0);' class='yellow_14_b white_14_b_link' > SIGN OUT </a>");
				
				$('#side_sign-in_mod, body#home #side_registration_mod').slideUp(time);
				
				$('.signed_in').removeClass('hide');
				$('.signed_in').removeClass('visib');

				$('.ul_sign_in-before-paying').addClass('hide');
				$('.ul_payment').removeClass('hide');

				//$('.signed_in').show();

				//unblock the screen
				$.unblockUI({ fadeOut: 200 });

				$('#sign_out_link,#sign_out_link_mod').bind('click', function(){
					sign_out();
				});
			}
			else
			{
				info_message('login details not present in our database, please try again.');
			}
			},
		error: function(msg){
					alert("SERVER ERROR!!! " + msg);
					}
	});
	
}



function sign_out(){
	
	$.ajax({
		type: "POST",
		url: "php-script/sign_out.php",
		success: function(response){
			if(response == 'true')
			{
				info_message('thanks for visiting, come back soon!!!');
				$('#sign_in_link_container').html("<a class='sign_in white_14_b_u' href='#' > Sign in </a> <a id='w_arrow_image' class='sign_in' href='#' > <img src='images/layout/arrow_white_blackbg.gif' alt='white_arrow' /> </a>");

				$('#side_reg-sign-in-status_mod .two').html("<span class='white_14_b' >You're not logged in...</span><br /><br /><div class='first'><div class='float_l' ><a class='sign-in_small_mod yellow_14_b' href='javascript:void(0);' >Sign in </a><img class='arrws' src='images/layout/arrow_yellow_greenbg.gif' alt='yellow arrow' /></div><div class='float_r' ><a class='register_small_mod yellow_14_b' href='javascript:void(0);' >Register </a><img class='arrws' src='images/layout/arrow_yellow_greenbg.gif' alt='yellow arrow' /></div><div class='clear_b' ></div></div>");


				$('.sign-in_small_mod').bind('click',function(){
					sign_in_mod_anim();
				});
				
				$('.register_small_mod').bind('click',function(){
					register_mod_anim();
				});

				$('.sign_in').bind('click', function(){
					login_pop_up();
				});
				
				//$('.public_image').hide();
				//$('.signed_in').hide();
				//$('.signed_in').addClass('hide');
				$('.signed_in').addClass('visib');
				
				$('.ul_sign_in-before-paying').removeClass('hide');
				$('.ul_payment').addClass('hide');
				
			}
					},
		error: function(msg){
					alert("SERVER ERROR!!! " + msg);
					}
	});

}


																										/* REGISTRATION */
function registration(){ //form always in the main page

	var reg_email = $('#reg_email').val();
	var reg_psw = $('#reg_password').val();
	var reg_psw_conf = $('#reg_password_conf').val();
	
	var str = "email=" + reg_email + "&psw=" + reg_psw + "&psw_conf=" + reg_psw_conf;
	
	//console.log(reg_psw);
	//console.log(reg_psw_conf);
	
	$.ajax({
		type: "POST",
		url: "php-script/registration.php",
		data: str,
		success: function(response){
			
			if(response == 'true')
			{
				info_message('email sent at ' + reg_email);
				$('#side_registration_mod').hide();
			}
			else if(response == 'false')
			{
				info_message('This email is already in our database...');
			}
			else //'psw_wrong' is returned if the password is wrong
			{
				info_message('Did the password match?');
			}
		},
		error: function(msg){
					alert("SERVER ERROR!!! " + msg);
					}
	});

}

function registration_form() //pop-up the last step form for the registration
{

	// clear out plugin default styling 
	$.blockUI.defaults.css = {};

	$.blockUI({
					fadeIn: 400,
					/*fateOut: 1000,*/
					message: $('#side_registration-form_mod'),
					showOverlay: true,
					css: {	top:  ($(window).height() - 510) /2 + 'px',
								left: ($(window).width() - 228) /2 + 'px',
								/*border: 'none',
								opacity: .95,
								padding: '0',
								margin: '0',*/
								width: '228px',
								height: '510px'//,
								//backgroundColor: 'red'
							}
				});
		$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
}

function last_step_registration(){

	var nam = $('#l_reg_name').val();
	var surname = $('#l_reg_surname').val();
	var username = $('#l_reg_username').val();
	var psw = $('#l_reg_password').val();
	
	if(nam == '')
	{
		$('#l_reg_name').css('border',"2px solid red");
	}
	else
	{
		$('#l_reg_name').css('border',"");
	}
	
	
	if(surname == '')
	{
		$('#l_reg_surname').css('border',"2px solid red");
	}
	else
	{
		$('#l_reg_surname').css('border',"");
	}
	
	if(username == '' || username.length < 3 || !username_available)
	{
		$('#l_reg_username').css('border',"2px solid red");
	}
	else
	{
		$('#l_reg_username').css('border',"");
	}
	
	if(psw == '')
	{
		$('#l_reg_password').css('border',"2px solid red");
	}
	else
	{
		$('#l_reg_password').css('border',"");
	}
	
	
	if(nam == '' || surname == '' || username == '' || psw == '' || username.length < 3 || !username_available)
	{
		return;
	}
	
	var street = $('#l_street_address').val();
	var city = $('#l_town-city').val();
	var post_code = $('#l_post_code').val();

	var str = "name=" + nam + "&surname=" + surname + "&username=" + username + "&psw=" + psw + "&street=" + street + "&city=" + city + "&post_code=" + post_code;
	
	$.ajax({
		type: "POST",
		url: "php-script/register_user.php",
		data: str,
		success: function(response){

			if(response == 'true')
			{
				
				info_message('registration done, start immediately uploading your pictures!!!');
				
				$('#sign_in_link_container').html("<span class='white_14_b' > Welcome " + username + "</span><span class='white_14_b' > - </span><a id='sign_out_link' href='#' class='white_14_b_link white_14_b' > SIGN OUT </a>");

				//$('.public_image').show();
				$('.signed_id').show();

				$('#sign_out_link,#sign_out_link_mod').bind('click', function(){
					sign_out();
				});
				
				$('#side_registration_mod').hide();
				
			}
			else if( response == 'wrong_psw' )
			{
				info_message('wrong password');
			}
			else if( response == 'username_used' )
			{
				info_message('this username has been already used');
			}
			else
			{
				info_message('Did you enter the right password?');
			}
		},
		error: function(msg){
					alert("SERVER ERROR!!! " + msg);
					}
	});

}

																				//create a random string
function rnd(){return String((new Date()).getTime()).replace(/\D/gi,'') }


function contact_us(){

	$('body#contact_us ul.contact_us li input[type="text"]').each(function(){

		var el = this;
		el = $(el).val();
		
		if(el == "")
		{
			info_message('fill all the fields please...');
			return;
		}
		
	});
	
	if( $('body#contact_us ul.contact_us li textarea').val() == "")
	{
		info_message('fill all the fields please...');
		return;
	}
	
	var form_details = $('body#contact_us #contact_us-form').serialize();

	$.ajax({
				type: "POST",
				url: "php-script/contact_us_form.php",
				data: form_details,
				success: function(response){

					info_message('Thank you for letting us know...');
					$('body#contact_us ul.contact_us li input').val('');
					$('body#contact_us ul.contact_us li textarea').val('');

					},
				error: function(msg){
							alert("SERVER ERROR!!! sorry, try again later... ERR: " + msg);
							}
			});
}

$(function(){

	$('#forgot_my_psw').click(function(){
	
		$.blockUI.defaults.css = {};

		$.blockUI({
						fadeIn: 400,
						message: $('#password_forgotten_mod'),
						//showOverlay: true,
						css: {	top:  ($(window).height() - 200) /2 + 'px',
									left: ($(window).width() - 228) /2 + 'px',
									width: '228px',
									height: '200px'
								}
					});
			$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
	
		/*$.blockUI({ overlayCSS: { backgroundColor: '#00f' } }); 
 
      setTimeout($.unblockUI, 2000);*/
	
	});

});

function password_forgotten(){

	var emaill = $('#email_forgotten').val();

	$.post('php-script/password_forgotten.php',{email: emaill},function(response){
		if(response == 1)
		{
			info_message('an email with your log in details has been just sent.');
		}
		else
		{
			info_message('your email is not in our database...');
		}
	});


}


