$(document).ready(function() {
	
	$("a.readon").colorbox({transition:"fade"});
	$("a[rel='gallery']").colorbox({transition:"fade"});
	//$("a.image-popup").colorbox({transition:"fade"});

	/* CLOSE BOX FUNCTIONALITY =========================
	## @author - Byron Rode
	## @desc - Hides any info boxes on click. Can be set
	## to close permanently using a cookie. Cookie check
	## needs to be done via PHP to ensure that box isn't
	## reloaded on refresh.
	================================================= */

	// first gen - alert function close (temp)
	$("a.close").click(function() {
	
		$('#alertbox-generic').fadeTo('slow', 0).hide('slow');
		$('#vertical-select').fadeTo('slow', 0).hide('slow');
		return false;
	
	});
		
	// first gen - alert function close (perm) - set cookie
	$("a.close-perm").click(function() {
	
		$('#alertbox-generic').fadeTo('slow', 0).hide('slow');
		$.cookie('pt_hide_alert', 'true', { expires: 365 });
		return false;
	
	});
	
	// home page - alert function close (temp)
	$("a.close-home").click(function() {
	
		$('#alertbox').fadeTo('slow', 0).hide('slow');
		$('#vertical-select').fadeTo('slow', 0).hide('slow');
		return false;
	
	});
		
	// home page - alert function close (perm) - set cookie
	$("a.close-perm-home").click(function() {
	
		$('#alertbox').fadeTo('slow', 0).hide('slow');
		$.cookie('pt_hide_alert_home', 'true', { expires: 365 });
		return false;
	
	});
	
	/* VOTE FUNCTIONALITY MULTIPLE LISTINGS ============
	## @author - Byron Rode
	## @desc - Themelet up and down voting. Results
	## submitted to database and store as well as
	## setting cookies to ensure that double voting is
	## prevented
	================================================= */
	
	// remove click function and hide loading images
	$("span.vote-load").hide();
	$("a.ptvote").click(function() {
	
		
		// get classes and define post vars for ajax call
		var $pt_el_classes = $(this).attr("class").split(' ');
		var $pt_votetype = $pt_el_classes[1];
		var $pt_pid = $pt_el_classes[2].replace('setvote', '');
		var pt_pid = $pt_el_classes[2].replace('setvote', '');
		
		// set variables and parent div
		var $pt_op = '';
		var $pt_parent = 'bvc'+$pt_pid;
		
		// show loading image
		$("."+$pt_parent+" > div.vote-link > span.vote-load").show();
		$("."+$pt_parent+" > div.vote-link > a").fadeTo('fast', 0.2);
		$("#post-votes > span.vote-text").fadeTo('fast', 0.2);
		
		// do ajax call and update page
		$.ajax({
			
			url: "/themelets/wp-content/themes/themelets/functions/vote/pt_update_votes.php?do=vote&pid="+$pt_pid+"&vt="+$pt_votetype,
			error: function(){
				// something went wrong, report the error
    			alert('It seems there was a problem voting.\nPlease try again in a few minutes.');
				$("."+$pt_parent+" > div.vote-link").empty("a").removeClass("vote-link").addClass("vote-img");
				$("."+$pt_parent+" > div.vote-link").fadeIn('fast');
				$("."+$pt_parent+" > div.vote-link > span.vote-load").hide();
				$("#post-votes > span.vote-text").fadeTo('fast', 1);
				
			},
			success: function(){
			
				var $pt_votes_output = $("."+$pt_parent+" > span.vote-text"); // output same as input
				var $pt_votes_text = ['votes','vote']; // for replacment
			
				// get current votes and update with new vote count
				var $pt_votes = parseInt($pt_votes_output.text().replace($pt_votes_text, ''));
				// if voteup add 1
				if($pt_votetype == 'voteup'){ 
					$pt_new_votes = $pt_votes+1; 
				}
				// if votedown -1
				if($pt_votetype == 'votedown'){
					$pt_new_votes = $pt_votes-1; 
				}
			
				// output grammatically correct wording
				if($pt_new_votes == '1') { $pt_vote_text = ' vote'; } else { $pt_vote_text = ' votes'; }
			
				// update text and remove links and classes
				$pt_votes_output.text($pt_new_votes + $pt_vote_text);
				$("."+$pt_parent+" > div.vote-link").empty("a").removeClass("vote-link").addClass("vote-img");
				$("."+$pt_parent+" > div.vote-link").fadeIn('fast');
				$("."+$pt_parent+" > div.vote-link > span.vote-load").hide();
				$("#post-votes > span.vote-text").fadeTo('fast', 1);
				
				
				/* VERTICAL SUBMISSION FUNCTIONALITY - MULTI PAGE == 
				## @author - Byron Rode
				## @desc - Submits users vertical selection to DB &
				## stores results based on user IP and Cookie.
				## Duplicate submission prevented by cookie. Form
				## hidden using Cookie and DB check once submitted.
				## This is invoked only on the multi page listings,
				## after a user has voted.
				================================================= */
				
				// invoke color box if no cookie is stored.
				var ptVertCookie = $.cookie('vert_has_submitted'+pt_pid);
				
				if(ptVertCookie !== 'true'){
				
					$(".vert_load").css('display', 'none');
					$('#vert'+pt_pid+' > .vert_response').css('display', 'none');
					$(".vert_submit"+pt_pid).submit(function(){
			
						var options = {
							// url: 'test', - for testing
							target: '#vert'+pt_pid+' > .vert_response',
							beforeSubmit: function(){
							
								var theForm = $('.vert_submit'+pt_pid+' select[name=cms_vertical], .vert_submit'+pt_pid+' select[name=cat_vertical]');
								var cmsValue = theForm[0].value;
								var catValue = theForm[1].value;
								var formSub  = true;
								
								if (!cmsValue[0] && !catValue[0]) {
						        	alert('Please select a CMS and Style option from the dropdowns below');
						        	formSub = false;
						       	return formSub; 
						   		}
						  		if (!cmsValue[0]) {
						        	alert('Please select a CMS option from the CMS dropdown'); 
							        formSub = false;
						       	return formSub;
						    	}
						    	if (!catValue[0]) {
						        	alert('Please select a Style option from the style dropdown');
						        	formSub = false;
						       	return formSub;
						    	}
								
								if(formSub){
									$('#vert'+pt_pid+' > .text-wrapper').css('display', 'none');	
									$(".vert_load").css('display', 'block');
									$('#vert'+pt_pid+' > .vert_response').css('display', 'block');
								}
								
							},
							success: function(){
								$('#vert'+pt_pid).fadeTo(4000, 1).fadeTo('fast',0).hide('fast', function(){ $('#modalClose').click(); });
							}
						};
						
						$(this).ajaxSubmit(options); 
						return false;
						
					});
				
					$(this).colorbox({inline: true, href:"#vert"+pt_pid, open:true, title:"&nbsp;"});
					
				
				} else {
					
					return true;
				
				}
				
 			}
		});
	
	// disable clicks			
	return false; 
	});
	
	/* VOTE FUNCTIONALITY - SINGLE PAGES ===============
	## @author - Byron Rode
	## @desc - Themelet up and down voting. Results
	## submitted to database and store as well as
	## setting cookies to ensure that double voting is
	## prevented
	================================================= */
	
	// remove click function and hide loading images
	$("span.vote-load").hide();
	$("a.ptvote-post").click(function() {
	
		
		// get classes and define post vars for ajax call
		var $pt_el_classes = $(this).attr("class").split(' ');
		var $pt_votetype = $pt_el_classes[1];
		var $pt_pid = $pt_el_classes[2].replace('setvote', '');
		var pt_pid = $pt_el_classes[2].replace('setvote', '');
		
		// set variables and parent div
		var $pt_op = '';
		var $pt_parent = 'bvc'+$pt_pid;
		
		// show loading image
		$("."+$pt_parent+" > div.vote-link > span.vote-load").show();
		$("."+$pt_parent+" > div.vote-link > a").fadeTo('fast', 0.2);
		$("#post-votes > span.vote-text").fadeTo('fast', 0.2);
		
		// do ajax call and update page
		$.ajax({
			
			url: "/themelets/wp-content/themes/themelets/functions/vote/pt_update_votes.php?do=vote&pid="+$pt_pid+"&vt="+$pt_votetype,
			error: function(){
				// something went wrong, report the error
    			alert('It seems there was a problem voting.\nPlease try again in a few minutes.');
				$("."+$pt_parent+" > div.vote-link").empty("a").removeClass("vote-link").addClass("vote-img");
				$("."+$pt_parent+" > div.vote-link").fadeIn('fast');
				$("."+$pt_parent+" > div.vote-link > span.vote-load").hide();
				$("#post-votes > span.vote-text").fadeTo('fast', 1);
				
			},
			success: function(){
			
				var $pt_votes_output = $("."+$pt_parent+" > span.vote-text"); // output same as input
				var $pt_votes_text = ['votes','vote']; // for replacment
			
				// get current votes and update with new vote count
				var $pt_votes = parseInt($pt_votes_output.text().replace($pt_votes_text, ''));
				// if voteup add 1
				if($pt_votetype == 'voteup'){ 
					$pt_new_votes = $pt_votes+1; 
				}
				// if votedown -1
				if($pt_votetype == 'votedown'){
					$pt_new_votes = $pt_votes-1; 
				}
			
				// output grammatically correct wording
				if($pt_new_votes == '1') { $pt_vote_text = ' vote'; } else { $pt_vote_text = ' votes'; }
			
				// update text and remove links and classes
				$pt_votes_output.html($pt_new_votes + '<br />' + $pt_vote_text);
				$("."+$pt_parent+" > div.vote-link").empty("a").removeClass("vote-link").addClass("vote-img");
				$("."+$pt_parent+" > div.vote-link").fadeIn('fast');
				$("."+$pt_parent+" > div.vote-link > span.vote-load").hide();
				$("#post-votes > span.vote-text").fadeTo('fast', 1);
				
 			}
		});
	
	// disable clicks			
	return false; 
	});
	
	
	/* VERTICAL SUBMISSION FUNCTIONALITY ===============
	## @author - Byron Rode
	## @desc - Submits users vertical selection to DB &
	## stores results based on user IP and Cookie.
	## Duplicate submission prevented by cookie. Form
	## hidden using Cookie and DB check once submitted
	================================================= */

	
	// Vertical Submit
	$(".vert_load").css('display', 'none');	
	$(".vert_submit").submit(function(){
		
		var options = {
			target: '.vert_response',
			beforeSubmit: function(){
				
				var cmsValue = $('select[name=cms_vertical]').fieldValue(); 
		    	var catValue = $('select[name=cat_vertical]').fieldValue();
		    	var formSub  = true;
										
				if (!cmsValue[0] && !catValue[0]) {
		        	alert('Please select a CMS and Style option from the dropdowns below');
		        	formSub = false;
		       	return formSub; 
		   		}
		  		if (!cmsValue[0]) {
		        	alert('Please select a CMS option from the CMS dropdown'); 
			        formSub = false;
		       	return formSub;
		    	}
		    	if (!catValue[0]) {
		        	alert('Please select a Style option from the style dropdown');
		        	formSub = false;
		       	return formSub;
		    	}
				
				if(formSub){
					$('.text-wrapper').css('display', 'none');
					$(".vert_load").css('display', 'block');
				}
			},
			success: function(){
				$(".vert_load").css('display', 'none');
				$('.vert_response').css('display', 'block');
				$('.vertical-select').fadeTo(5000, 1).fadeTo('fast',0).hide('fast');
			}
		};
	
		$(this).ajaxSubmit(options); 
		return false;
	
	});
	
});
