/** RZ Changes **/

	$(document).ready(function(){

		// implement curvy corners for ie
		
		

	
	   var options = { 
	        target:        '',   // target element(s) to be updated with server response 
	        beforeSubmit:  function(){
					jQuery('#login-form').slideUp();
					jQuery('#login-form').after('<img id="loader-Image" src="/images/loader.gif"/>');
	        	},  // pre-submit callback 
	        success:       function(){
					var username = $('#lusername').val();
					
					$('h4#login-username').html(username);
					$('#loader-Image').fadeOut();
					$('#login-output').fadeIn('slow');
					
	        	}  // post-submit callback 
	 
	        // other available options: 
	        //url:       url         // override for form's 'action' attribute 
	        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
	        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
	        //clearForm: true        // clear all form fields after successful submit 
	        //resetForm: true        // reset the form after successful submit 
	 
	        // $.ajax options can be used here too, for example: 
	        //timeout:   3000 
	    }; 
	 
	    // bind form using 'ajaxForm' 
	    jQuery('#login-form').ajaxForm(options); 
		
	    
	    
	    
	    //login box
	    $(".signin").click(function(e) {
                e.preventDefault();
                $("fieldset#signin_menu").toggle();
                $(".signin").toggleClass("menu-open");
            });

            $("fieldset#signin_menu").mouseup(function() {
                return false
            });
            $(document).mouseup(function(e) {
                if($(e.target).parent("a.signin").length==0) {
                    $(".signin").removeClass("menu-open");
                    $("fieldset#signin_menu").hide();
                }
            });     
	    	
            
    /**** POST SHARE FUNCTIONALITY ******/
	
	//add event handler when hover over the post
	/*
$('.post').hover(function(){
		
		//get the values we need
		//var sharePart = $(this).find('.share-part');
    	//var sharePart = $('.share-part', this);
    	
		
    	// ... and of course, we display our post share bit
    	$('.share-part', this).fadeIn(50);
    	
	}, function(){
		
	
		
		$('.share-part').fadeOut(50);
    	
	});
*/
	
/**
$('#featured-tabs .post').hover(function(){
	
		var $this = $(this);
		
		
		$('#featured-tabs .post').stop().animate({height : '0px'});
		
		$(this).animate({height: '60px'});
		
		
	}, function(){

		$(this).animate({height: '0px'}, 100);
		
	});
**/

var cfg = ($.hoverintent = {
		sensitivity: 7,
		interval: 100
	});
		
	$.event.special.hoverintent = {
		setup: function() {
			$(this).bind("mouseover", jQuery.event.special.hoverintent.handler);
		},
		teardown: function() {
			$(this).unbind("mouseover", jQuery.event.special.hoverintent.handler);
		},
		handler: function(event) {
			event.type = "hoverintent";
			var self = this,
				args = arguments,
				target = $(event.target),
				cX, cY, pX, pY;
				
			function track(event) {
				cX = event.pageX;
				cY = event.pageY;
			};
			pX = event.pageX;
			pY = event.pageY;
			function clear() {
				target.unbind("mousemove", track).unbind("mouseout", arguments.callee);
				clearTimeout(timeout);
			}
			function handler() {
				if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
					clear();
					jQuery.event.handle.apply(self, args);
				} else {
					pX = cX; pY = cY;
					timeout = setTimeout(handler, cfg.interval);
				}
			}
			var timeout = setTimeout(handler, cfg.interval);
			target.mousemove(track).mouseout(clear);
			return true;
		}
	};

		
	$(function() {
		$("#accordion").accordion({
			event: "click hoverintent"
		});
	});


	
	
	
	
	
	/***** VALIDATION ******/
	
	

	
	
	
	/***************** VALIDATION [END] ****
	****************************************/
	
	
	/** bottom search enlarge **/
	
	$('#town-road #on-the-road #footer-search input').focus(function(){
		var thisWidth = $(this).width();
		
		$(this).animate({width: '270px'});
	});
	
	$('#town-road #on-the-road #footer-search input').blur(function(){
		$(this).animate({width: '179px'});		
	});
	
	
	
	///////////////////////////////////////////////////////////////
	//////////////// SUBMISSION (video, stories, pics)
	///////////////////////////////////////////////////////////////
	
	$('.submission-buttons li').click(function(){
		$('#stories-submission-container').fadeOut('fast');
		$('#video-submission-container').fadeOut('fast');
		$('#picture-submission-container').fadeOut('fast');
	});
	
	$('li#submit-story-handler').click(function(){
		$('#stories-submission-container').slideToggle('slow');
	});
	
	$('li#submit-video-handler').click(function(){
		$('#video-submission-container').slideToggle('slow');
	});
	
	$('li#submit-pictures-handler').click(function(){
		$('#picture-submission-container').slideToggle('slow');
	});
	
	
	
	
	//////////////////////// SUBMIT VIDEOS
	
	 var video_options = { 
	        target:        '#submission-output',   // target element(s) to be updated with server response 
	        beforeSubmit:  function(){
					jQuery('#submission-output').after('<img id="loader-Image" src="/images/loader.gif"/>');
	        	},  // pre-submit callback 
	        success:       function(){
	        		$('#submission-output').fadeIn('slow');
	        		$('#loader-Image').fadeOut();
	        	}  // post-submit callback 
	    }; 
	 

	$('#video-submission-container form').ajaxForm(video_options); 
	
    $('input[type=submit]').button(); 
    $('.close-submission-form').button();
    
    $('.close-submission-form').click(function(){
        $('#submit').effect('puff', 1000); 
    });
    
    
    /** FETCH VIDEO INFORMATION **/
    /**
    
    Due tue security reasons you cannot make a crossdomain ajax request (getJSON = ajax).
    So in order to get some ajaxy going on you need to proxy your call through the server
    like jQuery(Ajax) => PHP Script(HTTP Request) => Responce + back to interface.
    
    So it will be done later.
      
    $('#video_url').keyup(function() {
	  
    	var video_url = $(this).attr('value');
    	
    	var api_url = "http://api.embed.ly/v1/api/oembed?url="+escape(video_url)+"&format=json";
    	
    	$.getJSON(api_url, function(data) {
		  	$('.form-list').append(data);
		});

	});
	
	*/
    
     
            
	/** TABS **/
	
	jQuery('#tabs').tabs({fx:{opacity: "toggle"}}).tabs("rotate", 6000, false);
	jQuery('#tabs2').tabs({fx:{opacity: "toggle"},event: "mouseover"})
	jQuery('#featured-tabs').tabs({fx:{opacity: "toggle"}}).tabs("rotate", 6000, false);
	
	
	/** 
	**	Implementing the like/dislike feature
	**/
	
	$('.ajaxLike').click(function(){
		
		
		var options = {
			id : $(this).attr('rel'),
			action : $(this).attr('id'),
			what : $(this).attr('type')
		}
		
				
		//alert('about to ' + action + ' the id ' + id + '. Its a ' + what);
		
		$.ajax({
		   type: "POST",
		   url: "/like.php",
		   data: "ID="+ options.id +"&what="+ options.what +"&action="+ options.action,
		   success: function(){
		   		if(options.action === 'like'){
		   			
		   			var currentNr = $('#likedNr').html();
		   			currentNr++
		   			
		   			$('#likedNr').html('<strong>'+currentNr+'</strong>');
		   			
		   		}else{
		   			
		   			var currentNr = $('#dislikedNr').html();
		   			currentNr--
		   			
		   			$('#dislikedNr').html('<strong>'+currentNr+'</strong>');
		   			
		   		}
		   		
		   		$('.ajaxLike').fadeOut();

		   }
		 });
		
		
		
		
		return false;
		
	});
	
	
	
	/*** RELATED VIDEOS SLIDER ***/
	$(".related-videos").jCarouselLite({
			auto: 6000,
            speed: 1000,
            visible:7,
            circular: true,
		 	btnNext: "#item-right",
        	btnPrev: "#item-left"

    });
    
    
    
    /** DESCRIPTION **/

	
	
	$('.video-tools').hover(function(){
		
		$(this).css('height', 'auto');
		$('div#description', this).css('color', '#000');
		
	}, function(){
		
		$(this).css('height', '35px');
		$('div#description', this).css('color', '#ACACAC');
		
	});
	
	
	
	/** Make the video embed element teh width of the container **/
	$('.post object embed').attr('width', '645');
	$('#featured-video object embed').attr('width', '510px');
	$('#featured-video object embed').attr('height', '300px');
	
	
	
	/** TIPSIS **/
	
	$('.related-videos ul li').tipsy({gravity: 'n', fade: true});
	$('.tip').tipsy({gravity: 'n', fade: true});
	$('.tip-s').tipsy({gravity: 's', fade: true});
	
	//$('#search-input').defaultValue('Search youFML');
	
	        
	/** SHARE THINGY **/
	
	if($('#social-bookmarking').length){
	
	//var socialHtml = $('#social-bookmarking').html();
	//$('#sharedowner ul').html(socialHtml);
	
	//$('#social-bookmarking').clone().appendTo('#sharedowner');
	$(window).scroll(function(){
			
			sharer = $('#sharedowner');
			
			
			if($(window).scrollTop() > 500){
				
				$('#social-bookmarking').fadeOut();
				sharer.slideDown('slow');
				
				
				
			}else if($(window).scrollTop() < 500){
				
				$('#social-bookmarking').fadeIn();
				sharer.slideUp('slow');
			
			}
		
	        //if  ($(window).scrollTop() == $(document).height() - $(window).height()){
	        //   lastPostFunc();
	        //}
		});
    }
            
    /*** DIALOGS ***/
               
            $('#signup_link').click(function(){
                
                
                      
                
                
                $('#signupform').dialog({
                    title: 'SignUp for YouFML',
                    modal: true,
                    width: 730,
                    resizable: false,
                    buttons: {
                        'Create account':  
                                function(){
                                    
                                    var options = {
                                        username: $('input#username').val(),
                                        password: $('input#password').val(),
                                        confirmpassword: $('input#confirmpassword').val(),
                                        email: $('#email').val(),
                                        gender: $('#gender').val(),
                                        captcha: $('#captcha').val()
                                    }
                                    
                                    var dataValues = "username="+options.username
                                                 +"&password="+options.password
                                                 +"&confirmpassword="+options.confirmpassword
                                                 +"&email="+options.email
                                                 +"&captcha="+options.captcha
                                                 +"&gender="+options.gender
                                                 +"&register=1";
                                                 
                                    //alert(dataValues);

                                    
                                           $.ajax({
                                           type: "POST",
                                           url: "/ajax_signup_check.php",
                                           data: dataValues,
                                           success: function(data){
                                                 $('.registration-message').html(data);
                                                 if(data = "G'day mate, your registration was succesful."){
                                                   $('#form').fadeOut();  
                                                 }
                                                                            
                                           }
                                         }); 
                                }
                        ,
                        'Cancel': function() {
                                $(this).dialog('close');
                            }
                    }
                });
                
                return false;
            });
            
            
     
    //To switch directions up/down and left/right just place a "-" in front of the top/left attribute
    //Vertical Sliding
    $('.boxgrid.slidedown').hover(function(){
        $(".cover", this).stop().animate({top:'-160px'},{queue:false,duration:300});
    }, function() {
        $(".cover", this).stop().animate({top:'0px'},{queue:false,duration:300});
    });
    //Horizontal Sliding
    $('.boxgrid.slideright').hover(function(){
        $(".cover", this).stop().animate({left:'325px'},{queue:false,duration:300});
    }, function() {
        $(".cover", this).stop().animate({left:'0px'},{queue:false,duration:300});
    });
    //Diagnal Sliding
    $('.boxgrid.thecombo').hover(function(){
        $(".cover", this).stop().animate({top:'260px', left:'325px'},{queue:false,duration:300});
    }, function() {
        $(".cover", this).stop().animate({top:'0px', left:'0px'},{queue:false,duration:300});
    });
    //Partial Sliding (Only show some of background)
    $('.boxgrid.peek').hover(function(){
        $(".cover", this).stop().animate({top:'90px'},{queue:false,duration:160});
    }, function() {
        $(".cover", this).stop().animate({top:'0px'},{queue:false,duration:160});
    });
    //Full Caption Sliding (Hidden to Visible)
    $('.boxgrid.captionfull').hover(function(){
        $(".cover", this).stop().animate({top:'26px'},{queue:false,duration:160});
    }, function() {
        $(".cover", this).stop().animate({top:'103px'},{queue:false,duration:160});
    });
    //Caption Sliding (Partially Hidden to Visible)
    $('.boxgrid.caption').hover(function(){
        $(".cover", this).stop().animate({top:'160px'},{queue:false,duration:160});
    }, function() {
        $(".cover", this).stop().animate({top:'220px'},{queue:false,duration:160});
    });

            


		/**
		*	Featured slider
		*/
		
		$('#featured-controlers a').click(function(){
			var clickedItem = $(this).attr('href');
			$('div#featured-content div.featured-content-item').fadeOut(300, function(){
				$(clickedItem + '-content').fadeIn('slow');
			});
		}); 
		
		// autoslider
		
		var loopFeatures = function(){
			
			var featuredContent = $('#featured-content');
			var featuredItems = $('.featured-content-item');
			
			
		};
		
		//setInterval(loopFeatures, 1000);
		
		
		
		// URL REPORTER
		
		$('#report-item').click(function(){
			
			var type = $(this).attr('type');
			var id = $(this).attr('item-id');
			
			$.ajax({
				url : "/report.php",
				data : "id="+id+"&type="+type,
				success : function(){
					
					$('#report-responce').html('<span style=" float: right; color: #4C6B19; font-weight: bold">Thanks!</span>');
					
				}
			});
			
			return false;
			
		});
		
	});
	
	    function submitToggle() {			
		if ($('#submit').css('display') == 'none') {
			$('#submit').slideDown();	
		} else {
			$('#submit').slideUp();
			}
		}
	
		function plusToggle(id) {
			if ($('#plus'+id).css('display') == 'none') {
				$('#plus'+id).slideDown();
			} else {
				$('#plus'+id).slideUp();
			}
		}	
	

 

