$(function() {
		$( "#tabs" ).tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true);
	});
	
	/* <![CDATA[ */
		$(function(){
			
			$('select#speedA').selectmenu();
			
			$('select#files, select#filesC').selectmenu({
				icons: [
					{find: '.script', icon: 'ui-icon-script'},
					{find: '.image', icon: 'ui-icon-image'}
				]
			});
			
			
		});
		
		//a custom format option callback
		var addressFormatting = function(text){
			var newText = text;
			//array of find replaces
			var findreps = [
				{find:/^([^\-]+) \- /g, rep: '<span class="ui-selectmenu-item-header">$1</span>'},
				{find:/([^\|><]+) \| /g, rep: '<span class="ui-selectmenu-item-content">$1</span>'},
				{find:/([^\|><\(\)]+) (\()/g, rep: '<span class="ui-selectmenu-item-content">$1</span>$2'},
				{find:/([^\|><\(\)]+)$/g, rep: '<span class="ui-selectmenu-item-content">$1</span>'},
				{find:/(\([^\|><]+\))$/g, rep: '<span class="ui-selectmenu-item-footer">$1</span>'}
			];
			
			for(var i in findreps){
				newText = newText.replace(findreps[i].find, findreps[i].rep);
			}
			return newText;
		}
		/* ]]> */
		
		 /* <![CDATA[ */
		    $(function() {
				$(".ps_album").css("position","absolute");
				$(".ps_album").css("float","none");
				/**
				* navR,navL are flags for controlling the albums navigation
				* first gives us the position of the album on the left
				* positions are the left positions for each of the 5 albums displayed at a time
				*/
                var navR,navL	= false;
				var first		= 1;
				var positions 	= {
					'0'		: 0,
					'1' 	: 215,
					'2' 	: 430
				}
				var $ps_albums 		= $('#ps_albums');
				/**
				* number of albums available
				*/
				var elems			= $ps_albums.children().length;
				var $ps_slider		= $('#ps_slider');
				
				/**
				* let's position all the albums on the right side of the window
				*/
				var hiddenRight 	= $(window).width() - $ps_albums.offset().left;
				$ps_albums.children('div').css('left',hiddenRight + 'px');
				
				/**
				* move the first 5 albums to the viewport
				*/
				$ps_albums.children('div:lt(3)').each(
					function(i){
						var $elem = $(this);
						$elem.animate({'left': positions[i] + 'px','opacity':1},800,function(){
							if(elems > 3)
								enableNavRight();
						});
					}
				);
				
				/**
				* next album
				*/
				$ps_slider.find('.next').bind('click',function(){
					if(!$ps_albums.children('div:nth-child('+parseInt(first+3)+')').length || !navR) return;
					disableNavRight();
					disableNavLeft();
					moveRight();
				});
				
				/**
				* we move the first album (the one on the left) to the left side of the window
				* the next 4 albums slide one position, and finally the next one in the list
				* slides in, to fill the space of the first one
				*/
				function moveRight () {
					var hiddenLeft 	= $ps_albums.offset().left + 215;
					
					var cnt = 0;
					$ps_albums.children('div:nth-child('+first+')').animate({'left': - hiddenLeft + 'px','opacity':0},500,function(){
							var $this = $(this);
							$ps_albums.children('div').slice(first,parseInt(first+2)).each(
								function(i){
									var $elem = $(this);
									$elem.animate({'left': positions[i] + 'px'},800,function(){
										++cnt;
										if(cnt == 2){
											$ps_albums.children('div:nth-child('+parseInt(first+3)+')').animate({'left': positions[cnt] + 'px','opacity':1},500,function(){
												//$this.hide();
												++first;
												if(parseInt(first + 2) < elems)
													enableNavRight();
												enableNavLeft();
											});
										}		
									});
								}
							);		
					});
				}
				
				/**
				* previous album
				*/
				$ps_slider.find('.prev').bind('click',function(){
					if(first==1  || !navL) return;
					disableNavRight();
					disableNavLeft();
					moveLeft();
				});
				
				/**
				* we move the last album (the one on the right) to the right side of the window
				* the previous 2 albums slide one position, and finally the previous one in the list
				* slides in, to fill the space of the last one
				*/
				function moveLeft () {
					var hiddenRight 	= $(window).width() - $ps_albums.offset().left;
				
					var cnt = 0;
					var last= first+2;
					$ps_albums.children('div:nth-child('+last+')').animate({'left': hiddenRight + 'px','opacity':0},500,function(){
							var $this = $(this);
							$ps_albums.children('div').slice(parseInt(last-3),parseInt(last-1)).each(
								function(i){
									var $elem = $(this);
									$elem.animate({'left': positions[i+1] + 'px'},800,function(){
										++cnt;
										if(cnt == 2){
											$ps_albums.children('div:nth-child('+parseInt(last-3)+')').animate({'left': positions[0] + 'px','opacity':1},500,function(){
												//$this.hide();
												--first;
												enableNavRight();
												if(first > 1)
													enableNavLeft();
											});
										}										
									});
								}
							);
					});
				}
				
				/**
				* disable or enable albums navigation
				*/
				function disableNavRight () {
					navR = false;
					$ps_slider.find('.next').addClass('disabled');
				}
				function disableNavLeft () {
					navL = false;
					$ps_slider.find('.prev').addClass('disabled');
				}
				function enableNavRight () {
					navR = true;
					$ps_slider.find('.next').removeClass('disabled');
				}
				function enableNavLeft () {
					navL = true;
					$ps_slider.find('.prev').removeClass('disabled');
				}		
				
				var $ps_container 	= $('#ps_container');
				var $ps_overlay 	= $('#ps_overlay');
				var $ps_close		= $('#ps_close');
				/**
				* when we click on an album,
				* we load with AJAX the list of pictures for that album.
				* we randomly rotate them except the last one, which is
				* the one the User sees first. We also resize and center each image.
				*/
				$ps_albums.children('div').bind('click',function(){
					var $elem = $(this);
					var mylink = $(this).find("a").attr("href");
					var album_name 	= 'album' + parseInt($elem.index() + 1);
					var $loading 	= $('<div />',{className:'loading'});
					$elem.append($loading);
					$ps_container.find('img').remove();
					$(location).attr('href',mylink);					
				});
				
				/**
				* when hovering each one of the images, 
				* we show the button to navigate through them
				*/
				$ps_container.live('mouseenter',function(){
					$('#ps_next_photo').show();
				}).live('mouseleave',function(){
					$('#ps_next_photo').hide();
				});
				
				/**
				* navigate through the images: 
				* the last one (the visible one) becomes the first one.
				* we also rotate 0 degrees the new visible picture 
				*/
				$('#ps_next_photo').bind('click',function(){
					var $current 	= $ps_container.find('img:last');
					var r			= Math.floor(Math.random()*41)-20;
					
					var currentPositions = {
						marginLeft	: $current.css('margin-left'),
						marginTop	: $current.css('margin-top')
					}
					var $new_current = $current.prev();
					
					$current.animate({
						'marginLeft':'250px',
						'marginTop':'-385px'
					},250,function(){
						$(this).insertBefore($ps_container.find('img:first'))
							   .css({
									'-moz-transform'	:'rotate('+r+'deg)',
									'-webkit-transform'	:'rotate('+r+'deg)',
									'transform'			:'rotate('+r+'deg)'
								})
							   .animate({
									'marginLeft':currentPositions.marginLeft,
									'marginTop'	:currentPositions.marginTop
									},250,function(){
										$new_current.css({
											'-moz-transform'	:'rotate(0deg)',
											'-webkit-transform'	:'rotate(0deg)',
											'transform'			:'rotate(0deg)'
										});
							   });
					});
				});
				
				/**
				* close the images view, and go back to albums
				*/
				$('#ps_close').bind('click',function(){
					$ps_container.hide();
					$ps_close.hide();
					$ps_overlay.fadeOut(400);
				});
				
				/**
				* resize and center the images
				*/
				function resizeCenterImage($image){
					var theImage 	= new Image();
					theImage.src 	= $image.attr("src");
					var imgwidth 	= theImage.width;
					var imgheight 	= theImage.height;
					
					var containerwidth  = 460;
					var containerheight = 330;
					
					if(imgwidth	> containerwidth){
						var newwidth = containerwidth;
						var ratio = imgwidth / containerwidth;
						var newheight = imgheight / ratio;
						if(newheight > containerheight){
							var newnewheight = containerheight;
							var newratio = newheight/containerheight;
							var newnewwidth =newwidth/newratio;
							theImage.width = newnewwidth;
							theImage.height= newnewheight;
						}
						else{
							theImage.width = newwidth;
							theImage.height= newheight;
						}
					}
					else if(imgheight > containerheight){
						var newheight = containerheight;
						var ratio = imgheight / containerheight;
						var newwidth = imgwidth / ratio;
						if(newwidth > containerwidth){
							var newnewwidth = containerwidth;
							var newratio = newwidth/containerwidth;
							var newnewheight =newheight/newratio;
							theImage.height = newnewheight;
							theImage.width= newnewwidth;
						}
						else{
							theImage.width = newwidth;
							theImage.height= newheight;
						}
					}
					$image.css({
						'width'			:theImage.width,
						'height'		:theImage.height,
						'margin-top'	:-(theImage.height/2)-10+'px',
						'margin-left'	:-(theImage.width/2)-10+'px'	
					});
				}
				
				$('.btn-next').bind('click',function(){
					$(".next").click();
				});
				$('.btn-prev').bind('click',function(){
					$(".prev").click();
				});
            });
			/* ]]> */
			
	
/*
$(function(){
$(document).ready(function(){
    $('#tweetFeed').jTweetsAnywhere({
        username: 'ypkerala',
        count: 1,
        showTweetBox: { label: '<span style="color: #D1C7BA">Spread the word ...</span>' }
    });
})
*/
		
		$(".social li").hover(
			function(){
				$(this).animate({marginLeft:"7px"},200) 
			}, function(){
				$(this).animate({marginLeft:"0"},200) 
			}
			
		);
		$(".useful-link li").hover(
			function(){
				$(this).animate({marginLeft:"7px"},200) 
			}, function(){
				$(this).animate({marginLeft:"0"},200) 
			}
			
		);
		 $("#notify_email").focus(function () {
			
        if($(this).val() == "Email Address"){
        	$(this).val("");
        	}
    });
		 $("#notify_email").blur(function () {
			if($(this).val() == ""){
        $(this).val("Email Address");
        }
    });    
    
   
    
	//	});
		
	 function ajax_email_subscription(){
		 		var siteroot = $("#siteroot").attr("value");
    			var email = $("#notify_email").val();
    		if(email !="" && email !="Email Address"){
    	 $.post(siteroot+"php/ajax_email_subscription.php", { email: email },
   		function(data){
     			//alert(data);
     			$("#emaildiv").html(data);
     			//$("#notify_email").val("");
   		});
   		}
   		
   		return false;
    	}
		  
		
		
		    jQuery(document).ready(function(){
               
                jQuery("#notifyform").validationEngine('attach', {promptPosition : "topLeft"});
            });
            
  $(function() {		
		$( "#tabs-sidebar" ).tabs({fx:{opacity: "toggle"}}).tabs("rotate", 4500, true);
		$( "#tabs-sidebar-b" ).tabs({fx:{opacity: "toggle"}}).tabs("rotate", 4600, true);
		$( "#tabs-sidebar-c" ).tabs({fx:{opacity: "toggle"}}).tabs("rotate", 5000, true);
	});
        
    function auto_suggestions_what(){
	  var siteroot = $("#siteroot").attr("value");
            	 var search_val = $("#what").val(); 
            	 if(search_val.length > 1){
                $.post(siteroot+"php/autocomplete_what.php", {
                	  fld:'what', 
                    what: search_val
                }, function (data) { 
                		if(data.length >1){
                    $("#autocomplete_what").show();
                    $("#autocomplete_what").html(data);
						}
                });
				}else{
					  $("#autocomplete_what").hide();
					}
			
          }
          
          function run_suggestion_what(type,key){
          	
          	  var siteroot = $("#siteroot").attr("value");
					$("#what").val(key);
					$("#autocomplete_what").hide();	
					
					if(type != "category"){
						$.post(siteroot+"php/autocomplete_what.php", {
							fld:type, 
                    what: key
                }, function (data) { 
                    
                    $("#where").val(data);
						$("#searchform").submit();
                });
					}else{
						$("#where").focus();
					}										
				}
          
		function auto_suggestions_where(){
	  var siteroot = $("#siteroot").attr("value");
            	 var search_val = $("#where").val();
            	 if(search_val.length > 1){
                $.post(siteroot+"php/autocomplete_where.php", {
                	  fld : 'where', 
                    where: search_val
                }, function (data) {
                	if(data.length >1){
                    $("#autocomplete_where").show();
                    $("#autocomplete_where").html(data);
						}
                });
                }else{
                	$("#autocomplete_where").hide();
                	
                	}
          }         
          
      
	 	function run_suggestion_where(type, key){
					$("#where").val(key);
					$("#autocomplete_where").hide();
					 var search_val = $("#what").val();
            	 if(search_val.length > 1){
					$("#searchform").submit();	
					}	
							
				}	
  
		 $("html").click(function (e)
{
if (e.target != document.getElementById("autocomplete_what") && e.target != document.getElementById("autocomplete_where") ){
    $("#autocomplete_what").hide();
	 $("#autocomplete_where").hide();
	 }
}); 
