// JavaScript Document
jQuery(document).ready(function () {
	// current control select
	var href = jQuery(location).attr('href');
	
	href = href.split('/');
	href = href[href.length-1];
	
	// set class for link
	$("*").find("a[href="+ href +"]").each(function(){		
		$(this).addClass("current");
	})
	
	//VSP
	href = href.split('?');
	href = href[0];
	
	// set class for list item selected
	$("div.membermenu").find('a[href^='+ href +']').each(function(){		
		$(this).parent('li').addClass("current");
	})
	
	// fix activity tab submenus
	if(href=='listviewswinks.php'){
		$("div.membermenu").find('a[href^=buddybanlist.php]').each(function(){		
			$(this).parent('li').addClass("current");
		})
	}
	
	// fix my profile tab submenus
	if(href=='edituser.php'
	|| href=='uploadsnaps.php'
	|| href=='cancel.php'){
		$("div.membermenu").find('a[href^=showprofile.php]').each(function(){		
			$(this).parent('li').addClass("current");
		})
	}
	
	$('#register-step-1 .step-forward').click(function() {
		var count = 0;
		$('#register-step-1 :input').each(function() {
			if($(this).val() == ''){
				$(this).parent('div.field').addClass('required');
				count++;
			}else {
				$(this).parent('div.field').removeClass('required');
			}
		});
		
		if($('.password :input').val() != $('._password :input').val() ){
				count++;
				$('.password :input, ._password :input').parent('div.field').addClass('required');
		}
		
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		
		
		if( !pattern.test( $('.email :input').val() ) ){
			count++;
			$('.email :input').parent('div.field').addClass('required');
		}
		
		if(count > 0) {
			return false;
		}
		count = 0; // reset
		
		$('#frmSignup div.step').hide().filter(this.hash).show();
		
		/*$('#quicksingup a.step-forward').attr('href', 'signup.php#register-step-2');
		$('#quicksingup a.step-forward').attr('rel', 'popup-purple');*/
		
		return false;		
	});
	
	// focus on user name for login
	$("form#quickLogin input:first").focus(); 
	

	
	// MEMBER PROFILE PHOTOS 
	$("#member-photos a").fancybox({
		type: 'image'
	});
	
	
	// FANCY BOX BLUE SCREEN
	$("a[rel=popup]").fancybox({
		type: 'iframe',
		titleShow: false,
		overlayColor: '#000',
		overlayOpacity: 0.8,
		showNavArrows: false,
		width:385,
		height:388,
		scrolling: 'no',
		showCloseButton: true,
		
		onStart: function() {
			$('body').addClass('fancybox-blue');
		},
		onClosed: function (){
			$('body').removeClass('fancybox-blue');
		}
	});	
	
	// FANCY BOX PURPLE SCREEN
	$("a[rel=popup-purple]").fancybox({
		type: 'iframe',
		titleShow: false,
		overlayColor: '#000',
		overlayOpacity: 0.8,
		showNavArrows: false,
		width: 385,
		height: 388,
		scrolling: 'no',
		showCloseButton: true,
		
		onStart: function(event) {
			var toHref = jQuery(this).attr('href');
			toHref = toHref.split('/');
			toHref = toHref[toHref.length-1];
			toHref = toHref.split('?');
			toHref = toHref[0];
			if(toHref == 'signup.php#register-step-2'){
				// validation
				var count = 0;
				$('#register-step-1 :input').each(function() {
					if($(this).val() == ''){
						$(this).parent('div.field').addClass('required');
						count++;
					}else {
						$(this).parent('div.field').removeClass('required');
					}
				});
				
				if($('.password :input').val() != $('._password :input').val() ){
						count++;
						$('.password :input, ._password :input').parent('div.field').addClass('required');
				}
				
				var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
				
				
				if( !pattern.test( $('.email :input').val() ) ){
					count++;
					$('.email :input').parent('div.field').addClass('required');
				}
				
				if(count > 0) {
					return false;
				}
				count = 0; // reset
			}
			
			$('html body').addClass('fancybox-purple');	
		},
		
		onComplete: function(){
			$( function() {
				$('iframe').load( function(){
					var frame = this.contentDocument;
					//$(this.contentDocument).find('body').html('This frame was modified with jQuery! Yay!!!');
					$('#quicksingup :input').each(function(){
						$(frame).find('input[name='+ $(this).attr('name') +']').val( $(this).attr('value') );
					});
					
					
				});
			});
		},
		
		onClosed: function (){
			$('html body').removeClass('fancybox-purple');
		}
	});	
	
	$('#quicksingup .step-forward').click( function() {
		$('iframe').load( function(){
			var frame;
			if(this.contentDocument){
				frame = this.contentDocument;
			} else if(this.contentWindow){
				frame = this.contentWindow.document;
			}
				$(frame).find('#frmSignup .step').hide();
				var err = $(frame).find('div.display_error.errors ');
				var errid = $(err).attr('id');
				if(errid != null){
					if(errid == '101' || errid=='25' || errid=='22' || errid == '6' || errid == '21'){
						$(frame).find('#register-step-1').show();
					}
					/*if(errid == '38' || errid == '4' || errid == '5'){
					}*/
					else{
						$(frame).find('#register-step-2').show();
					}
				}else{
					$(frame).find('#register-step-2').show();
				}
		});
	});
	
	$('#FormRate select[name=txtrating]').attr('value', 3);
	$('#FormRating').submit();
	
	//RATE CONTROL 
	$('ul.submitrate li').click(function (){
		//
		$(this).addClass('current');
		var rate_id = this.id.replace('rate-', '');
		//console.log(rate_id);
		$('#FormRating select[name=txtrating]').attr('value', rate_id);
		
		$.post($('#FormRating').attr('action'), $("#FormRating").serialize(),
		function(data){
		// alert("Now you can write comment");
		location.reload();
		});		
	});
	
	// TABS CONTROL
	var tabContainers = $('div.tabs div.tabcontent > div');
	tabContainers.hide().filter(':first').show();
	
	$('div.tabs .tabnavigation a').click(function (){
		tabContainers.hide().filter(this.hash).show();
		$('div.tabs .tabnavigation li').removeClass('current');
		$(this).parent('li').addClass('current');
		
		return false;
	});
	
	
	// EDIT FORM VALIDATION
	$("#frmEditUser").validate({
		rules: {
			txtfirstname: "required",
			txtlastname: "required",
			username: {
				required: true,
				minlength: 2
			},
			txtemail: {
				required: true,
				email: true
			},
			txtstateprovince: "required",
			txtcounty:"required",
			txtcity:"required",
			txtzip:"required",
			txtcounty:"required"
		}
		
	});
	
});


