if(!window.root) { window['root'] = 'http://'+location.host+'/'; }

function load_filtered_designs()
{
//	alert(link);
$.ajax({
url: window.root+'personalise_a_design?filtered',
beforeSend:function(){ $('#filtering').show(); },
error: function(data){ $('#filtering').hide(); alert('Error '); 
								},
success: function(data) {
	$('#filtering').hide();  
	//alert('=='+data+'==');

	$('#scroll_content').html(data);
}
});	
}

function load_filtered_scroller(link)
{
//	alert(link);
$.ajax({
url: link,
beforeSend:function(){ $('#filtering').show(); },
error: function(data){ $('#filtering').hide(); alert('Error '); 
								},
success: function(data) {
	$('#filtering').hide();  
	//alert('=='+data+'==');

	$('#scroll_content').html(data);
}
});	
}

function validate(form, schema , path, show_div)
{
$.ajax({
url: path+'?validate_type='+schema,
type: 'POST',
data: $('#'+form).serialize(),
beforeSend:function(){ $('#'+show_div).show(); },
error: function(data){ $('#'+show_div).hide();  $('#rent').html('Error '); 
								},
success: function(data) {
	$('#'+show_div).hide();  
	//alert('=='+data+'==');
	if (data=='OK')
	{
	$('#'+form).submit();
	}
	else
	{
	alert(data);
		
	}
}
});
}

function proceed_to_paypal(path, show_div)
{
$.ajax({
url: path,
//type: 'POST',
//data: $('#'+form).serialize(),
beforeSend:function(){ $('#'+show_div).show(); },
error: function(data){ $('#'+show_div).hide();  $('#rent').html('Error '); 
								},
success: function(data) {
	$('#'+show_div).hide();  
	var m=data.split('##');
	//alert('=='+data+'==');
	if (m[0]=='OK')
	{
	//alert(m[1]);
	$('#order_number').val(m[1]);
	$('#order_name').val($('#order_name').val()+m[1]);
	//alert($('#paypal_form').serialize());
	$('#paypal_form').submit();
	}
	else
	{
	alert(data);
		
	}
}
});
}

function proceed_to_wp(path, show_div)
{
$.ajax({
url: path,
//type: 'POST',
//data: $('#'+form).serialize(),
beforeSend:function(){ $('#'+show_div).show(); },
error: function(data){ $('#'+show_div).hide();  $('#rent').html('Error '); 
								},
success: function(data) {
	$('#'+show_div).hide();  
	//alert(data);
	//return;
	var m=data.split('##');
	//alert('=='+data+'==');
	if (m[0]=='OK')
	{
	//alert(m[1]);
	$('#wp_cartId').val(m[1]);
	//$('#order_name').val($('#order_name').val()+m[1]);
	//alert($('#paypal_form').serialize());
	$('#wp_form').submit();
	}
	else
	{
	alert(data);
		
	}
}
});
}

function proceed_without_payment(path, show_div, go_to)
{
$.ajax({
url: path,
//type: 'POST',
//data: $('#'+form).serialize(),
beforeSend:function(){ $('#'+show_div).show(); },
error: function(data){ $('#'+show_div).hide();  $('#rent').html('Error '); 
								},
success: function(data) {
	$('#'+show_div).hide();  
	var m=data.split('##');
	//alert('=='+data+'==');
	if (m[0]=='OK')
	{
	//alert(m[1]);
	var order_nr= m[1];
	location =go_to+order_nr;
	}
	else
	{
	alert(data);
		
	}
}
});
}

function go_to_step3(from, form, show_div, obj)
{

$.ajax({
url: from+'?save_shipping_method',
type: 'POST',
data: $('#'+form).serialize(),
beforeSend:function(){ $('#'+show_div).show(); },
error: function(data){ $('#'+show_div).hide();  $('#rent').html('Error ');  
								},
success: function(data) {
	$('#'+show_div).hide();  
	//alert('=='+data+'==');
	if (data=='OK')
	{
	//alert($(obj).attr('href'));
		location=$(obj).attr('href');
	}
	else
	{
	alert(data);	
	}
}
});

}

function save_your_idea_our_designer (form, schema , path, show_div)
{
$.ajax({
url: path+'?validate_type='+schema,
type: 'POST',
data: $('#'+form).serialize(),
beforeSend:function(){ $('#'+show_div).show(); },
error: function(data){ $('#'+show_div).hide();  $('#rent').html('Error '); 
								},
success: function(data) {
	$('#'+show_div).hide();  
	//alert('=='+data+'==');
	if (data=='OK')
	{
/********************************/

		$.ajax({
		url: window.root+'/your_idea_our_designer?save=yes',
		type: 'POST',
		data: $('#upload_form').serialize(),
		error: function(data){		},
		success: function(data) 
			{
			//alert(data);
			if (data.indexOf('http://')!=-1)
			{
			jQuery.facebox({ ajax: data });
			}
			else
			{
			alert(data);				
			location =window.root+'my_solecreator/my_ideas';
			}
		

			}
});
/********************************/
	}
	else
	{
	alert(data);
		
	}
}
});
		
}

function validate_send_next(form, schema , path, show_div, obj)
{
$.ajax({
url: path+'?validate_type='+schema,
type: 'POST',
data: $('#'+form).serialize(),
beforeSend:function(){ $('#'+show_div).show(); },
error: function(data){ $('#'+show_div).hide();  $('#rent').html('Error ');  
								},
success: function(data) {
	$('#'+show_div).hide();  
	//alert('=='+data+'==');
	if (data=='OK')
	{
	//$('#'+form).submit();
	//alert($('#'+form).attr('action'));
/***************************************************/
$.ajax({
url: $('#'+form).attr('action'),
type: 'POST',
data: $('#'+form).serialize(),
beforeSend:function(){ $('#'+show_div).show(); },
error: function(data){ $('#'+show_div).hide();  $('#rent').html('Error ');  
								},
success: function(data) {
	$('#'+show_div).hide();  
	//alert('=='+data+'==');
	if (data=='OK')
	{
	//alert($(obj).attr('href'));
		location=$(obj).attr('href');
	}
	else
	{
	alert(data);	
	}
}
});
/***************************************************/
	
	}
	else
	{
	alert(data);	
	}
}
});
}


function show_mailing_list_signup()
{
	jQuery.facebox({ ajax: window.root+'mailing_list_signup' });
}

function add_to_cart(idd, ttt)
{
//alert(window.root+'/add_to_cart');
	//alert($('#choose_dsign_form').serialize());
			$.ajax({
		url: window.root+'/add_to_cart',
		type: 'POST',
		data: 'unit_type='+ttt+'&select_design[]='+idd,
		error: function(data){		},
		success: function(data) 
			{
			alert(data);
			refresh_cart();
			}
});
}

function refresh_cart()
{
	$.ajax({
		url: window.root+'/cart_designs_count',
		error: function(data){		},
		success: function(data) 
			{
			//alert(data);
			refresh_cart_html(data);
			}
});
}

function refresh_cart_html(nr)
{
	$('#cart_count').html(nr);
	$('#cart_span').show();
}

function load_filtered_items(section, scroll_to, pos)
{
	//alert(section+'--'+scroll_to+'--'+pos);
	$.ajax({
url: window.root+section+'?filtered&scroll_to='+scroll_to+'&pos='+pos,
//beforeSend:function(){ $('#filtering').show(); },
error: function(data){ alert('Error '); 
								},
success: function(data) {
	//$('#filtering').hide();  
	//alert('=='+data+'==');

	$('#'+scroll_to).html(data).fadeIn();
}
});	

}

$(document).ready(
	function () {


	
$('#loading').ajaxStart(function () {$(this).show()}).ajaxStop(function () {$(this).hide()});


$('.but_upload').each(function(){
	        new AjaxUpload(this, {
	            action: window.root+'your_idea_our_designer',
	            name: 'image',
	           /* data : {
	                'key1' : "This data won't be sent, we will overwrite it."
	            },
				*/
				responseType: 'text',
	            onSubmit : function(file , ext){
	                // Allow only images. You should add security check on the server-side.
	                if (ext && /^(jpg|jpeg|JPG|JPEG)$/.test(ext)){

	                  /*  this.setData({
	                        'key1': '2 keys should be received',
							'key2': '...'
	                    });
					*/	
                        // If you want to allow uploading only 1 file at time,
                        // you can disable upload button
                        this.disable();		
						$('#loading').show();				
	                } else {
                    
	                    return false;               
	                }       
	            },
	            onComplete : function(file, response){
	                // enable upload button
	                this.enable();
					$('#loading').hide();
				if (response !='ok')
				{
				alert(response);					
				}
				else
				{
				$('#uploaded_container').append('<tr><td>'+file+'</td><td><img src="'+window.root+'images/delete.png" border="0" style="cursor:pointer;" onclick="delete_uploaded_image(\''+file+'\', this)" /></td><tr>');	
				}
				//alert(file);	
				//	console.dir(response['files']);
				//	console.dir(response['post']);
	            }       
	        }); 
        });

/*##################*/



jQuery(function( $ ){

 $('#screen').serialScroll({
 target:'#sections',
 items:'.screen_li', // Selector to the items ( relative to the matched elements, '#sections' in this case )
 //prev:'img.prev',// Selector to the 'prev' button (absolute!, meaning it's relative to the document)
 //next:'span.next',// Selector to the 'next' button (absolute too)
 axis:'xy',// The default is 'y' scroll on both ways
 //navigation:'#navigation li a',
 duration:700,// Length of the animation (if you scroll 2 axes and use queue, then each axis take half this time)
force:true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)

 //queue:false,// We scroll on both axes, scroll both at the same time.
 //event:'click',// On which event to react (click is the default, you probably won't need to specify it)
 stop:false,// Each click will stop any previous animations of the target. (false by default)
 lock:true, // Ignore events if already animating (true by default)
 start: 0, // On which element (index) to begin ( 0 is the default, redundant in this case )
 cycle:false,// Cycle endlessly ( constant velocity, true is the default )
 //step:1, // How many items to scroll each time ( 1 is the default, no need to specify )
 //jump:false, // If true, items become clickable (or w/e 'event' is, and when activated, the pane scrolls to them)
 //lazy:false,// (default) if true, the plugin looks for the items on each event(allows AJAX or JS content, or reordering)
 //interval:1000, // It's the number of milliseconds to automatically go to the next
 //constant:true, // constant speed

 onBefore:function( e, elem, $pane, $items, pos ){
/**
50 * 'this' is the triggered element
51 * e is the event object
52 * elem is the element we'll be scrolling to
53 * $pane is the element being scrolled
54 * $items is the items collection at this moment
55 * pos is the position of elem in the collection
56 * if it returns false, the event will be ignored
 */
 //those arguments with a $ are jqueryfied, elem isn't.
 //alert('aa'+pos);
 e.preventDefault();
 if( e.stopPropagation ) { e.stopPropagation(); } //For 'Good' browsers
		else { e.cancelBubble = true; } 
 if( this.blur )
 this.blur();
// if (($('#sections').attr('screen')=='personalize_a_design') && (pos==4))
 if (pos!=0 && !($('#sections').attr('screen')=='start_from_scratch' && pos==4) && !($('#sections').attr('screen')=='personalise_a_design' && pos==5) && !($('#sections').attr('screen')=='your_idea_our_designer' && pos==4) )
{
//alert($(elem).attr('id')+' '+pos);
	//alert($('#sections').attr('screen'));
load_filtered_items($('#sections').attr('screen'), $(elem).attr('id'), pos );	
}
 },
 onAfter:function( elem ){
 //'this' is the element being scrolled ($pane) not jqueryfied
}
 }); 

$('#up_screen').serialScroll({
 target:'#up_sections',
 items:'.upload_li',
 stop:true,
 lock:false, 
 //jump:true,
 //axis:'xy',// The default is 'y' scroll on both ways
 //navigation:'#na a',
 duration:400,
 cycle:false,
 //lazy:true,
 force:true, 
 onBefore:function( e, elem, $pane, $items, pos ){
 //alert('bb'+pos);
 e.preventDefault();
 if( e.stopPropagation ) { e.stopPropagation(); } //For 'Good' browsers
		else { e.cancelBubble = true; } 
 if( this.blur )
 this.blur();
 },
 onAfter:function( elem ){
 
// alert('aa')
}
 }); 
 
 });
/*#################*/

/*##################*/

jQuery(function( $ ){

 
 
 });
/*#################*/

});

$(document).scroll(function(){
//alert($().scrollTop());
$('#loading').css('top', $().scrollTop());
});
/*
function save_your_idea_our_designer ()
{
		$.ajax({
		url: window.root+'/your_idea_our_designer?save=yes',
		type: 'POST',
		data: $('#upload_form').serialize(),
		error: function(data){		},
		success: function(data) 
			{
			alert(data);
			if (data.indexOf('http://')!=-1)
			{
			jQuery.facebox({ ajax: data });
			}
			else
			{
			alert(data);				
			//location =window.root+'my_solecreator';
			}
		

			}
});		
}
*/
function delete_uploaded_image(image, obj)
{
	//alert(image);
	//alert($(obj).attr('src'));
		$.ajax({
		url: window.root+'/your_idea_our_designer',
		type: 'POST',
		data: 'delete_image=&image='+image,
		error: function(data){		},
		success: function(data) 
			{
		if(data !='ok')
		{
			alert(data);
		}
		else
		{
			$(obj).parent().parent().remove();			
		}

			}
});	
}



function start_from_scratch(param, val)
{
//alert(window.root+'/start_from_scratch/'+param+'/'+val+'/');
if(!window.start_from_scratch_current_step) { window['start_from_scratch_current_step'] = 1; }
		$.ajax({
		url: window.root+'/start_from_scratch',
		type: 'POST',
		data: 'param='+param+'&value='+val,
		error: function(data){		},
		success: function(data) 
			{
		//	alert(data);
				start_from_scratch_next_step();

			}
});
//start_from_scratch_next_step();
}

function start_from_scratch_next_step()
{
	window.start_from_scratch_current_step++;
	var s=window.start_from_scratch_current_step;
	if (s==2)
	{
	$('#navigation').css('visibility','visible');	
	}
	$('#stage_'+s+'_icon').removeClass('stage_'+s+'').addClass('stage_'+s+'a');
	$('#stage_'+s+'_text').removeClass('stage_txt').addClass('stage_txt_a');
	$('#stage_'+s+'_arrow').removeClass('stage_arrow').addClass('stage_arrow_a');
	$('#sections').trigger( 'next' );
}

function start_from_scratch_show_app(obj)
{
//				jQuery.facebox({ ajax: data });
	$type=$(obj).attr('action_type');
	//alert($type);
	if ($type=='facebox')
	{
	$action=$(obj).attr('action');
	//alert($action);
	jQuery.facebox({ ajax: $action });	
	};
	
	if ($type=='normal')
	{
		$.ajax({
		url: window.root+'/start_from_scratch',
		type: 'POST',
		data: 'show=app',
		error: function(data){		},
		success: function(data) 
			{
			//alert(data);
			$('#navigation').css('visibility','hidden');	
			//	start_from_scratch_next_step();
			$('#screen').html(data);
			//alert('aa');
			}
			});
		
	}			

}

function start_from_scratch_previous_step()
{
	var s=window.start_from_scratch_current_step;
	if (s==2)
	{
	$('#navigation').css('visibility','hidden');	
	}
	
	$('#stage_'+s+'_icon').addClass('stage_'+s+'').removeClass('stage_'+s+'a');
	$('#stage_'+s+'_text').addClass('stage_txt').removeClass('stage_txt_a');
	$('#stage_'+s+'_arrow').addClass('stage_arrow').removeClass('stage_arrow_a');
	$('#sections').trigger( 'prev' );
	window.start_from_scratch_current_step--;
	
}
/*######################*/
function personalise_a_design(param, val)
{
//alert(window.root+'/start_from_scratch/'+param+'/'+val+'/');
if(!window.personalise_a_design_current_step) { window['personalise_a_design_current_step'] = 1; }
		$.ajax({
		url: window.root+'/personalise_a_design',
		type: 'POST',
		data: 'param='+param+'&value='+val,
		error: function(data){		},
		success: function(data) 
			{
		//	alert(data);
				personalise_a_design_next_step();

			}
});
//start_from_scratch_next_step();
}

function personalise_a_design_next_step()
{
	window.personalise_a_design_current_step++;
	var s=window.personalise_a_design_current_step;
	if (s==2)
	{
	$('#navigation').css('visibility','visible');	
	}
	//alert('stage_'+s+'1===='+'stage_'+s+'1a');
	$('#stage_'+s+'_icon').removeClass('stage_'+s+'1').addClass('stage_'+s+'1a');
	$('#stage_'+s+'_text').removeClass('stage_txt').addClass('stage_txt_a');
	$('#stage_'+s+'_arrow').removeClass('stage_arrow').addClass('stage_arrow_a');
	$('#sections').trigger( 'next' );
}

function personalise_a_design_previous_step()
{
	var s=window.personalise_a_design_current_step;
	if (s==2)
	{
	$('#navigation').css('visibility','hidden');	
	}
	
	$('#stage_'+s+'_icon').addClass('stage_'+s+'1').removeClass('stage_'+s+'1a');
	$('#stage_'+s+'_text').addClass('stage_txt').removeClass('stage_txt_a');
	$('#stage_'+s+'_arrow').addClass('stage_arrow').removeClass('stage_arrow_a');
	$('#sections').trigger( 'prev' );
	window.personalise_a_design_current_step--;
	
}

function personalise_a_design_show_app(obj)
{
//				jQuery.facebox({ ajax: data });
	$type=$(obj).attr('action_type');
	//alert($type);
	if ($type=='facebox')
	{
	$action=$(obj).attr('action');
	//alert($action);
	jQuery.facebox({ ajax: $action });	
	};
	
	if ($type=='normal')
	{
		$.ajax({
		url: window.root+'/personalise_a_design',
		type: 'POST',
		data: 'show=app',
		error: function(data){		},
		success: function(data) 
			{
			//alert(data);
			$('#navigation').css('visibility','hidden');	
			//	start_from_scratch_next_step();
			$('#screen').html(data);
			//alert('aa');
			}
			});
		
	}			

}


/*#######################*/

/*######################*/
function your_idea_our_designer(param, val)
{
//alert(window.root+'/start_from_scratch/'+param+'/'+val+'/');
if(!window.your_idea_our_designer_current_step) { window['your_idea_our_designer_current_step'] = 1; }
		$.ajax({
		url: window.root+'/your_idea_our_designer',
		type: 'POST',
		data: 'param='+param+'&value='+val,
		error: function(data){		},
		success: function(data) 
			{
		//	alert(data);
				your_idea_our_designer_next_step();

			}
});
//start_from_scratch_next_step();
}

function your_idea_our_designer_stay(param, val, obj, cl)
{
if(!window.your_idea_our_designer_current_step) { window['your_idea_our_designer_current_step'] = 1; }
		$.ajax({
		url: window.root+'/your_idea_our_designer',
		type: 'POST',
		data: 'param='+encodeURIComponent(param)+'&value='+encodeURIComponent(val),
		error: function(data){		},
		success: function(data) 
			{
//			alert(data);
			$("."+cl).css("border", "1px none");
			$(obj).css("border", "1px solid red");
			
				//your_idea_our_designer_next_step();

			}
});
//start_from_scratch_next_step();
}


function your_idea_our_designer_next_step()
{
	window.your_idea_our_designer_current_step++;
	if (window.your_idea_our_designer_current_step==5)
	{
	//alert(window.your_idea_our_designer_current_step)
	window.your_idea_our_designer_current_step++;	
	//alert(window.your_idea_our_designer_current_step)
	}
	var s=window.your_idea_our_designer_current_step;

	if (s==2)
	{
		//alert(s);
	$('#navigation').css('visibility','visible');	
	//alert(	$('#navigation').css('visibility'))
	}
	//alert('stage_'+s+'1===='+'stage_'+s+'1a');
	$('#stage_'+s+'_icon').removeClass('stage_'+s+'').addClass('stage_'+s+'a');
	$('#stage_'+s+'_text').removeClass('stage_txt').addClass('stage_txt_a');
	$('#stage_'+s+'_arrow').removeClass('stage_arrow').addClass('stage_arrow_a');
	$('#sections').trigger( 'next' );
}

function your_idea_our_designer_previous_step()
{
	var s=window.your_idea_our_designer_current_step;
	//alert('s='+s);
	if (s==2)
	{
	$('#navigation').css('visibility','hidden');	
	}
	
	$('#stage_'+s+'_icon').addClass('stage_'+s+'').removeClass('stage_'+s+'a');
	$('#stage_'+s+'_text').addClass('stage_txt').removeClass('stage_txt_a');
	$('#stage_'+s+'_arrow').addClass('stage_arrow').removeClass('stage_arrow_a');
	
	window.your_idea_our_designer_current_step--;
	if (window.your_idea_our_designer_current_step==5)
	{
	window.your_idea_our_designer_current_step--;
	$('#sections').trigger( 'goto', [window.your_idea_our_designer_current_step-1] );
	}
	else
	{
	$('#sections').trigger( 'goto', [s-2] );
	}

}
/*#######################*/

function trigger_up(step)
{
	$('#up_sections').trigger( 'goto', step );
}
/*function personalise_a_design(val)
{
		$.ajax({
		url: window.root+'/personalise_a_design/step5',
		type: 'POST',
		data: 'style='+val,
		error: function(data){		},
		success: function(data) 
			{
			//alert(data);
				$('#steps_containter').html(data);

				$('#stage_2_icon').removeClass('stage_2').addClass('stage_2a');
				$('#stage_2_text').removeClass('stage_txt').addClass('stage_txt_a');
				$('#stage_2_arrow').removeClass('stage_arrow').addClass('stage_arrow_a');


			}
});
}*/

