$(document).ready(function(){

	if($('.frame').length){
		$('.frame').each(function(){
                    if ($(this).find('img').length)
                        $(this).append('<span class="corner1"></span><span class="corner2"></span><span class="corner3"></span><span class="corner4"></span><span class="shadow1"></span><span class="shadow2"></span><span class="shadow3"></span><span class="shadow4"></span>');
		});
		function frames(){
		$('.frame').each(function(){
			var frame_node = $(this);
			var image_node = $(this).find('img');

                        if (image_node.length) {
                            var image_width = image_node.attr('width');
                            var image_height = image_node.attr('height');
                            var image_url = image_node.attr('src');
                            if(image_width != 0 && image_height != 0){
                            if(image_width > image_height){
                                    if(image_width > 900){image_width = 900;}
                                    image_node.css({'width':image_width});
                                    image_width = image_node.width();
                                    image_height = image_node.height();
                                    if(image_height == 0){image_height = image_node.attr('height');}
                            }else{
                                    if(image_height > 600){image_height = 600;}
                                    image_node.css({'height':image_height});
                                    image_width = image_node.width();
                                    image_height = image_node.height();
                            }
                            if(frame_node.find('.opener-closer').length){
                                    frame_node.css({'width':image_width+16,'height':image_height+58,'overflow':'hidden'});
                                    width_val = frame_node.width()-14;
                                    height_val = frame_node.height()-57;
                                    frame_node.find('.shadow1').css({'width':width_val});
                                    frame_node.find('.shadow3').css({'width':width_val,'bottom':'42px'});
                                    frame_node.find('.shadow2').css({'height':height_val});
                                    frame_node.find('.shadow4').css({'height':height_val,'bottom':'53px'});
                                    frame_node.find('.corner4,.corner3').css({'bottom':'42px'});
                            }else{
                                    frame_node.css({'width':image_width+16,'height':image_height+16,'overflow':'hidden'});
                                    width_val = frame_node.width()-14;
                                    height_val = frame_node.height()-14;
                                    frame_node.find('.shadow1').css({'width':width_val});
                                    frame_node.find('.shadow3').css({'width':width_val});
                                    frame_node.find('.shadow2').css({'height':height_val});
                                    frame_node.find('.shadow4').css({'height':height_val});
                            }
                            }
                        } else {
                            
                        }



		});
		}
		frames();
	}

	/*if($('.home-foto').length){
		$('.home-foto').each(function(){
			if($(this).find('img').length){
				var width_val = parseInt($(this).find('img').width()) + 16;
				$(this).css({'width':width_val});
				var height_val = parseInt($(this).find('img').height()) + 16;
				$(this).css({'height':height_val});
				width_val = width_val-14;
				$(this).find('.shadow1').css({'width':width_val});
				$(this).find('.shadow3').css({'width':width_val});
				height_val = height_val-14;
				$(this).find('.shadow2').css({'height':height_val});
				$(this).find('.shadow4').css({'height':height_val});
			}
			if(!$(this).find('img').length){
				$(this).find('.shadow1,.shadow2,.shadow3,.shadow4,.corner1,.corner2,.corner3,.corner4').css({'display':'none'});
			}
		});
	}

	if($('.post .foto-bg').length){
		$('.post .foto-bg img').each(function(){
			if($(this).width()>900){
				var height_vall = $(this).height();
				var src_val = $(this).attr('src');
				$(this).css({'width':'900px','height':height_vall});
				$(this).attr({'src':'/wp-content/themes/fer/i/0.gif'});
				$(this).css({'background':'url("' + src_val + '") center center no-repeat'});
			}
				var width_val = parseInt($(this).width()) + 16;
				$(this).parent().parent().css({'width':width_val});
				var height_val = parseInt($(this).height()) + 16;
				$(this).parent().parent().css({'height':height_val});
				width_val = width_val-14;
				$(this).parent().parent().find('.shadow1').css({'width':width_val});
				$(this).parent().parent().find('.shadow3').css({'width':width_val});
				height_val = height_val-14;
				$(this).parent().parent().find('.shadow2').css({'height':height_val});
				$(this).parent().parent().find('.shadow4').css({'height':height_val});

		});
		$('.post .foto-bg img').load(function(){
			if($(this).width()>900){
				var height_vall = $(this).height();
				var src_val = $(this).attr('src');
				$(this).css({'width':'900px','height':height_vall});
				$(this).attr({'src':'/wp-content/themes/fer/i/0.gif'});
				$(this).css({'background':'url("' + src_val + '") center center no-repeat'});
			}
				var width_val = parseInt($(this).width()) + 16;
				$(this).parent().parent().css({'width':width_val});
				var height_val = parseInt($(this).height()) + 16;
				$(this).parent().parent().css({'height':height_val});
				width_val = width_val-14;
				$(this).parent().parent().find('.shadow1').css({'width':width_val});
				$(this).parent().parent().find('.shadow3').css({'width':width_val});
				height_val = height_val-14;
				$(this).parent().parent().find('.shadow2').css({'height':height_val});
				$(this).parent().parent().find('.shadow4').css({'height':height_val});
		});/*
		$('.post .foto-bg').each(function(){
			if($(this).find('img').length){
				var width_val = parseInt($(this).find('img').width()) + 16;
				$(this).css({'width':width_val});
				var height_val = parseInt($(this).find('img').height()) + 16;
				$(this).css({'height':height_val});
				width_val = width_val-14;
				$(this).find('.shadow1').css({'width':width_val});
				$(this).find('.shadow3').css({'width':width_val});
				height_val = height_val-14;
				$(this).find('.shadow2').css({'height':height_val});
				$(this).find('.shadow4').css({'height':height_val});
			}
			if(!$(this).find('img').length){
				$(this).find('.shadow1,.shadow2,.shadow3,.shadow4,.corner1,.corner2,.corner3,.corner4').css({'display':'none'});
			}
		});
	}*/

	/* CUFON *********************************************************************************/
	if($('.menu>ul>li>a').length){
			Cufon.replace('.menu>ul>li>a', {
				hover: true,
				fontFamily: 'Stobart'
			});
		}
	if($('#archives p a.month').length){
		Cufon.replace('#archives p a.month', {
			hover: true,
			fontFamily: 'Stobart'
		});
	}
	if($('#content .welcome h1').length){
		Cufon.replace('#content > .welcome  > h1 ', {
			hover: true,
			fontFamily: 'Stobart'
		});
	}
	if($('#content .twitter a.follow').length){
		Cufon.replace('#content > .twitter  > a.follow ', {
			hover: true,
			fontFamily: 'Stobart'
		});
	}
	if($('#content .facebook a').length){
		Cufon.replace('#content > .facebook  > a ', {
			hover: true,
			fontFamily: 'Stobart'
		});
	}
	if($('#content .post p.date ').length){
		Cufon.replace('#content > .post > p.date ', {
			hover: true,
			fontFamily: 'Stobart'
		});
	}
	if($('#content .post .hidden .frame .vote-handler a ').length){
		Cufon.replace('#content .post .hidden .frame .vote-handler a ', {
			hover: true,
			fontFamily: 'Stobart'
		});
	}
	if($('#content .post .hidden .frame .vote-handler span ').length){
		Cufon.replace('#content .post .hidden .frame .vote-handler span ', {
			hover: true,
			fontFamily: 'Stobart'
		});
	}
	if($('#content .post .hidden .menu-comments ').length){
		Cufon.replace('#content > .post > .hidden > .menu-comments  ', {
			hover: true,
			fontFamily: 'Stobart'
		});
	}
	

	if($('#content #navigation p.previous  ').length){
		Cufon.replace('#content > #navigation > p.previous ', {
			hover: true,
			fontFamily: 'Stobart'
		});
	}
	if($('#content #navigation p.next  ').length){
		Cufon.replace('#content > #navigation > p.next ', {
			hover: true,
			fontFamily: 'Stobart'
		});
	}

	if($('#content .contact_page form p label').length){
		Cufon.replace('#content .contact_page form p label', {
			hover: true,
			fontFamily: 'Stobart'
		});
	}

	/* SCROLLBAR ******************************************************************************/
	if($('.opened .comments-scroll').length){
		$('.opened .comments-scroll').jScrollPane({showArrows:false});
	}

	/* POSTS OPENER-CLOSER ********************************************************************/
	$('.opener-closer').click(function(){
		var parent_node = $(this).parents('div.post');
		var hidden_node = parent_node.find('.hidden');
		if(parent_node.hasClass('opened')){
			$(window).scrollTo({top:parseInt(parent_node.find('.hidden').offset().top), left:'0px'}, 200, {axis:'xy'});
			hidden_node.animate({'height':'hide'},300,function(){
				parent_node.removeClass('opened');
				parent_node.addClass('closed');
			});
		}else{
			$('.opened').each(function(){
				$(this).find('.hidden').css({'display':'none'});
				$(this).addClass('closed');
				$(this).removeClass('opened');
			});
			hidden_node.animate({'height':'show'},200,function(){
				frames();
				parent_node.addClass('opened');
				parent_node.removeClass('closed');
				parent_node.find('.comments-scroll').jScrollPane({showArrows:false});
				$(window).scrollTo({top:parseInt(parent_node.offset().top), left:'0px'}, 300, {axis:'xy'});
			});
		}
		return false;
	});

        $('.post .main-image img').click(function(){
            var fn = $(this);
            var post = fn.parents('div.post');

            post.find('.opener-closer a').trigger('click');
        });

	/* SLIDESHOW *****************************************************************************/
	if(jQuery('.welcome .slideshow').length){
		jQuery('.welcome .slideshow').jcarousel({
			scroll:1,
			wrap: 'last',
			buttonNextHTML: '<a href="javascript:;"></a>',
			buttonPrevHTML: '<a href="javascript:;"></a>'
		});
	}

	/* MENU *********************************************************************************/
	$('.menu .search-link a').click(function(){
		$(this).parent().find('form').toggleClass('visible');
		return false;
	});
	var timeout    = 200;
	var closetimer = 0;
	var ddmenuitem = 0;
	var timeout2    = 200;
	var closetimer2 = 0;
	var ddmenuitem2 = 0;
	var opened_node_index = 0;
	var opened_node_index2 = 0;

	function jsddm_open(){
		jsddm_canceltimer();
		jsddm_close();
		ddmenuitem = $(this).find('.submenu:eq(0)').css('visibility', 'visible');
		Cufon.replace('.menu > ul > li.hover > a', {
			hover: true,
			fontFamily: 'Stobart'
		});
	}

	function jsddm_open2(){
		jsddm_canceltimer2();
		jsddm_close2();
		//$(this).find('.posts:eq(0) .posts-scroll').jScrollPane({showArrows:false});
		ddmenuitem2 = $(this).find('.posts:eq(0)').css('visibility', 'visible');
		opened_node_index2 = $('.menu>ul>li>ul>li').index($(this));
		$(this).addClass('hover');
		/*Cufon.replace('.menu > ul > li> ul > li.hover > a', {
			hover: true,
			fontFamily: 'Stobart'
		});*/
	}

	function jsddm_close(){
		if(ddmenuitem){
			ddmenuitem.css('visibility', 'hidden');
		}
		$('.menu > ul > li').removeClass('hover');
		Cufon.replace('.menu > ul > li > a', {
			hover: false,
			fontFamily: 'Stobart'
		});
	}

	function jsddm_close2(){
		if(ddmenuitem2){
			ddmenuitem2.css('visibility', 'hidden');
		}
		$('.menu > ul > li > ul > li').removeClass('hover');
	}

	function jsddm_timer(){
		closetimer = window.setTimeout(jsddm_close, timeout);
	}
	function jsddm_timer2(){
		closetimer2 = window.setTimeout(jsddm_close2, timeout2);
	}

	function jsddm_canceltimer(){
		if(closetimer){
			window.clearTimeout(closetimer);
			closetimer = null;
		}
	}

	function jsddm_canceltimer2(){
		if(closetimer2){
			window.clearTimeout(closetimer2);
			closetimer2 = null;
		}
	}

	$('.menu > ul > li').bind('mouseover', jsddm_open);
	$('.menu > ul > li').bind('mouseout',  jsddm_timer);
	$('.menu > ul > li > ul > li').bind('mouseover', jsddm_open2);
	$('.menu > ul > li > ul > li').bind('mouseout', jsddm_timer2);

	document.onclick = jsddm_close;

});


$(function(){
    var voteForImage = function() {
        var fn = $(this);
        var link = fn.attr('href');
        var votesHandler = fn.parent().find('.votes em');
        //fn.unbind('click');
        fn.remove();
        $.get(link, {}, function(data){
            if (data != 'error') {
                votesHandler.text(data);


                Cufon.replace(votesHandler, {
			hover: true,
			fontFamily: 'Stobart'
		});

            }
        });
        return false;
    }
    $('div.post .frame a').click(voteForImage);

    $('form.add-comment').find('input[type=text], textarea').labelify();

    var validateOptions = {
        rules:{
            author:{
                required:true,
                minlength:2
            },
            email:{
                required:true,
                email:true
            },
            comment:{
                required:true
            }
        },
        ignoreTitle: true,
        onkeyup: false,
        onfocusout: false,
        showErrors:function(errorMap, errorList){
            $.each(errorList, function(key, elm){
                $(elm.element)
                   .animate( {opacity: 0.2}, 175 )
                   .animate( {opacity: 1}, 150 )
                   .animate( {opacity: 0.2}, 150 )
                   .animate( {opacity: 1}, 150 )
                   .animate( {opacity: 0.2}, 150 )
                   .animate( {opacity: 1}, 150 );
            });
        },
        invalidHandler: function(form, validator) {
            $(form.currentTarget).find("input[type=text], textarea").blur();
        },
        submitHandler:function(_form){
            var form = $(_form);

            var post = form.parents('div.post');
            var comments = post.find('.comments-scroll');
            var messages = post.find('div.messages');
            var post_id = form.find('input[name=comment_post_ID]').val();
            var link = form.attr('action');
            var loadLink = link.replace('wp-comments-post.php', 'wp-content/themes/fer/comments-ajax.php?id=' + post_id + '&type=comments')

            form.ajaxSubmit({
                beforeSubmit:function(){
                    form.find('.send').hide();
                },
                success:function(){
                    if (!comments.length) {
                        messages.before('<div class="comments сomments-scroll"></div>');
                        comments = post.find('.comments-scroll').show();
                    }
                    messages.hide().html('Thank you for commenting!').slideDown('fast');

                    setTimeout(function(){
                        messages.slideUp('fast');
                    }, 10000);

                    form.clearForm();
                    form.find('.send').show();
                    form.find('input[type=text], textarea').labelify();

                    comments.load(loadLink, {}, function(){
                        if (!comments.is(':visible')) {
                            comments.show();
                        }
                        comments.jScrollPane({showArrows:false});
                        $(window).scrollTo({top:parseInt(comments.offset().top), left:'0px'}, 300, {axis:'xy'});

                    });
                },
                error:function(request){
                    var error = request.responseText.match(/<p>([^<]+)<\/p>/)[1];
                    messages.hide().html(error).slideDown('fast');
                    form.find('div.say').show();
                }
            });
        }
    };

    $('form.add-comment').each(function(key, form){
        $(form).validate(validateOptions);
    });

    $('#contact-form').find('input[type=text], textarea').labelify({
        text:'label'
    });

    $('div.post div.foto-bg img').click(function(){
        var fn = $(this);
        var post = fn.parents('div.post');
        post.find('.opener-closer a').trigger('click');
    });

    var posts = $('div.post');
    if (posts.length == 1) {
        posts.find('.opener-closer a').trigger('click');
        posts.find('.opener-closer').remove();
    }


    function initFooterNav()
    {
        var nav = $('#footer-menu-container');

        $(window).scroll(function(){
            var fn = $(this)
            if (fn.scrollTop() >= 600) {
                nav.fadeIn('fast');
            } else {
                nav.fadeOut('fast');
            }
        });
    }
    initFooterNav();
})

