 $(document).ready(function(){


// General -----------------------------------------------

$window = $(window);
$sidebar = $("#sidebar");
$sidebarNav = $('nav', $sidebar); 
$logo = $("#logoWrapper");
$logoIcon = $("#logoIcon");
$openCloseSidebarTxt = $("#openCloseSidebarTxt");
$socialLinksWrapper = $('#socialLinksWrapper');
$socialLinks = $('#socialLinksWrapper a');
$homePageLinks = $('#homePageLinks a');
$paginationLinks = $('#pagination a');
$articleHeaders = $('#articleList li h1');
$submitCommentBt = $('#submitCommentBt');
$iLikeIt = $('#iLikeIt div');
$isOldIE = false;
$imgLink = $('.imgLink');
$mainMenuItems = $('#menu-default>.menu-item');

if ($('html').hasClass('oldIE')) {
    $isOldIE = true;
}

// Global Variables --------------------------------------

$windowHeight = 0; 
$negativeMenuMargin = 0;
$sidebarNavHeight = $sidebarNav.height();
$socialLinksHeight = $socialLinksWrapper.height()

// logo animation ----------------------------------------

$sidebar.hover(
	function(){									// jeśli najadę na sidebar
		$logoIcon.stop().animate({				// ukryj logo
			left: 105
		},100);
		$openCloseSidebarTxt.stop().animate({	// pokaż napis "otwórz / zamknij menu" 
			left: 0
		},250)
	},
	function(){									// jeśli wyjadę z sidebaru
		$openCloseSidebarTxt.stop().animate({	// ukryj napis "otwórz / zamknij menu" 
			left: -105
		},250);
		$logoIcon.stop().animate({				// pokaż logo
			left: 0
		},100);
	}
);

// sliding sidebar ---------------------------------------

// cookie - memorizes the state of the sidebar (open / closed)

function getCookie(c_name){
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
return "";
}

function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString()) + "; path=/";
}

function checkCookie(){								// by default the sidebar is closed
	sidebarState = getCookie('sidebarState');
	if (sidebarState!=null && sidebarState!=""){	// if there is a cookie
		if (sidebarState == 'open'){ 				// and its value is open
			$sidebar.css({							// open the sidebar
				'margin-left': 0
			});
			$logo.addClass("open");					// and add class "open" to the sidebar
		}											// if the cookie has value different than "open" (closed) don't do anything
	} else {										// if the cookie isn't set
		$logo.addClass("closed");					// add the class "closed" to the sidebar
	}
}

$(document).ready(function(){
	checkCookie();
})

$logo.click(function(){
    if (($(this).hasClass("open"))) {				// if a sidebar is closed by the user
        $sidebar.stop().animate({
            'margin-left': -200
        }, 250);
        $logo.removeClass("open");
		setCookie('sidebarState','closed',14);		// set the cookie value to "closed"
    }
    else {											// if a sidebar is opened by the user
        $sidebar.stop().animate({
            'margin-left': 0
        }, 250);
        $logo.addClass("open");
		setCookie('sidebarState','open',14);		// set the cookie value to "open"
    }
    
})

// centering social icons (right sidebar) -----------------

$socialLinksWrapper.css({
	height: $socialLinksHeight,
	marginTop: -($socialLinksHeight/2)
})

// centering sidebar content ------------------------------

$sidebarNav.css({
	height: $sidebarNavHeight,
	marginTop: -($sidebarNavHeight/2)
})

/*
function SmoothSidebarCentering(){};
*/

// dropdown submenu animations ------------------------------------------

// adds "plus" sign to categories with subcategories
$mainMenuItems.each(
	function(){
		if($('.sub-menu', this).size()){
			$currentLink = $('a', this).first();
			$currentLink
				.after('<div class="showSubcatsBt closedSubcats">&nbsp;</div>')
				.css({
					width: '134'	
			})
		}
	}
)

// saves in a variable all the "plus" signs
$showSubcatsBtns = $('.showSubcatsBt');

// animations fired after clicking "plus" sign
$showSubcatsBtns.click(
	function(){
		if($(this).hasClass('closedSubcats')){
			
			$(this)
				.removeClass('closedSubcats')
				.addClass('openSubcats');
			
			$currentSubmenu = $(this).next();
			$submenuHeight = 0;
			
			$currentSubmenu
				.find('li')
				.each(
					function(){
						$submenuHeight += 36;
					}
				);
				
			$currentSubmenu
				.animate({
					height: $submenuHeight
				},200);
			
			// adds submenu's height to the overall height of the nav menu
			$sidebarNavHeight += $submenuHeight;
			
			// animates opeining submenu and changed menu position
			$sidebarNav.animate({
				height: $sidebarNavHeight,
				marginTop: -($sidebarNavHeight/2)
			}, 200)
			
		}else{
			
			$(this)
				.removeClass('openSubcats')
				.addClass('closedSubcats')
				
			$currentSubmenu = $(this).next();
			$currentSubmenuHeight = $currentSubmenu.height();
			 
			$currentSubmenu
				.animate({
					height: 0
				},200);
			
			$sidebarNavHeight = $sidebarNavHeight - $currentSubmenuHeight;
			
			$sidebarNav.animate({
				height: $sidebarNavHeight,
				marginTop: -($sidebarNavHeight/2)
			}, 200)
		}
	}
)


// fading icons -----------------------------------------------

if (!$isOldIE) { //don't fade when viewed on IE (double alpha transparency bug
	
	// fades Social Media Icons
    $socialLinks.hover(function(){
		$socialLinksWrapper.stop().animate({
			width: '50'
		},100);
        $('div', this).stop().animate({
            opacity: '1'
        }, 150)
    }, function(){
		$socialLinksWrapper.stop().animate({
			width: '45'
		},100);
        $('div', this).stop().animate({
            opacity: '0'
        }, 150)
    })
}

// Code Box -------------------------------------------------------------------------------------------------------------
	
	$("pre")
		// operations will be conducted on each <pre> object separately
		.each(function(){
		// checks and saves the height of the code box in the variable code_height 
        var code_height = $('code', this).height() + 3;
		// measures how many lines of code we have by dividing the total height of th code by the line-height of the text
        var no_of_code_lines = Math.floor(code_height / 18); //18px default line-height
		// makes a div before the current <pre> element - this is where we will keep our line numbers
        if(no_of_code_lines>10){
			$(this)
			.wrap('<div class="codeWrapper hiddenCode" style="height:192px;"></div>')
			.before('<div class="lineCounter" style="height:' + code_height + 'px; "><div>')
			.closest('.codeWrapper')
			.after('<div class="toggleCodeBox"><span class="toggleCodeButton">Show all code</span></div>')
		} else{
			$(this).before('<div class="lineCounter" style="height:' + code_height + 'px; "><div>');
		}
		// creates a new, empty variable char_string which will hold the numbers
        var char_string = '';
		// creates an html string with following numbers and <br /> tags - the number is equal to the number of code lines
        for (i = 1; i <= no_of_code_lines; i++) {
            char_string += i + "<br />";
        }
		// inserts our string in lineCounter which we created ".before" our <pre> tag.  
        $(this).prev().html(char_string);
    })
    	.hover(function(){
        $codeInnerWidth = $("code", this).width();
        if($codeInnerWidth > 530) {
            $(this)
                .stop()
                .css({
                    zIndex: "100",
                    position: "relative"})
                .animate({
                width: $codeInnerWidth
            },250);
        }
    }, function(){
        $(this).stop().animate({
            width: '530'
        },250);
    });

// toggling visibility of full code in the codebox

$toggleCodeButtons = $('.toggleCodeButton');

$toggleCodeButtons.click(
	function(){
		$codeBox = $(this).parent().prev();
		$codeBoxOrigHeight = $('pre', $codeBox).height()+20; //20px added for paddings
		if($codeBox.hasClass('hiddenCode')){
			$codeBox.stop().animate({
				'height': $codeBoxOrigHeight
			}, $codeBoxOrigHeight/2);
			$codeBox.removeClass('hiddenCode');
			$(this).html('Hide')
		}else{
			$codeBox.stop().animate({
				'height': '192'
			}, $codeBoxOrigHeight/2);
			$codeBox.addClass('hiddenCode');
			$(this).html('Show all code')
		}
	}
);

// I Like It button - swapping bg ----------------------------------

$iLikeIt.click(
	function(){
		$(this).addClass('thankYou')
				.removeClass('on')
				.html('Thanks');
	})

// pagination links animations -------------------------------------

$paginationLinks.hover(
	function(){
		$('span',this).stop().animate({
			'margin-right': '5',
			'margin-left':'5'
		},100)
	},
	function(){
		$('span',this).stop().animate({
			'margin-right': '8',
			'margin-left':'8'
		},100)
	}
)

// showing img. captions on hover in portfolioList

if ($isOldIE){
	$('p',$imgLink).css({
		'opacity':'0'
});	
}


$imgLink.hover(
	function(){
		$('p',this).stop().animate({
			'opacity': '1'
		},200);
	},function()
	{
		$('p',this).stop().animate({
			'opacity': '0'
		},200);;
	}
)

// submit comment bt animation --------------------------------------

$submitCommentBt.hover(
	function(){
		$('#submit',this).stop().animate({
			'padding-right': '32'
		},150)
		$('#submitCommentArrow',this).stop().animate({
			'margin-right': '5',
			'margin-left': '5'
		},150)
	},
	function(){
		$('#submit',this).stop().animate({
			'padding-right': '42'
		},150)
		$('#submitCommentArrow',this).stop().animate({
			'margin-right': '10',
			'margin-left': '10'
		},150)
	}
)

// homePageLinks animations -----------------------------------------

$homePageLinks = $('#homePageLinks a');

$($homePageLinks).hover(function(){
    $('.dottedStrip', this).stop().animate({
        fontSize: '22',
        top: '76'
    }, 200)
}, function(){
    $('.dottedStrip', this).stop().animate({
        fontSize: '18',
        top: '80'
    }, 200)
})

// animates fading labels --------------------------------------------

$formItems = $("input:text, textarea");

$formItems
	.each(function(){
		if ($(this).val() !== '') {
		// jeśli inputy mają jakąś wartość to labels są ukrywane
		$(this).prev().css({
				opacity: '0'
			});
		};
	})
	.focus(function(){ // po najechaniu inputu labels znikają jeśli input nie miał żadnej treści
		if ($(this).val() == '') {
			$(this).prev().stop().animate({
				opacity: '0'
			}, 200);
		}	
	})
	.blur(function(){
		if ($(this).val() == '') { // po opuszczeniu inputu labels pojawiają się jeśli nic nie zostało wprowadzone do inputu
			$(this).prev().stop().animate({
				opacity: '1'
			}, 200);
		}
	})
	
	
// ---- end ----
});
