$(document).ready(function() {
	
	/* --------------------------------- Initialize Cufon --------------------------------- */
	
	
	Cufon.replace('h1, h1.quote, h2, h3, h4');
	Cufon.replace('.footerTitleSpan, #intro p');
	Cufon.replace('.sf-menu li span');
	Cufon.replace('.nivo-caption p, a.button-header span, .bottom-tab span');
	Cufon('.sf-menu li a', {
		hover: {
			color: '#333'
		}
	});
	
	/* --------------------------------- Initialize Carousel --------------------------------- */

	
	jQuery('#mycarousel').jcarousel({
		auto: 2,
		scroll: 4,
		speed: 1000,
		wrap: 'last',
		initCallback: mycarousel_initCallback
	});

	function mycarousel_initCallback(carousel) {
		// Disable autoscrolling if the user clicks the prev or next button.
		carousel.buttonNext.bind('click', function() {
			carousel.startAuto(0);
		});

		carousel.buttonPrev.bind('click', function() {
			carousel.startAuto(0);
		});

		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	};
	
	/* --------------------------------- Initialize SF menu --------------------------------- */
	
	
	jQuery('ul.sf-menu').superfish({
		speed: 400,
		animation:  {opacity:'show',height:'show'}
	});
		

	/* --------------------------------- Initialize jqTransform --------------------------------- */
	
	
	$('form.jqtransform').jqTransform({imgPath:'../images/jqtransform/'});
	

	/* --------------------------------- Form Value Clearing Script --------------------------------- */
	
	
	var clearNamePrevious = '';
	var clearEmailPrevious = '';
	
	// clear input on focus
	$('#name, #email, #search').focus(function() {
		if($(this).val()==$(this).attr('title')) {
			clearNamePrevious = $(this).val();
			$(this).val('');
		}
	});

	// if field is empty afterward, add text again
	$('#name, #email, #search').blur(function() {
		if($(this).val()=='') {
			$(this).val(clearNamePrevious);
		}
	});
	
	
	/* --------------------------------- Form Value Clearing Script --------------------------------- */
	
	
	$("a[rel^='prettyPhoto']").hover(function()
	{
		$(this).find("img").stop().fadeTo(200, 0.6);
		
	}, function()
	{
		$(this).find("img").stop().fadeTo(200, 1);
	});

	/* --------------------------------- Form Value Clearing Script --------------------------------- */
	
	var current_next = $(".sf-menu li a.current").parent().next().find("a.main");
	if (jQuery.browser.msie) {
		current_next.css("background-position", "0px -37px");
	}
	
	

	
});


function show_demo_video(video,clicked){
	
	if(clicked < 1){
	
		clicked++;

		//alert(video);
	
		$f("demo-video-player", "/templates/lightroom-light/globaljs/flowplayer-3.2.5/flowplayer-3.2.5.swf", {
									
									      
				 clip: {
	
	        url: video,
	
	      // Configure clip to use RTMP as our provider.
	
	      provider: 'rtmp'
	
	          },
	
	    // Streaming plugins are configured under the plugins node
	
	    plugins: {
	
	      // RTMP plugin configuration.
	
	      rtmp: {
	
	        url: 'flowplayer.rtmp-3.2.3.swf',
	
	          // netConnectionUrl defines where the streams are found.
	
	          netConnectionUrl: 'rtmp://fms.cdn.wamersports.com/'+video
	
	              }
	
	              }	
		
			});
			
	}//end if
	
}  

function user_login(uid,fid){
	
	
	
	$('#login-error').hide();
	$('#login-error').html('');
	$('#trial-error').hide();
	$('#trial-error').html('');

	
	var email		=	$('#login-email').val();
	var password	=	$('#login-password').val();
	
	var error		=	false;
	
		//start validation
		if(!echeck(email)){
			
			error	=	true;
			$('#login-error').append('<p>Must provide a valid EMAIL ADDRESS.  Please correct and try again.</p>');
			$('#login-email').addClass('txt-error');
			
		}//end if
		else{
			
			$('#login-email').removeClass('txt-error');
		
		}

		
		if(password.length < 1 || password.length > 16){
				
			error	=	true;
			$('#login-error').append('<p>PASSWORD INVALID.  Please correct and try again.</p>');
			$('#login-password').addClass('txt-error');
				
		}//end if
		else{
				
			$('#login-password').removeClass('txt-error');
			
		}
		
		//end validation
		
		if(error){
			
			$('#login-error').show();
		
		}//end if
		else{
		
			show_loading();
		
			//determine url
			if(uid.length == 128){
				var url		=	"/connectors/ajax-connector.php?Command=UserLogin&UID="+uid+"&FID="+fid;
			}
			else{
				
				var url		=	"/connectors/ajax-connector.php?Command=UserLogin";
			
			}//end else
		
		
			//jquery ajax method
				$.ajax({
															
					type:			"GET",
					url:			url,
					dataType:		"xml",
					data:			{
										EmailAddress:				email,
										Password:					password,
										LoginSource:				1007
									},
																
					cache:			false,
					success:		function(data,status){
						
						//alert(data);										
						var arr = xml2array(data);
					
							try{
														
										if(arr['APIErrorResponse'].length !=0){
																
										//alert();
											var error_msg		=	"<p>"+arr['APIErrorResponse']['APIErrorMessage']+' - ('+arr['APIErrorResponse']['APIErrorNumber']+')'+"</p>";
																	
											$('#login-error').html(error_msg);
											$('#login-error').show();
											
											hide_loading();
																
										}//end if
										else{
											//alert("no error");
										}//end else
															
							}//end try
							catch(e){								
								
								uid					=	arr['UserLoginResponse']['UID'];
								fid					=	arr['UserLoginResponse']['FID'];
								
								var firstname		=	arr['UserLoginResponse']['UserFirstName'];
								var lastname		=	arr['UserLoginResponse']['UserLastName'];
								
								request_trial(uid,fid,firstname,lastname,email);
									
																											
							}//end catch
			
											
					},//end success
					error:		function(xhr,desc,err){
									
						alert("Ajax Error: "+desc);						
						hide_loading();
									
					}//end error
					
				});//end ajax
		
		}//end else
	

}//end function

function create_account(uid,fid){

	$('#signup-error').hide();
	$('#signup-error').html('');
	
	var firstname		=	$('#new-firstname').val();
	var lastname		=	$('#new-lastname').val();
	var email			=	$('#new-email').val();
	var password_1		=	$('#new-password-1').val();
	var password_2		=	$('#new-password-2').val();
	
	var error			=	false;
	
	//start validation
		
		if(firstname.length < 1 || firstname.length > 45){
			
			error	=	true;
			$('#signup-error').append('<p>FIRSTNAME must be 1-45 characters in length.  Please correct and try again.</p>');
			$('#new-firstname').addClass('txt-error');
			
		}//end if
		else{
			
			$('#new-firstname').removeClass('txt-error');
		
		}
		
		if(lastname.length < 1 || lastname.length > 45){
			
			error	=	true;
			$('#signup-error').append('<p>LASTNAME must be 1-45 characters in length.  Please correct and try again.</p>');
			$('#new-lastname').addClass('txt-error');
			
		}//end if
		else{
			
			$('#new-lastname').removeClass('txt-error');
		
		}
		
		if(!echeck(email)){
			
			error	=	true;
			$('#signup-error').append('<p>Must provide a valid EMAIL ADDRESS.  Please correct and try again.</p>');
			$('#new-email').addClass('txt-error');
			
		}//end if
		else{
			
			$('#new-email').removeClass('txt-error');
		
		}
		if(password_1 !== password_2){
			
			error	=	true;
			$('#signup-error').append('<p>PASSWORDS must match, please confirm your password.  Please correct and try again.</p>');
			$('#new-password-1').addClass('txt-error');
			$('#new-password-2').addClass('txt-error');
			
		}//end if
		

		
		
		if(password_1.length < 1 || password_1.length > 16){
				
			error	=	true;
			$('#signup-error').append('<p>PASSWORD must be 1-45 characters in length.  Please correct and try again.</p>');
			$('#new-password-1').addClass('txt-error');
				
		}//end if
		else{
				
			$('#new-password-1').removeClass('txt-error');
			
		}
			
		if(password_2.length < 1 || password_2.length > 16){
				
			error	=	true;
			$('#signup-error').append('<p>CONFIRM PASSWORD must be 1-45 characters in length.  Please correct and try again.</p>');
			$('#new-password-2').addClass('txt-error');
				
		}//end if
		else{
				
			$('#new-password-2').removeClass('txt-error');
			
		}
		
	
	//end validation
	
	if(error){
		
		$('#signup-error').show();

	
	}//end if
	else{
	
		show_loading();
	
		if(uid.length == 128){
			var url		=	"/connectors/ajax-connector.php?Command=CreateUser&UID="+uid+"&FID="+fid;
		}
		else{
			
			var url		=	"/connectors/ajax-connector.php?Command=CreateUser";
		
		}//end else
	

	
		//jquery ajax method
			$.ajax({
														
				type:			"GET",
				url:			url,
				dataType:		"xml",
				data:			{
									FirstName:					firstname,
									LastName:					lastname,
									EmailAddress:				email,
									Password1:					password_1,
									Password2:					password_2,
									SecurityQuestion:			0,
									SecurityAnswer:				"N/A",
									DisplayName:				firstname,
									UserSource:					1007
								},
															
				cache:			false,
				success:		function(data,status){
															
					var arr = xml2array(data);
				
						try{
													
									if(arr['APIErrorResponse'].length !=0){
															
									//alert();
										var error_msg		=	"<p>"+arr['APIErrorResponse']['APIErrorMessage']+' - ('+arr['APIErrorResponse']['APIErrorNumber']+')'+"</p>";
																
										$('#signup-error').html(error_msg);
										$('#signup-error').show();
										
										hide_loading();
															
									}//end if
									else{
										//alert("no error");
									}//end else
														
						}//end try
						catch(e){								
							
							uid		=	arr['CreateUserResponse']['UID'];
							fid		=	arr['CreateUserResponse']['FID'];
							
							request_trial(uid,fid,firstname,lastname,email);
								
																										
						}//end catch

		
										
				},//end success
				error:		function(xhr,desc,err){
								
					alert("Ajax Error: "+desc);						
					hide_loading();
								
				}//end error
				
			});//end ajax

	}//end else

}//end function

function request_trial(uid,fid,firstname,lastname,email){

//jquery ajax method
			$.ajax({
														
				type:			"GET",
				url:			"/connectors/ajax-connector.php?Command=RequestFreeTrial",
				dataType:		"xml",
				data:			{
									UID:						uid,
									FID:						fid,
									AppID:						1
								},
															
				cache:			false,
				success:		function(data,status){
															
					var arr = xml2array(data);
				
						try{
													
									if(arr['APIErrorResponse'].length !=0){
															
									//alert();
										var error_msg		=	"<p>"+arr['APIErrorResponse']['APIErrorMessage']+' - ('+arr['APIErrorResponse']['APIErrorNumber']+')'+"</p>";
																
										$('#trial-error').html(error_msg);
										$('#trial-error').show();
										
										hide_loading();
															
									}//end if
									else{
										//alert("no error");
									}//end else
														
						}//end try
						catch(e){								
							
							var key		=	arr['RequestFreeTrialResponse']['ProductKey'];

							$('#trial-request').hide();
							$('#new-key').html(key);
							$('#trial-success').show();
							scroll(0,0);
							hide_loading();
							
							//join list silently
							join_list(65,firstname,lastname,email)
																											
						}//end catch
		
										
				},//end success
				error:		function(xhr,desc,err){
								
					alert("Ajax Error: "+desc);						
					hide_loading();
								
				}//end error
				
			});//end ajax


}//end function

function confirm_mac_notify(){
	
	$("#mac-notify-dialog").dialog({						
		autoOpen: false,
		height: 500,
		width: 600,
		modal: true,
		buttons: {
			
			Cancel: function() {
			
				$( this ).dialog( "close" );
			},
			"Notify Me": function(){
				mac_notify();
				//$( this ).dialog( "close" );
			}
		}
								
	});
	
	$("#mac-notify-dialog").dialog( "open" );
			
}//end function

function mac_notify(){

	$('#mac-error').hide();
	$('#mac-error').html('');

	var firstname			=	$('#mac-firstname').val();
	var lastname			=	$('#mac-lastname').val();
	var email				=	$('#mac-email').val();

	var	error				=	false;
	
	//start validation
		
		if(firstname.length < 1 || firstname.length > 45){
			
			error	=	true;
			$('#mac-error').append('<p>FIRSTNAME must be 1-45 characters in length.  Please correct and try again.</p>');
			$('#mac-firstname').addClass('txt-error');
			
		}//end if
		else{
			
			$('#mac-firstname').removeClass('txt-error');
		
		}
		
		if(lastname.length < 1 || lastname.length > 45){
			
			error	=	true;
			$('#mac-error').append('<p>LASTNAME must be 1-45 characters in length.  Please correct and try again.</p>');
			$('#mac-lastname').addClass('txt-error');
			
		}//end if
		else{
			
			$('#mac-lastname').removeClass('txt-error');
		
		}
		
		if(!echeck(email)){
			
			error	=	true;
			$('#mac-error').append('<p>Must provide a valid EMAIL ADDRESS.  Please correct and try again.</p>');
			$('#mac-email').addClass('txt-error');
			
		}//end if
		else{
			
			$('#mac-email').removeClass('txt-error');
		
		}

	
	//end validation
	
	if(error){
		
		$('#mac-error').show();

	
	}//end if
	else{
		
		show_loading();
		//jquery ajax method
			$.ajax({
														
				type:			"GET",
				url:			"/connectors/ajax-connector.php?Command=JoinList",
				dataType:		"xml",
				data:			{
									ListID:						66,
									FirstName:					firstname,
									LastName:					lastname,
									EmailAddress:				email
								},
															
				cache:			false,
				success:		function(data,status){

					var arr = xml2array(data);
				
						try{
													
									if(arr['APIErrorResponse'].length !=0){
															
									//alert();
										var error_msg		=	"<p>"+arr['APIErrorResponse']['APIErrorMessage']+' - ('+arr['APIErrorResponse']['APIErrorNumber']+')'+"</p>";
																
										$('#mac-error').html(error_msg);
										$('#mac-error').show();
										
										hide_loading();
															
									}//end if
									else{
										//alert("no error");
									}//end else
														
						}//end try
						catch(e){								
							
							hide_loading();
							$('#mac-success').show();																				
						}//end catch
		
										
				},//end success
				error:		function(xhr,desc,err){
								
					alert("Ajax Error: "+desc);						
					hide_loading();
								
				}//end error
				
			});//end ajax

		
	}//end else


}//end function

function join_list(list,firstname,lastname,email){
	
	//jquery ajax method
			$.ajax({
														
				type:			"GET",
				url:			"/connectors/ajax-connector.php?Command=JoinList",
				dataType:		"html",
				data:			{
									ListID:						list,
									FirstName:					firstname,
									LastName:					lastname,
									EmailAddress:				email
								},
															
				cache:			false,
				success:		function(data,status){

					//alert(data);		
					//handle silently					
				},//end success
				error:		function(xhr,desc,err){
								
					alert("Ajax Error: "+desc);						
					hide_loading();
								
				}//end error
				
			});//end ajax


}//end function




