 
 $(document).ready(function()
		{
			
			$("div#invitations").click(function(){
				
				load_invitations();
				
			});

			$("div#select_day").click(function(){																
				load_day();		
				//return false;
			});
																 

			$("div#select_week").click(function(){
					
				load_week();
				
			});
			
			$("div#select_month").click(function(){
			

				load_month();
				
			});
			
			$("span.saved_event").click(function(){
				var event_id = $(this).attr("rel");
				load_event("", event_id);
				return false;
			});	
			
			$("a#next_month").click(function(){
				load_month("next");	
				return false;
			});

			$("a#prev_month").click(function(){
				load_month("prev");	
				return false;
			});

			$("a#next_day").click(function(){
				load_day("next");
				return false;
			});


			$("a#prev_day").click(function(){
				load_day("prev");
				return false;
			});

			$("a#next_week").click(function(){
				load_week("next");
				return false;
			});

			$("a#prev_week").click(function(){
				load_week("prev");
				return false;
			});

			$("div.calendar_boxes").click(function(){
				var utime = $(this).attr("utime");
				load_event(utime);
				return false;
			});	
	
			$("a.day_num").click(function(){
				var d = $(this).attr("day");
				var m = $(this).attr("month");
				var Y = $(this).attr("year");
				
				load_day("goto", d, m, Y);
				return false;
			});	
			
			function load_invitations(){
				$("div#messages").css( {display: "block" } );	
				$("div#view").load("/standalone/modules/calendar/invitations.php", function(){
					$("div#messages").css( {display: "none" } );
					
					$("a[@id^=accept_invitation_]").click(function(){
							var id = 	$(this).attr("id").substring(18);
							var num_of_invitations = $("span#num_of_invitations").html();
							
							if( num_of_invitations == 1 ){
								num_of_invitations = 0;
							}else{
								num_of_invitations -=1;
							}
							
							$("div#an_invitation_"+id+"").fadeOut("slow");
							$("span#num_of_invitations").html(num_of_invitations);
							$.get("/standalone/modules/calendar/invitations.php", { accept: id } );
					});																		 

					$("a[@id^=reject_invitation_]").click(function(){
							var id = 	$(this).attr("id").substring(18);
							var num_of_invitations = $("span#num_of_invitations").html();
							
							if( num_of_invitations == 1 ){
								num_of_invitations = 0;
							}else{
								num_of_invitations -=1;
							}
							
							$("div#an_invitation_"+id+"").fadeOut("slow");
							$("span#num_of_invitations").html(num_of_invitations);
							$.get("/standalone/modules/calendar/invitations.php", { reject: id } );
					});

				});	
				$("div.view_selected").attr("class", "bar_element");																
				$("div#invitations").removeAttr("class");
				$("div#invitations").addClass("view_selected");
			}
	
			function load_month( action ){
				
				$("div#messages").css( {display: "block" } );	
				//alert("/standalone/modules/calendar/month.php?m="+m+"&Y="+Y+"");
				$("div#view").load("/standalone/modules/calendar/month.php?action="+action+"", function(){
					$("div#messages").css( {display: "none" } );
					//$('.datepicker').datePicker();	

					$("span.saved_event").click(function(){
						var event_id = $(this).attr("rel");
						load_event("", event_id);
						return false;
					});

					$("a#next_month").click(function(){
						load_month("next");
						return false;
					});

					$("a#prev_month").click(function(){	
						load_month("prev");
						return false;
					});
	

					$("a.day_num").click(function(){
						var d = $(this).attr("day");
						var m = $(this).attr("month");
						var Y = $(this).attr("year");
						
						load_day("goto", d, m, Y);
						return false;
					});
					$("div.calendar_boxes").click(function(){
						var utime = $(this).attr("utime");
						load_event(utime);
						return false;
					});					

				});
												
				$("div.view_selected").attr("class", "bar_element");																
				$("div#select_month").removeAttr("class");
				$("div#select_month").addClass("view_selected");

			}
			
			function load_day( action, d, m, Y ){

	
				$("div#messages").css( {display: "block" } );	
					$("div#view").load("/standalone/modules/calendar/day.php?action="+action+"&d="+d+"&m="+m+"&Y="+Y+"", function(){
		
						$("div#messages").css( {display: "none" } );
		
						$("a#next_day").click(function(){
							load_day("next");
							return false;
						});
		
						$("a#prev_day").click(function(){
							load_day("prev");
							return false;
						});
						$("div.saved_event").click(function(){
							var event_id = $(this).attr("rel");
							load_event("", event_id);
							return false;
						});	
						$("div.half_hour").click(function(){
							var utime = $(this).attr("utime");
							load_event(utime);
							return false;
						});							
				});
				$("div.view_selected").attr("class", "bar_element");																
				$("div#select_day").removeAttr("class");
				$("div#select_day").addClass("view_selected");	
			}			

			function load_week( action ){
				$("div#messages").css( {display: "block" } );	
				$("div#view").load("/standalone/modules/calendar/week.php?action="+action+"", function(){
	
					$("div#messages").css( {display: "none" } );
					
					$("div.saved_event").click(function(){
						var event_id = $(this).attr("rel");
						load_event("", event_id);
						return false;
					});	
	
					$("a#next_week").click(function(){
						load_week("next");
						return false;
					});
	
					$("a#prev_week").click(function(){
						load_week("prev");
						return false;
					});
					$("div.half_hour").click(function(){
						var utime = $(this).attr("utime");
						load_event(utime);
						return false;
					});						
					
				});
				$("div.view_selected").attr("class", "bar_element");																
				$("div#select_week").removeAttr("class");
				$("div#select_week").addClass("view_selected");
			}	
			function load_event( utime, event_id ){
				$("div#messages").css( {display: "block" } );	
				
				var append = "utime="+utime+"";
				if( utime == "" && event_id !="" ){
					append = "event_id="+event_id+"";
				}
				
				$("div#view").load("/standalone/modules/calendar/add_event.php?"+append+"", function(){
					$("div#messages").css( {display: "none" } );
					
					Date.firstDayOfWeek = 7;
					Date.format = 'mm/dd/yyyy';
					$('.date-pick').datePicker().dpSetSelected(new Date().asString());


					
					$("a#selected_item").click(function(){
						$("div#select_box").toggle();															
					});																		
					
					$("div.select_item").click(function(){
						var main_item = $("a#selected_item").html();
						var select_item = $(this).html();
						
						$(this).html( main_item );
						$("a#selected_item").html( select_item );
						$("div#select_box").hide();	
						
																							
					});

					$('a#sort_by_first').click(function(){	
						//$("select[@name='invited_last']").val("");																	
						//$("div#attendees_last").hide();
						//$("div#attendees_first").show();
						var lb = document.getElementById('teez');
						sortSelect( lb, false );
					});	

					$('a#sort_by_last').click(function(){	
						//$("select[@name='invited_first']").val("");																 
						//$("div#attendees_last").show();
						//$("div#attendees_first").hide();	
						var lb = document.getElementById('teez');
						sortSelect( lb, true );
					});
					
					$('a.add_attendees').click(function(){	
																							
						var m1 = document.getElementById('not_added_attendees');
						var m2 = document.getElementById('added_attendees');
						
						m1len = m1.length ;
						for ( i=0; i<m1len ; i++){
								if (m1.options[i].selected == true ) {
										m2len = m2.length;
										m2.options[m2len]= new Option(m1.options[i].text,m1.options[i].value );
								}
						}
				
						for ( i = (m1len -1); i>=0; i--){
								if (m1.options[i].selected == true ) {
										m1.options[i] = null;
								}
						}																	
																							
					});
					
					$('a.remove_attendees').click(function(){	
			
						var m1 = document.getElementById('not_added_attendees');
						var m2 = document.getElementById('added_attendees');
						
						m2len = m2.length ;
						for ( i=0; i<m2len ; i++){
								if (m2.options[i].selected == true ) {
										m1len = m1.length;
										m1.options[m1len]= new Option(m2.options[i].text, m2.options[i].value );
								}
						}
						for ( i=(m2len-1); i>=0; i--) {
								if (m2.options[i].selected == true ) {
										m2.options[i] = null;
								}
						}
	
					});

					$("a#cancel_event").click(function(){	
						if( $("div[@class='view_selected']").attr('id') == 'select_week' ){
							load_week();
						} else if( $("div[@class='view_selected']").attr('id') == 'select_month' ) {
							load_month();	
						} else if ( $("div[@class='view_selected']").attr('id') == 'select_day' ) {
							load_day();	
						}
					});	
					
					$("a#save_event").click(function(){	
						
						//reccurence
						var reccurence="&reccur_type=none";
						var reccur_type = $("input[@name='recurrence_pattern']:checked").val();
						if( reccur_type != "none" ){
							reccurence = "&reccur_type="+reccur_type+"";
							//Daily
							if( reccur_type == "daily" ){
								//first option
								if( $("input[@name='daily_select']:checked").val() == 0 ){
									reccurence +="&every_day="+$("input[@name='every_day']").val()+"";
								//second option
								}else if( $("input[@name='daily_select']:checked").val() == 1 ){
									reccurence +="&everyweek=1";
								}
							//Weekly
							}else if( reccur_type == "weekly" ){
								reccurence +="&every_week="+$("input[@name='every_week']").val()+"";
								
								//sunday
								if( $("input[@name='weekly_sunday']:checked").val() == 1 ){
									reccurence +="&weekly_sunday=1";
								}
								//monday
								if( $("input[@name='weekly_monday']:checked").val() == 1 ){
									reccurence +="&weekly_monday=1";
								}
								//tuesday
								if( $("input[@name='weekly_tuesday']:checked").val() == 1 ){
									reccurence +="&weekly_tuesday=1";
								}
								//wednesday
								if( $("input[@name='weekly_wednesday']:checked").val() == 1 ){
									reccurence +="&weekly_wednesday=1";
								}
								//thursday
								if( $("input[@name='weekly_thursday']:checked").val() == 1 ){
									reccurence +="&weekly_thursday=1";
								}
								//friday
								if( $("input[@name='weekly_friday']:checked").val() == 1 ){
									reccurence +="&weekly_friday=1";
								}
								//saturday
								if( $("input[@name='weekly_saturday']:checked").val() == 1 ){
									reccurence +="&weekly_saturday=1";
								}								
							
							//Monthly
							}else if( reccur_type == "monthly" ){
								//option1
								if( $("input[@name='monthly_option']:checked").val() == 0 ){
									reccurence +="&option=0&day="+$("input[@name='monthly_day']").val()+"&month="+$("input[@name='monthly_month']").val()+"";
								//option2
								}else if( $("input[@name='monthly_option']:checked").val() == 1 ){
									reccurence +="&option=1&position="+$("select[@name='position']").val()+"&position_of="+$("select[@name='position_of']").val()+"&of_every="+$("input[@name='of_every']").val()+"";	
								}
								
							// Yearly
							}else if( reccur_type == "yearly" ){
								// Option 1
								if( $("input[@name='yearly_option']:checked").val() == 0 ){
									reccurence +="&option=0&month="+$("select[@name='yearly_month']").val()+"&day="+$("input[@name='yearly_day']").val()+"";
								// Option 2
								}else if( $("input[@name='yearly_option']:checked").val() == 1 ){
									reccurence +="&option=1&position="+$("select[@name='yearly_position']").val()+"&position_of="+$("select[@name='yearly_position_of']").val()+"&position_of_month="+$("select[@name='yearly_position_of_month']").val()+"";
								}
							}

							//start date
							reccurence +="&reccur_sday="+$("input[@name='recurrence_sday']").val()+"";		
							if( $("input[@name='recur_range']:checked").val() == "no_end" ){
								reccurence +="&no_end_date=1";
							//end after
							}else if( $("input[@name='recur_range']:checked").val() == "end_after" ){
								reccurence +="&end_after="+$("input[@name='end_after_reccurs']").val()+"";				
							//end by
							}else if( $("input[@name='recur_range']:checked").val() == "end_by" ){
								reccurence +="&end_by="+$("input[@name='recurrence_eday']").val()+"";	
							}
							

						}
						

						
						var m2 = document.getElementById('added_attendees');
						var attendees ="&attendees=";
						
						m2len = m2.length ;
						for ( i=0; i<m2len ; i++){
							attendees += ""+m2.options[i].value+",";
						}
					
						
						var categories_id = $("a#selected_item span:first-child").attr("rel");

						var category = "&categories_id="+categories_id+"";
						
						var subject = $("input[@name='subject']").val();
						subject = replaceAll(" ", "|", subject );
									
						var location = $("input[@name='location']").val();
						location = replaceAll(" ", "|", location );
						
						var sday = $("input[@name='sday']").val()		
						
						var stime = $("input[@name='select_stime']").val();
						stime = stime.replace(" ", "");
						var eday = $("input[@name='eday']").val();
						var etime = $("input[@name='select_etime']").val();
						etime = etime.replace(" ", "");
						var notes = $("textarea[@name='notes']").val();
						notes = replaceAll(" ", "|", notes );
						notes = replaceAll("\n", "~", notes );
						
						var delete_event = "";
						if( $("input#delete:checked").val() > 1 ){
							delete_event = "&delete="+$("input#delete:checked").val()+"";
						}
						
						if( subject == "" ){
							alert("Subject Field is empty");
							return false;
						}
						var update = $("input#update").attr("value");
						
						var update_append = "";
						if ( update > 1 ){
							update_append = "&update="+update+""
						}
						$("div#view").load("/standalone/modules/calendar/save_event.php?subject="+subject+"&location="+location+"&sday="+sday+"&stime="+stime+"&eday="+eday+"&etime="+etime+"&notes="+notes+update_append+category+delete_event+attendees+reccurence+"", function(){

							if( $("div[@class='view_selected']").attr('id') == 'select_week' ){
								load_week();
							} else if( $("div[@class='view_selected']").attr('id') == 'select_month' ) {
								load_month();	
							} else if ( $("div[@class='view_selected']").attr('id') == 'select_day' ) {
								load_day();	
							}

						});
						return false;
					});	
					
					
					$("a.rp").click(function(){								 
						$("div.recurrence").toggle();		
						return false;
					});	
					

					$("a.invite_users").click(function(){								 
						$("div.users_invitation").toggle();		
						return false;
					});	

					$("input[value='none']").click(function(){
						$("div.daily").hide();
						$("div.weekly").hide();
						$("div.monthly").hide();
						$("div.yearly").hide();
					});

					$("input[value='daily']").click(function(){
						$("div.daily").show();
						$("div.weekly").hide();
						$("div.monthly").hide();
						$("div.yearly").hide();
					});	

					$("input[value='weekly']").click(function(){
						$("div.daily").hide();
						$("div.weekly").show();
						$("div.monthly").hide();
						$("div.yearly").hide();
					});	
					
					$("input[value='monthly']").click(function(){
						$("div.daily").hide();
						$("div.weekly").hide();
						$("div.monthly").show();
						$("div.yearly").hide();
					});	
					
					$("input[value='yearly']").click(function(){
						$("div.daily").hide();
						$("div.weekly").hide();
						$("div.monthly").hide();
						$("div.yearly").show();
					});						

					$("a[id^=tpicker]").click(function(){
						var id = $(this).attr("id");
						$("div.timeticker_main").css( { display:"none" } );
						$("div#"+id+"_container").load("/standalone/modules/calendar/timepicker.php", function(){
	
		
							$("span.more").click(function(){
								
								//alert( expanded );
								$("div.mins").css({display:"block"});
								$(this).css({display: "none"});
								$("span.less").css({display: "inline"});
							
							});
							
							
							$("span.less").click(function(){
								
								$("div.mins").css({display:"none"});
								$("div.mins_default").css({display:"block"});
								
								$(this).css({display: "none"});
								$("span.more").css({display: "inline"});
								
							});
							
							$("div.hour_box").click(function(){
								$("div[class^=hour_box]").removeClass("hour_box_selected");
								$("div[class^=hour_box]").addClass("hour_box");
								$(this).addClass("hour_box_selected");
							});
							
															 
							
							$("div.mins").click(function(){
								var hour = $("div.hour_box_selected").html();
								var minute = $(this).html();
								var ampm = $("div.hour_box_selected").attr("rel");
								
								var alltogether = ""+hour+minute+" "+ampm+"";
								
								if(	hour!=null ){
									$("input#"+id+"_input").attr("value", ""+alltogether+"");
									$("div.timeticker_main").css( { display:"none" } );
								}
										
							});
																			 						
						});
					});
				
				});
			}
			//This function is string replace funtion that loops through the whole string
			function replaceAll( findme, newme, me ){
				var strReplaceAll = me;
				var intIndexOfMatch = strReplaceAll.indexOf( findme );
				while (intIndexOfMatch != -1){
					strReplaceAll = strReplaceAll.replace( findme, newme );
					intIndexOfMatch = strReplaceAll.indexOf( findme );
					
				}
				return strReplaceAll;
			}
			
	 /*************Select Box sort functions*************/
			// sort function - ascending (case-insensitive)
			function sortFuncAsc(record1, record2) {
					var value1 = record1.optText.toLowerCase();
					var value2 = record2.optText.toLowerCase();
					if (value1 > value2) return(1);
					if (value1 < value2) return(-1);
					return(0);
			}

			// sort function - descending (case-insensitive)
			function sortFuncDesc(record1, record2) {
					var value1 = record1.optText.toLowerCase();
					var value2 = record2.optText.toLowerCase();
					if (value1 > value2) return(-1);
					if (value1 < value2) return(1);
					return(0);
			}

			function sortSelect(selectToSort, ascendingOrder) {
					if (arguments.length == 1) ascendingOrder = true;    // default to ascending sort

					// copy options into an array
					var myOptions = [];
					for (var loop=0; loop<selectToSort.options.length; loop++) {
							myOptions[loop] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].value };
					}

					// sort array
					if (ascendingOrder) {
							myOptions.sort(sortFuncAsc);
					} else {
							myOptions.sort(sortFuncDesc);
					}

					// copy sorted options from array back to select box
					selectToSort.options.length = 0;
					for (var loop=0; loop<myOptions.length; loop++) {
							var optObj = document.createElement('option');
							optObj.text = myOptions[loop].optText;
							optObj.value = myOptions[loop].optValue;
							selectToSort.options.add(optObj);
					}
			}

		/*************************************************************/


		}
		
 );

