jQuery(document).ready(function($) {

    if(window.location.hash.substr(1).length>1){

        var toLoad = siteurl + '/' + window.location.hash.substr(1) + ' #ajax-load';

        $('#content').hide();

            $('#content').load(toLoad, function() {

                $('#content').fadeIn();

            });

    }


    $('.goto-upload').click( function(){


        if($('#title').val()=='' || $('#contact_person').val()=='' || $('#contact_phone').val()=='' || $('#contact_email').val()=='' || $('#description').val()==''){

            alert('Palun täida kõik tärniga märgitud väljad!');

        }else{

            var postData = 'action=save_upload';
            var link = $(this).attr('href');

            $('#content input, #content textarea').each(function(){
                if($(this).attr('name')=='subscribe'){

                  var checked = $('#content input[type=checkbox]:checked').val() != undefined;

                    if(checked){
                        postData = postData + '&' + $(this).attr('name') + '=1';
                    }else{
                        postData = postData + '&' + $(this).attr('name') + '=0';
                    }
                }else{

                    postData = postData + '&' + $(this).attr('name') + '=' + $(this).val();

                }
                //alert(postData);
            });

            $.ajax({
        		url:  link,
        		type: "POST",
        		data: postData,
        		success: function (response) {

                    if(response=='false'){
                        alert('Palun täida kõik tärniga märgitud väljad!');
        		    }else{
                        $('#content').fadeOut(function(){
                            $(this).html(response).fadeIn()
                        });
                    }
        		}
        	});
        }
        return false;
    });

    $('.upload-youtube').live('click', function(){

        if($('#realupload').val()!=''){

            $('#ajax-loader').fadeIn();
            $('#youtube-form').submit();
            //alert('submit');

        }else if($('#youtube_link').val()!=''){

            var link = $(this).attr('href');

            $.ajax({
        		url:  link,
        		type: "POST",
        		data: 'action=save_link&clip_id=' + $('#saved_clip_id').val() + '&link=' + $('#youtube_link').val(),
        		success: function (response) {

                    if(response=='false'){
                        alert('Salvestamisel tekkis viga');
        		    }else{
                        $('#content').fadeOut(function(){
                            $(this).html(response).fadeIn()
                        });
                    }
        		}
        	});

        }else{
            alert('Palun täida vähemalt üks väljadest.');
        }


        return false;

    });

    $('.ajax-load').live('click', function(){

        var link = $(this).attr('href');
        //alert($(this).attr('rel'));
        if($(this).attr('rel')!=''){
            window.location.hash = $(this).attr('rel');
        }else{
            window.location.hash = '';
        }
        $('#content iframe').css('visibility', 'hidden');
        $('#content').fadeOut(function(){

            $('#content').load(link + ' #ajax-load', function(){

                $('#content').fadeIn();
                FB.XFBML.parse();

            });
        });

        return false;

    });

    $('.checkbox input[type=checkbox]').prettyCheckboxes({
		checkboxWidth: 15, // The width of your custom checkbox
		checkboxHeight: 14 // The height of your custom checkbox)
	})

	$('.like_button iframe').attr('allowTransparency', true);

	$('.button-add').live('click', function() {
		var input = $(this).prev();
		$(this).before(input.clone().val(''));
	});


});
