$(document).ready(function(){
	
	// Lavalamp plugin for navigation
	$(function() {
		$("#navigation ul").lavaLamp({speed: 700 })
	});
	
	$("#jump").change(function() {
        var val = $(this).val();
        if (val != '') {
            location.href=val;
        }
    });
	
	function overlay(){
		
		 $t = $("#fileBox"); // CHANGE it to the table's id you have

		$(".overlay").css({
		  opacity : 0.2,
		  top     : 55,
		  left    : 0,
		  width   : $t.outerWidth(),
		  height  : $t.outerHeight()
		});	
		
		$(".overlay").animate({opacity: 'toggle'}); 
		
	}
	
	$('#clicky').click(function(){
							test();	
								  });
	
	$('input[type=file]').change(function(){
		var file_name = $('.file').val();	
		$('.file_load').text(file_name);
	});
	
	$('#imageUpload').click(function(){
		$('#file_upload').uploadifyUpload();
	});
	
	$('#file_upload').uploadify({
    'uploader'  : '/images/uploadify.swf',
    'script'    : 'http://www.purplecoo.com/dashboard/photos/upload.php',
    'cancelImg' : '/images/cancel.png',
	'buttonImg'   : '/images/btn-choose-file.gif',
	'height' : '30',
	'width' : '150',
	'queueSizeLimit' : 1,
	'scriptData': {'username' : $('#username').val()},
  	'fileExt' : '*.jpg;*.gif;*.png',
  	'fileDesc'    : 'Image files',
	'queueID' : 'queueImage',
	'onOpen': function(event,ID,fileObj) {
		overlay();
    },
  	'onComplete'  : 
	 function(event,ID,fileObj, response) {
		
		$("#title").attr("disabled", "disabled");
		$("#about").attr("disabled", "disabled");
		$("#location").attr("disabled", "disabled");
		var title = $('#title').val();
	  	var about = $('#about').val();
	  	var location = $('#location').val();
	  	var filename = response;
	  	$.ajax({
			type: "POST",
			url: "http://www.purplecoo.com/dashboard/photos/photo.php",
			dataType: 'json',
			data: "title="+title+"&about="+about+"&location="+location+"&filename="+filename,
			error: function(data){
				// Handle error
				alert('error');
				overlay();
			},
			success: function(data){
				window.location.reload();
				// Handle success
				$("#title").removeAttr("disabled");
				$("#about").removeAttr("disabled");
				$("#location").removeAttr("disabled");
				overlay();
			}
		}, "json");
    },
	'removeCompleted' : false,
    'auto' : false,
	'multi': false
  });
	
	$('#avatar_upload').uploadify({
    'uploader'  : '/images/uploadify.swf',
    'script'    : 'http://www.purplecoo.com/dashboard/account/avatar-upload.php',
    'cancelImg' : '/images/cancel.png',
	'buttonImg'   : '/images/btn-choose-avatar.gif',
	'height' : '30',
	'width' : '150',
	'queueSizeLimit' : 1,
	'scriptData': {'username' : $('#username').val()},
  	'fileExt' : '*.jpg;*.jpeg;',
  	'fileDesc'    : '.jpg',
	'queueID' : 'queueAvatar',
  	'onComplete'  : 
	 function(event,ID,fileObj) {
	  	var filetype = '.jpg';
	  	$.ajax({
			type: "POST",
			url: "http://www.purplecoo.com/dashboard/account/avatar.php",
			dataType: 'json',
			data: "filetype="+filetype,
			error: function(data){
				// Handle error
				alert('error');
			},
			success: function(data){
				// Handle success
				var username = $('#username').val();
				var location = username+'.jpg';
     			var timestamp = new Date().getTime();
				$('#useravatar').attr('src', '/images/avatars/'+location+'?'+timestamp);
			}
		}, "json");
    },
	'removeCompleted' : false,
    'auto' : true,
	'multi': false
  });
	
	$('#business_upload').uploadify({
    'uploader'  : '/images/uploadify.swf',
    'script'    : 'http://www.purplecoo.com/dashboard/business/business-upload.php',
    'cancelImg' : '/images/cancel.png',
	'buttonImg'   : '/images/btn-choose-business.gif',
	'height' : '30',
	'width' : '150',
	'queueSizeLimit' : 1,
    'auto' : true,
	'scriptData': {'username' : $('#username').val()},
  	'fileExt' : '*.jpg;',
  	'fileDesc'    : '.jpg',
	'queueID' : 'queueBusiness',
  	'onComplete'  : 
	 function(event,ID,fileObj) {
	  	var filetype = '.jpg';
	  	$.ajax({
			type: "POST",
			url: "http://www.purplecoo.com/dashboard/business/business.php",
			dataType: 'json',
			data: "filetype="+filetype,
			error: function(data){
				// Handle error
				alert('error');
			},
			success: function(data){
				// Handle success
				var username = $('#username').val();
				var location = username+filetype;
     			var timestamp = new Date().getTime();
				$('#businessLogo').attr('src', '/images/business/'+location+'?'+timestamp);
			}
		}, "json");
    },
	'removeCompleted' : false,
	'multi': false
  });
	
	$('#flower_upload').uploadify({
    'uploader'  : '/images/uploadify.swf',
    'script'    : 'http://www.purplecoo.com/dashboard/admin/upload.php',
    'cancelImg' : '/images/cancel.png',
	'buttonImg'   : '/images/btn-choose-flower.gif',
	'height' : '30',
	'width' : '150',
	'queueSizeLimit' : 1,
    'auto' : true,
  	'fileExt' : '*.jpg;',
  	'fileDesc'    : '.jpg',
	'queueID' : 'queueFlower',
	'removeCompleted' : false,
	'multi': false
  });
	
	// Sliding Image Menu
	lastBlock = $("#article_1");
	maxWidth = 730;
	minWidth = 105;	
		
	// Slide Image on click
	$(".artbox").click(
		function(){
			$(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:600, easing:"jswing"})
			$(this).animate({width: maxWidth+"px"}, { queue:false, duration:600, easing:"jswing"})
			lastBlock = this;
		}
	);
	
	// TipTip plugin for tooltips
	$(function(){
		$(".tip").tipTip();
	});
	
	// Check all boxes
	$("#checkall").click(
						 
		function(){
			
			var checked_status = this.checked;
			
			$(".blogs-table input[type='checkbox']:not([disabled='disabled'])").each(function(){
				this.checked = checked_status;
			});
		}
		
	);
	
	$('.flag').hide();
	$('.delete').hide();
	$('.comment').hover(function(){
		$(this).find('.flag').toggle();
		$(this).find('.delete').toggle();							 
	});
	
	$('.flagPost').click(function(){
		var href = $(this).attr('href');
		$.colorbox({width:"500px", height:"200px", iframe:true, href:"http://www.purplecoo.com/includes/"+href, scrolling: false});
		return false;
	});
	
	$('.followUser').click(function(){
		var href = $(this).attr('href');
		$.colorbox({width:"500px", height:"200px", iframe:true, href:"http://www.purplecoo.com/includes/"+href, scrolling: false});
		return false;
	});
	
	$('.flagBlog').click(function(){
		var href = $(this).attr('href');
		$.colorbox({width:"500px", height:"200px", iframe:true, href:"http://www.purplecoo.com/includes/"+href, scrolling: false});
		return false;
	});
	
	$('.deletePost').click(function(){
		var href = $(this).attr('href');
		$.colorbox({width:"500px", height:"200px", iframe:true, href:"http://www.purplecoo.com/includes/"+href, scrolling: false});
		return false;
	});
	
	
	$('.actions').hide();
	$('.photoAdmin').hover(function(){
		$(this).find('.actions').toggle();							 
	});
	
		
	
	// Create rich text editor
	$('.textarea-comment').wysiwyg({       
   			css: '/styles/editor.css',
			controls: {
            h1: { visible: false },
			h2: { visible : false },
			h3: { visible: false },
			indent: { visible : false },
			outdent: { visible : false },
			insertHorizontalRule: { visible : false },
			subscript: { visible : false },
			superscript: { visible : false },
			insertTable: { visible : false },
			insertImage: { visible : false },
			alertSep: { separator: true },
        	smiles: {
            	visible: true,
            	exec: function() { $('.smilies').toggle(); },
            	className: 'smiles'
       		}
        },
		events: {
			paste: function() {                
				var iframe_textarea = $(this).find('body');
				var prepaste_content = iframe_textarea.html();
				setTimeout(function() {
					if (iframe_textarea.html().match(/<style>|MsoNormal|MsoList/)) {
						alert("Oops! You pasted content from MS Word. We're gonna remove it.");
						iframe_textarea.html(prepaste_content)
					}
				}, 100);
			}
		}
    });
	
	$('.textarea-summary').wysiwyg({    
   			css: '/styles/editor.css',
			controls: {
            h1: { visible: false },
			h2: { visible : false },
			h3: { visible: false },
			indent: { visible : false },
			outdent: { visible : false },
			insertHorizontalRule: { visible : false },
			subscript: { visible : false },
			superscript: { visible : false },
			insertTable: { visible : false },
			insertImage: { visible : false },
			alertSep: { separator: true },
        	smiles: {
            	visible: true,
            	exec: function() { $('.smilies').toggle(); },
            	className: 'smiles'
       		}
        }
    });
	
	$('.textarea-blog').wysiwyg({    
   			css: '/styles/editor.css',
			controls: {
            h1: { visible: false },
			h2: { visible : false },
			h3: { visible: false },
			indent: { visible : false },
			outdent: { visible : false },
			insertHorizontalRule: { visible : false },
			subscript: { visible : false },
			superscript: { visible : false },
			insertTable: { visible : false },
			insertImage: { 
				visible : true,
				exec: function() { $.colorbox({width:"1030px", height:"580px", iframe:true, href:"images.php", scrolling: false, onCleanup:function(){ $('#photoUploads').hide(); }}); $('#photoUploads').show(); },
			},
			alertSep: { separator: true },
        	smiles: {
            	visible: true,
            	exec: function() { $('.smilies_2').toggle(); },
            	className: 'smiles'
       		},
			separator: { visible : true },
        	youtube: {
            	visible: true,
            	exec: function() { $('#youtubeContainer').show();$.colorbox({width:"440px", height:"400px", inline:true, href:"#youtubeContainer", scrolling: false, onCleanup:function(){ $('#youtubeContainer').hide(); } }); },
            	className: 'youtube'
       		}
        }
    });
	
	$('#addYoutube').click(function(){
		var ytlink = $('#youtube').val();
		ytlink_sp = ytlink.split('?v=');
		yt = ytlink_sp[1];
		yt_r = yt.split('&');
		yt = yt_r[0];
		var html = '<iframe title="YouTube video player" width="500" height="350" src="http://www.youtube.com/embed/'+yt+'" frameborder="0" allowfullscreen></iframe>'; 		
		$('#blog').wysiwyg('insertHtml', html);
		$('#blog').wysiwyg('save');
		$.colorbox.close();
	});
	
	$('#displayComment').click(function(){
		$('#hiddenComment').show();
		$.colorbox({width:"440px", height:"150px", inline:true, href:"#hiddenComment", scrolling: false, onCleanup:function(){ $('#hiddenComment').hide(); } });
	});
	
	$('.chat .chat_form .richtext').wysiwyg({       
   			css: '/styles/editor.css',
			controls: {
            h1: { visible: false },
			h2: { visible : false },
			h3: { visible: false },
			indent: { visible : false },
			outdent: { visible : false },
			insertHorizontalRule: { visible : false },
			subscript: { visible : false },
			superscript: { visible : false },
			insertTable: { visible : false },
			insertImage: { visible : false },
			alertSep: { separator: true },
        	smiles: {
            	visible: true,
            	exec: function() { $('.smilies').toggle(); },
            	className: 'smiles'
       		},
			separator: { visible : false },
			youtube: {visible: false}
        },
		
		events: {
			keypress: function(e){
				if(e.keyCode == 13 || e.which == 13){
					e.preventDefault();
					$("#chatpost").submit();
				}
			},
		}
    });
	
	$('.business').wysiwyg({       
   			css: '/styles/editor.css',
			controls: {
            h1: { visible: false },
			h2: { visible : false },
			h3: { visible: false },
			indent: { visible : false },
			outdent: { visible : false },
			insertHorizontalRule: { visible : false },
			subscript: { visible : false },
			superscript: { visible : false },
			insertTable: { visible : false },
			insertImage: { visible : false },
			alertSep: { separator: false },
        	smiles: { visible: false },
			separator: { visible : false },
			youtube: {visible: false}
        }
    });
		
	$('a[href="#insertImage"]').click(function() {
		$('.richtext').wysiwyg('insertImage', $(this).find('img').attr('src'), { 'class': 'smile', 'className': 'smile' });
		$('.smilies').toggle();
	});
	
	$('a[href="#insertSmileSummary"]').click(function() {
		$('.textarea-summary').wysiwyg('insertImage', $(this).find('img').attr('src'), { 'class': 'smile', 'className': 'smile' });
		$('.smilies').toggle();
	});
	
	$('a[href="#insertSmileBlog"]').click(function() {
		$('.textarea-blog').wysiwyg('insertImage', $(this).find('img').attr('src'), { 'class': 'smile', 'className': 'smile' });
		$('.smilies_2').toggle();
	});
	
	// Star ratings scripts
	$(".rate").children().not(":radio").hide();
	$(".rate").stars({
		cancelShow: false,
		oneVoteOnly: true,
		callback:function(ui, type, value){vote(value, $(this).attr("name"))}
	});
	
	function vote(value, name){
		var blog = name.substring(5);
		$.ajax({
			type: "POST",
			url: "http://www.purplecoo.com/common/rate.php",
			dataType: 'json',
			data: "value="+value+"&blog_id="+blog,
			error: function(data){
				// Handle error
			},
			success: function(data){
				// Handle success
			}
		}, "json");
	}
	$(".rate_photo").children().not(":radio").hide();
	$(".rate_photo").stars({
		cancelShow: false,
		oneVoteOnly: true,
		callback:function(ui, type, value){vote_photo(value, $(this).attr("name"))}
	});
	
	function vote_photo(value, name){
		var photo = name.substring(6);
		$.ajax({
			type: "POST",
			url: "http://www.purplecoo.com/galleries/rate.php",
			dataType: 'json',
			data: "value="+value+"&photo_id="+photo,
			error: function(data){
				// Handle error
			},
			success: function(data){
				// Handle success
			}
		}, "json");
	}
	
	$(".rate_article").children().not(":radio").hide();
	$(".rate_article").stars({
		cancelShow: false,
		oneVoteOnly: true,
		callback:function(ui, type, value){vote_article(value, $(this).attr("name"))}
	});
	
	function vote_article(value, name){
		var article = name.substring(8);
		$.ajax({
			type: "POST",
			url: "http://www.purplecoo.com/article/rate.php",
			dataType: 'json',
			data: "value="+value+"&article_id="+article,
			error: function(data){
				// Handle error
				alert('no');			},
			success: function(data){
				// Handle success
				alert('yes');	
			}
		}, "json");
	}

	
	
	$('.reasons-list').jcarousel({
        scroll: 1
    });
	
	$('.photo_carousel').jcarousel({
        scroll: 5
    });
	
	// Set up form validation check
	$(".form").validationEngine();
		
	// Set up username validation check
	$("#username").focusout(
		function(){
			if($("#username").val().length > 2){
				var username = $("#username").val();
				$.ajax({
				   type: "POST",
				   url: "validate-user.php",
				   dataType: 'json',
				   data: "username="+username,
				   error: function(){
					 $.validationEngine.buildPrompt("#username","* Could not check username, please try again","error")	
				   },
				   success: function(data){
					  if(data.result == 'true'){
						   $.validationEngine.buildPrompt("#username","* Sorry this username is already taken, please enter an alternative","error");	
					   }else{
						   $.validationEngine.buildPrompt("#username","* This user name is available","pass");	
					   }
				   }
				 }, "json");
			}
		}
	);
	
	// Set up email validation check
	$("#email").focusout(
		function(){
			if($("#email").val().length > 2){
				var email = $("#email").val();
				$.ajax({
				   type: "POST",
				   url: "validate-email.php",
				   dataType: 'json',
				   data: "email="+email,
				   error: function(){
					 $.validationEngine.buildPrompt("#email","* Could not check email, please try again","error")	
				   },
				   success: function(data){
					  if(data.result == 'true'){
						   $.validationEngine.buildPrompt("#email","* Sorry this email is already in use, please enter an alternative","error");	
					   }else{
						   $.validationEngine.buildPrompt("#email","* This email is available to register","pass");	
					   }
				   }
				 }, "json");
			}
		}
	);
	
	// Set up captcha validation check
	$("#captcha").focusout(
		function(){
			var captcha = $("#captcha").val();
			$.ajax({
			   type: "POST",
			   url: "captcha.php",
			   dataType: 'json',
			   data: "captcha="+captcha,
			   error: function(){
				 $.validationEngine.buildPrompt("#captcha","* Code does not match, please try again","error")	
			   },
			   success: function(data){
				  if(data.result == 'true'){
					   $.validationEngine.buildPrompt("#captcha","* Code is correct","pass");	
				   }else{
					   $.validationEngine.buildPrompt("#captcha","* Code does not match, please try again<br />* Remember the code is case sensitive","error");	
				   }
			   }
			 }, "json");	
		}
	);

	
	function searchSite(){
		if($("#searchBar").val().length > 2){
				var searchArea = $("#searchArea").val();
				var searchValue = $("#searchBar").val();
				$.ajax({
				   type: "POST",
				   url: "searchLive.php",
				   dataType: 'json',
				   data: "searchValue="+searchValue+"&area="+searchArea,
				   error: function(){
					 $.validationEngine.buildPrompt("#searchBar","* Could not search, please try again","error")	
				   },
				   success: function(data){
					  $("#searchResults ul").html(data.result);
				   }
				 }, "json");
			}	
	}
	
	$("#searchButton").click(function(){
			searchSite();
			return false;
	});
	
	// Set up email validation check
	$("#searchArea").change(
		function(){
			$("#searchResults ul").html('<li class="loading"><img src="/images/loading-chat.gif" title="Loading" /></li>');
			searchSite();
		}
	);
	
	// Set up email validation check
	$("#searchBar").keyup(
		function(){
			$("#searchResults ul").html('<li class="loading"><img src="/images/loading-chat.gif" title="Loading" /></li>');
			searchSite();
		}
	);
	
	// Close all promts
	$("#prompts").click(
		function(){
			$.validationEngine.closePrompt('.formError',true)	
		}
	);
	
	// Login box 
	$(".loginclick").colorbox({transition:"fade", width: 420});
			
	
});
