/******************************************************
       Project:  Martin Metodiev's personal blog

     Developer:  Martin Metodiev

    Started on:  29/11/2009
    Updated on:  11/03/2010

    Sheet type:  DEFAULT JavaScript
******************************************************/



	jQuery.fn.exists = function(){return jQuery(this).length>0;} // function exists()

	jQuery(document).ready(function() {

/* .............. Random image */
		function setCookie(name, value, expires, path, domain, secure) {
		  var curCookie = name + "=" + escape(value) +
			  ((expires) ? "; expires=" + expires.toGMTString() : "") +
			  ((path) ? "; path=" + path : "") +
			  ((domain) ? "; domain=" + domain : "") +
			  ((secure) ? "; secure" : "");
		  document.cookie = curCookie;
		}
		
		function getCookie(name) {
		  var dc = document.cookie;
		  var prefix = name + "=";
		  var begin = dc.indexOf("; " + prefix);
		  if (begin == -1) {
			begin = dc.indexOf(prefix);
			if (begin != 0) return null;
		  } else
			begin += 2;
		  var end = document.cookie.indexOf(";", begin);
		  if (end == -1)
			end = dc.length;
		  return unescape(dc.substring(begin + prefix.length, end));
		}
		
		function checkRandomNum(base, array){
			num = Math.ceil(Math.random()*base)
			for(var i=0; i<array.length; i++){
				if (Number(array[i])==num){
					checkRandomNum(base, array)
				}
				
			}
			return(num)
		}
		
		function randomNumber(){
			num = 8 //total number of images
			casheLimit = 7 //how many to keep in memory
			
			if (getCookie("randomCookie")){
				cookie_array = getCookie("randomCookie")
				cookie_array = cookie_array.split("|")
				if(cookie_array.length>=casheLimit){
					cookie_array.shift()
				}
				randomNum = checkRandomNum(num, cookie_array)
			}
			else {
				randomNum = Math.ceil(Math.random()*num)
				cookie_array = Array()
			}
			
			cookie_array[cookie_array.length]=randomNum
			//cookie_array.push(randomNum)
			cookiestring=""
			
			for(var i=0; i<cookie_array.length; i++){
				cookiestring +=cookie_array[i]
				if(i<cookie_array.length-1){
					cookiestring +="|"
				}
			}
			
			setCookie("randomCookie", cookiestring)
	
			jQuery("#wallpaper").addClass("wallpaper_" + randomNum);
		}
		
		jQuery("div#background").click(function() {
			jQuery("#wallpaper").removeClass();
			randomNumber();
		});
/* ........................... */


/* .................... iPhone */
		function iMobile() {
			if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
				jQuery("body").addClass("iphone");
			}
			else {
				randomNumber();
				jQuery("body").addClass("wallpaper");
			}
		}
		
		iMobile();
/* ........................... */


/* ............... Show layout */
		jQuery("#gradient_bottom").css("visibility", "visible");
/* ........................... */


/* ............... Body height */
		var bodyH = jQuery("div#lines").height();
		
		function mainHeight() {
			var viewportH = jQuery(window).height();
			var headerH = jQuery("div#header_holder").height();
			var footerH = jQuery("div#footer_holder").height();
			var mainH = jQuery("div#main div.wrapper").height();
			var mainNewH = viewportH - headerH - footerH;
			
			if (bodyH < viewportH) {
				jQuery("div#main div.wrapper").css("height", mainNewH);
			}
		}
		
		mainHeight();
/* ........................... */


/* .............. Single image */
		jQuery("img.alignnone").each(function(i) {
			var imgW = jQuery(this).width();
			var imgH = jQuery(this).height();
			var imgCoef = imgH / imgW;
			imgW = 528;
			imgH = imgW * imgCoef;
			
			jQuery(this).attr("width", imgW);
			jQuery(this).attr("height", imgH);
			jQuery(this).parent("a").css({margin: "0px", float: "none", display: "block"});
		});
/* ........................... */


/* ................. Paginator */
		var paginator = jQuery("div.paginator");
		var leftPaginator = jQuery("div.paginator div.left");
		var rightPaginator = jQuery("div.paginator div.right");
		
		if (leftPaginator.html() == "" && rightPaginator.html() == "") {
			paginator.remove();
		}
/* ........................... */


/* ............ Content height */
		function contentHeight() {
			var postBox, contentH, sidebarH = null;
			var biggerSidebar = false;
			
			function bigger() {
				contentH = jQuery("div.content").height();
				sidebarH = jQuery("div.sidebar").height();
				
				if (sidebarH > contentH) {biggerSidebar = true;}
				else {biggerSidebar = false;}
			}
			
			do {
				bigger();
				
				if (biggerSidebar) {
					var restResult = sidebarH - contentH;
					var lastLi = jQuery("ul.widgets_list li:last");
					
					if(restResult > (lastLi.height() / 2)) {
						lastLi.remove();
					}
					else {
						postBox = jQuery("div.content div.post_box:last div.post_text");
						
						if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {return false;}
						else {postBox.css("height", postBox.height() + restResult - 10);}
						break;
					}
				}
			} while (biggerSidebar);
		}
		
		contentHeight();
/* ........................... */


/* ........... Blog categories */
		var blogCatsMargin = (872 - jQuery("div#blog_categories ul").width()) / 2;
		jQuery("div#blog_categories ul").css("margin-left", blogCatsMargin);
/* ........................... */


/*................. Form focus */
		jQuery("form input.textfield, form textarea").focus(function() {
			jQuery(this).css("border-color", "#FCE47A");
			jQuery(this).css("color", "#666")
		})

		jQuery("form input.textfield, form textarea").blur(function() {
			jQuery(this).css("border-color", "#D3D3D3");
			jQuery(this).css("color", "#999")
		})
/* ........................... */


/* .................... Button */
		jQuery('form input.btn').mousedown(function(){ 
			$(this).css("background-position", "center bottom");
		});

		jQuery('form input.btn').mouseup(function(){ 
			$(this).css("background-position", "center top");
		});

		jQuery('form input.btn').blur(function(){ 
			jQuery(this).css("background-position", "center top");
		});
/* ........................... */


/*.................... Last.fm */
		jQuery("div.homepage div.sidebar div.lastfm div#lastfmlive_recent_tracks > div:last").css("border-bottom", "0px");
		
		jQuery("#lastfmlive_recent_tracks .lastfmlive_recently_played").each(function(i){
			var img = this.getElementsByTagName("img");
			img = this.removeChild(img[0]);
			var container_div = document.createElement('div');
			container_div.className="thumb";
			container_div.appendChild(img);
			this.insertBefore(container_div,this.firstChild);
		});


		jQuery("#lastfmlive_recent_tracks .lastfmlive_recently_played img").each(function(i){
			$(this).removeAttr("alt");
			$(this).attr("width", "36");
			$(this).attr("height", "36");
		});


		var listened = null;
		jQuery("div.homepage div.sidebar div.lastfm div#lastfmlive_recent_tracks div.time").each(function(i) {
			if (jQuery(this).html() != "Слушам го в момента!") {
				listened = jQuery(this).html();
				listened = listened.replace(/(\d+)\s+(\w+)\s+\w+/, 'Слушах го преди $1 $2');
				listened = listened.replace('seconds', 'секунди');
				listened = listened.replace('second', 'секунда');
				listened = listened.replace('minutes', 'минути');
				listened = listened.replace('minute', 'минута');
				listened = listened.replace('hours', 'часа');
				listened = listened.replace('hour', 'час');
				listened = listened.replace('days', 'дни');
				listened = listened.replace('day', 'ден');
				listened = listened.replace('weeks', 'седмици');
				listened = listened.replace('week', 'седмица');
				listened = listened.replace('months', 'месеца');
				listened = listened.replace('month', 'месец');
				listened = listened.replace('years', 'години');
				listened = listened.replace('year', 'година');
				jQuery(this).html(listened);
			}
		});
/* ........................... */


/* ................... Tooltip */
		jQuery('.tooltip').tipsy({gravity: 's'});
		jQuery('.tooltip-n').tipsy({gravity: 'n'});
		jQuery('.tooltip-w').tipsy({gravity: 'w'});
		jQuery('.tooltip-e').tipsy({gravity: 'e'});
/* ........................... */


/* ........... Recent projects */
		/*jQuery('.slideshow_projects').cycle({
			fx: 'scrollUp', 
    		speed: 2000, 
    		timeout: 5000 // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		});*/
/* ........................... */


/* ................ Photo page */
		jQuery('ul.photo li') 
			.before('<div id="nav">') 
			.cycle({ 
				fx:     'fade', 
				speed:  'fast', 
				timeout: 5000, 
				pager:  '#nav'
		})

		var photoTitle = jQuery("li#flickrrss a").attr("target", "_blank");



		jQuery("div#fotobook-credits").remove();
		
		jQuery("table#fotobook-main td small").each(function(i) {
			var photoLabel = jQuery(this).html();
			photoLabel = photoLabel.replace("photos", "снимки");
			jQuery(this).html(photoLabel);
		});
		
		jQuery("table#fotobook-main td a").each(function(i) {
			var titleLabel = jQuery(this).html();
			titleLabel = titleLabel.replace("My 24th Birthday", "24 успешни завъртания");
			titleLabel = titleLabel.replace("My last day @ Mag Studio", "Последен работен ден в Маг Студио");
			jQuery(this).html(titleLabel);
		});
		
		/*jQuery("div.fotobook-subheader span.main").each(function(i) {
			var albumLabel = jQuery(this).html();
			albumLabel = albumLabel.replace("Albums", "Албуми: стр. ");
			albumLabel = albumLabel.replace("out of", "от");
			jQuery(this).html(albumLabel);
		});*/
/* ........................... */


/* .............. Recent posts */
		/*jQuery('#recent_posts').cycle({ 
			fx:     'fade', 
			timeout: 15000, 
			delay:  500 
		});*/
		
		jQuery("div.recent_posts div.mp_title_box").each(function(i) {
			var recentThumb = jQuery(this).children("div.recent_posts div.mp_title_box div.text a:first");
			recentThumb.addClass("thumb");
			recentThumb.children("img").removeAttr("hspace");
			recentThumb.insertBefore(jQuery(this).children("div.recent_posts div.mp_title_box h1"));
			jQuery(this).children("div.recent_posts div.mp_title_box div.text").remove();
		});
		
		jQuery("div.recent_posts div#recent_posts div.mp_title_box:last").css("margin-bottom", "0px");
		
		//jQuery("div.recent_posts div.text").remove();
/* ........................... */


/* .............. Comment form */
		jQuery('div.comment_form a').click(function(){
			jQuery('div.comment_form form').slideToggle();
		});
/* ........................... */


/* ............... Image thumb */
		jQuery("img.tfe").attr("height", "162");
		jQuery("img.tfe").removeAttr("width");
/* ........................... */


/* ............ Homepage video */
		if (jQuery("div.wrapper").hasClass("homepage")) {
			/*var homeVideoText = jQuery("div.homepage div.video_player p.VideoCaption");
			var homeVideoTitle = jQuery("div.homepage div.video_player div.gray_line h3");
			var homeVideoLink = jQuery("div.homepage div.video_player embed").attr("src");
			homeVideoTitle.html('<a href="http://www.youtube.com/watch?v='+homeVideoLink.substring(25)+'" target="_blank">' + homeVideoText.html() + '</a>');
			homeVideoText.remove();*/
			
			jQuery("div.homepage div.video_player div.tv h2").remove();
			jQuery("div.homepage div.video_player div.tv div#videos h4").remove();
			jQuery("div.homepage div.video_player div.tv div#videos br").remove();
			jQuery("div.homepage div.video_player div.tv div#videos object, div.homepage div.video_player div.tv div#videos embed").attr("width", "570");
			jQuery("div.homepage div.video_player div.tv div#videos object, div.homepage div.video_player div.tv div#videos embed").attr("height", "460");
		}
/* ........................... */


/* ............. Sidebar video */
		/*if (jQuery("ul.widgets_list li").hasClass("randomvideosidebar")) {
			var sideVideoText = jQuery("ul.widgets_list li.randomvideosidebar p.VideoCaption");
			var sideVideoTitle = jQuery("ul.widgets_list li.randomvideosidebar div.gray_line h3");
			var sideVideoLink = jQuery("ul.widgets_list li.randomvideosidebar embed").attr("src");
			sideVideoTitle.html('<a href="http://www.youtube.com/watch?v='+sideVideoLink.substring(25)+'" target="_blank">' + sideVideoText.html() + '</a>');
			sideVideoText.remove();
		}*/
/* ........................... */


/* ..................... Cufon */
		Cufon.replace('div.twitter_box p', { fontWeight: 400 });
/* ........................... */


/* ................... Twitter */
		var twitterMsg = jQuery("div.twitter_box p");
		
		if (twitterMsg.html() == "No public Twitter messages.") {
			twitterMsg.html("В момента няма въведени съобщения в Twitter.");
		}
/* ........................... */


/* .................... Resize */
		jQuery(window).resize(function() {
			mainHeight();
		});
/* ........................... */

	});



/*****************************************************/