$(document).ready(function()
{
	//comments
	$("#comment-field").jqEasyCounter({
    'maxChars': 250
	});
	//reviews
	$("#field_id_31").jqEasyCounter({
    'maxChars': 250
	});
	   /* Jquery hot pockets */
	   $(".pocket-backing").click(function()
       {
       	  var state = $(this).hasClass("hot-pocket")
          if(state)
          {
          	$(this).removeClass("hot-pocket");
          	$(this).next().stop().slideUp();
          	 $(this).children().removeClass("hot");
          }
          else
          {
          	 $(this).addClass("hot-pocket");
          	 $(this).children().addClass("hot");
          	  $(this).next().stop().slideDown();
          }
          return false;
        });
	//$( "#entry_date" ).datepicker({ dateFormat: 'yy-mm-dd' });
 	/** Accordion **/
    lastBlock = $("#a1");
    maxWidth = 500;
    minWidth = 111;
    $("#accordion ul li a").hover(
      function(){
        $(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:400 });
	$(this).animate({width: maxWidth+"px"}, { queue:false, duration:400});
	lastBlock = this;
      }
    );
    /** EO Accordion **/
	/** Edit Profile **/
	$("#countries").change(function ()
	{
		//jAlert('This is a custom alert box', 'Alert Dialog');
          var str = "";
          $("#countries option:selected").each(function ()
          {
                countryval = $(this).val();
          });
          $.ajax({
				type	: "POST",
				cache	: false,
				async	: false,
				url		: "/my-profile/show_cities",
				data	: ({code : countryval, selclass : "city-personal"}),
				error	: function()
				{
					alert("error");
					return false;
			},
			success: function(data)
			{
				$("#city-holder").html(data);
				$("#user_country").val(countryval)
			}
		});
     });
     $("#gig_countries").change(function ()
	{
          var str = "";
          $("#gig_countries option:selected").each(function ()
          {
                countryval = $(this).val();
          });
          $.ajax({
				type	: "POST",
				cache	: false,
				async	: false,
				url		: "/my-profile/show_cities",
				data	: ({code : countryval, selclass : "city-photo"}),
				error	: function()
				{
					alert("error");
					return false;
			},
			success: function(data)
			{
				$("#city-holder-gigs").html(data);
				$("#photo_country").val(countryval)
			}
		});
     });
    $("#video_countries").change(function ()
	{
          var str = "";
          $("#video_countries option:selected").each(function ()
          {
                countryval = $(this).val();
          });
          $.ajax({
				type	: "POST",
				cache	: false,
				async	: false,
				url		: "/my-profile/show_cities",
				data	: ({code : countryval, selclass : "city-video"}),
				error	: function()
				{
					alert("error");
					return false;
			},
			success: function(data)
			{
				$("#city-holder-video").html(data);
				$("#video_country").val(countryval)
			}
		});
     });
    $("#city-personal").live('change', function()
	{
        $("#city-personal option:selected").each(function ()
        {
            $("#user_city").val($(this).val());
        });
    });
    $("#city-photo").live('change', function()
	{
        $("#city-photo option:selected").each(function ()
        {
            $("#photo_city").val($(this).val());
        });
    });
    $("#city-video").live('change', function()
	{
        $("#city-video option:selected").each(function ()
        {
            $("#video_city").val($(this).val());
        });
    });
	/** EO Edit Profile **/
 		//$('*[title]').monnaTip();
		/** LOGIN **/
        //slidey form
        $("a#showLogin").toggle(function() {
            $("#login-form").slideDown("slow");
            return false;
        }, function() {
            $("#login-form").slideUp("slow");
            return false;
        });
         $("a#comments-login").toggle(function() {
            $("#login-form").slideDown("slow");
            return false;
        }, function() {
            $("#login-form").slideUp("slow");
            return false;
        });
          $("a#favourites-trigger").toggle(function()
          {
            $("#all-media").slideDown("slow");
            return false;
        }, function() {
            $("#all-media").slideUp("slow");
            return false;
        });
         $("a#comments-trigger").toggle(function()
          {
            $("#profile-comments").slideDown("slow");
            return false;
        }, function() {
            $("#profile-comments").slideUp("slow");
            return false;
        });
         $("a#gigs-trigger").toggle(function()
          {
            $("#profile-gigs").slideDown("slow");
            return false;
        }, function() {
            $("#profile-gigs").slideUp("slow");
            return false;
        });
        //close login form
        $("a#close-login").click(function(e)
        {
            $("#login-form").slideUp("slow");
          	return false;
        });
        /** GOOGLE MAPS **/
        //slidey map
        $("a#showMap").toggle(function()
        {
            $("#google-map").slideDown("slow");
            return false;
        }, function() {
            $("#google-map").slideUp("slow");
            return false;
        });
        /**  GIGGAGE **/
        //add attendance form
        $("#add_attendance").live("submit",function(event)
      	{
      		var gigid = $(this).find('#gig_id').val();
      		var gigurl = $(this).find('#gig_url').val();
			$.ajax({
				type	: "POST",
				cache	: false,
				async	: false,
				beforeSend: showRequest,
				url		: "/my-profile/add_attendance",
				data	: $(this).serializeArray(),
				error	: function(data){
				alert("error " + data);
				return false;
			},
			success: function(data)
			{
				$("#attending-data").load('/gigs/attending/'+gigurl, {gig_id : gigid}, function()
				{
				});
			}
		});
		return false;
		});
		function showRequest(formData, jqForm, options)
		{
			$("#attending").html("loading...");
	    }
     	/** MEDIA GALLERY **/
     	/*
        $('#all-media').masonry({
  			columnWidth: 150,
  			itemSelector: '.media'
		});
		$(".fancybox").fancybox(
		{
			'autoScale'     	: true,
			'overlayOpacity':	0.8,
			'overlayColor'	:	'#000',
			'overlayShow'	:	true
		});
		$(".lyrics").fancybox(
		{
			'autoScale'     	: true,
			'overlayOpacity':	0.8,
			'overlayColor'	:	'#000',
			'type'				: 'iframe',
			'overlayShow'	:	true
		});
		$(".video-fancybox").fancybox({
     		   'autoScale'     	: true,
				'type'				: 'iframe',
				'overlayOpacity':	0.8,
				'overlayColor'	:	'#000',
				'overlayShow'	:	true
		});
		$("a#map-popup").fancybox({
			'overlayOpacity':	0.8,
			'overlayColor'	:	'#000',
			'overlayShow'	:	true,
			'centerOnScroll':	false,
			'autoScale'		: 	false,
			'overlayShow'	: 	true,
			'titleShow'		: 	false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'callBackOnShow': testFancy, // This not working! It call function when page is loaded
			'padding'		:	3
		});
		*/
	var options = {
       // target:        '#output1',   // target element(s) to be updated with server response
        beforeSubmit:  showRequest,  // pre-submit callback
        success:       showResponse  // 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
    };
    var reviewoptions = {
        beforeSubmit:  checkReview,  // pre-submit callback
        success:       showResponse  // post-submit callback
        //clearForm: true        // clear all form fields after successful submit
    };
       var videooptions = {
        beforeSubmit:  checkVideo,  // pre-submit callback
        success:       showResponse  // post-submit callback
        //clearForm: true        // clear all form fields after successful submit
    };
    // bind form using 'ajaxForm'
    $('#imagePublishForm').ajaxForm(options);
    $('#videoPublishForm').ajaxForm(videooptions);
    $('#reviewForm').ajaxForm(reviewoptions);
    $('.delete-link').click(function()
    {
    	var page = $(this).attr("href");
    	//jAlert('This is a custom alert box', 'Alert Dialog');
  		$.ajax({
  				type    : "POST",
				cache	: false,
				async	: false,
				url		: page,
				error	: function()
				{
				alert("error");
				return false;
			},
			success: function(data)
			{
				alert(data);
			}
		});
		return false;
	});
});
function fillImageForm(path, thumb_path)
{
	$("#image-submit").slideDown("slow");
	$("#field_id_22").val(path);
	$("#field_id_23").val(thumb_path);
}
function fillVideoForm(youtube_path)
{
    var index = youtube_path.indexOf("=");
    var youtube_code = youtube_path.substr(index+1,youtube_path.length)
    $("#field_id_49").val(youtube_code);
}
//End of document ready
function removeGig(form)
{
    var gigid = $(form).find('#gig_id').val();
    var gigurl = $(form).find('#gig_url').val();
	$.ajax({
				type	: "POST",
				cache	: false,
				async	: false,
				url		: "/my-profile/remove_attendance",
				data	: $(form).serializeArray(),
				error	: function(){
				alert("error");
				return false;
			},
			success: function(data)
			{
				$("#attending-data").load('/gigs/attending/'+gigurl, {gig_id : gigid}, function()
				{
				});
			}
		});
}
function removeMessage(form)
{
    var gigid = $(form).find('#gig_id').val();
	$.ajax({
				type	: "POST",
				cache	: false,
				async	: false,
				url		: "/my-profile/remove_message",
				data	: $(form).serializeArray(),
				error	: function(){
				alert("error");
				return false;
			},
			success: function(data)
			{
			alert(data);
				$(form).parent().parent().slideUp();
			}
		});
}
function add_favourite(form)
{
  			$.ajax({
				type	: "POST",
				cache	: false,
				async	: false,
				url		: "/my-profile/add_favourite",
				data	: $(form).serializeArray(),
				error	: function()
				{
					alert("error");
					return false;
			},
			success: function(data)
			{
			 		jAlert('Added to favourites.', 'Favourites');
			}
		});
		return false;
}
function remove_favourite(form)
{
	var theform = form;
	jConfirm('Are you sure?', 'Remove Favourite', function(r, form)
	{
   		if(r)
   		{
   			$.ajax({
				type	: "POST",
				cache	: false,
				async	: false,
				url		: "/my-profile/remove_favourite",
				data	: $(theform).serializeArray(),
				error	: function()
				{
					alert("error");
					return false;
			},
			success: function(data)
			{
			 	$(theform).parent().hide("slow");
			 	jAlert('Favourite was removed.', 'Remove Favourite');
			}
			});
   		}
   		else
   		{
   		}
	});
}
function removeFav(r)
{
    jAlert('Confirmed: ' + r, 'Confirmation Results');
}
function testFancy()
{
	alert("callback for fancybox");
}
function getGigDetails(newurl)
{
		$('#map-popup').attr('href', newurl);
		$('#map-popup').trigger('click');
}
// pre-submit callback
function showRequest(formData, jqForm, options)
{
    var queryString = $.param(formData);
	 alert('About to submit: \n\n' + queryString);
    return true;
}
function checkReview(formData, jqForm, options)
{
	var title = $(formData[19]).val();
	var review = $(formData[20]).val();
	var message = "Please correct the following errors: \n\n";
	var error = "false";
	var errorText = "";
	if(!title)
	{
		errorText += "Title must not be blank \n";
		error = "true";
	}
	if(!review)
	{
		errorText += "Review must not be blank \n";
		error = "true";
	}
	if(error == "true")
	{
		jAlert(message + errorText, 'Review submit');
	}
     return false;
}
function checkVideo(formData, jqForm, options)
{
	var youtubelink = $(formData[14]).val();
	//fillVideoForm($('#youtubecode').val());
    fillVideoForm(youtubelink);
    //return false;
}
// post-submit callback
function showResponse(responseText, statusText, xhr, $form)
{
	jAlert(responseText, 'Alert Dialog');
} 


