﻿var aCounter = 0;
var alerting = false;
var aMax;
var aArray;
var thumbs = false;
var languageCode = "uk";
var widthFixCount = 0;
var widthFixMax = 10;

$().ready(function()
{
	if ($("#top").hasClass("dk")) languageCode = "dk";

	iLikeUpdate($.cookie("iLike") + '');

	var divs = $("div.thumbs").each(
		function()
		{
			thumbs = true;
			var t = $(this).find("img").length;
			if (t < 7)
			{
				$(this).width(t * 120);
			}
		}
	);
	fixHeights();
	aArray = $("a");
	aMax = aArray.length;
	setTimeout(recurring, 1);
	if (!thumbs) setTimeout(widthBug, 200);
});

function widthBug()
{
	if (widthFixCount < widthFixMax)
	{
		var img = aArray[1].getElementsByTagName('img');
		if (img)
		{
			var width = img[0].offsetWidth;
			if (width > 200) aArray[1].style.width = width + 'px';
		}
		widthFixCount++;
		setTimeout(widthBug, 200);
	}
}

function fixHeights()
{
	var page = pageHeight();
	var footer = $("#footer");
	var top = $("#top");
	var maxFooterHeight = page - $("#footer").offset().top - 61;
	if (originalHeight == 0) originalHeight = footer.height();
	if (originalHeight < maxFooterHeight) 
	{
		$(footer).height(maxFooterHeight);
	}
}

var resizeTimer = null;
var originalHeight = 0;
$(window).bind('resize', function() {
   if (resizeTimer) clearTimeout(resizeTimer);
   resizeTimer = setTimeout(fixHeights, 100);
});

function recurring()
{
	if (aCounter < aMax)
	{
		if (aArray[aCounter].className == 'nojs')
		{
			if (thumbs)
			{
				aArray[aCounter].className = 'js';
				aArray[aCounter].innerHTML = '<b class="bt"><b></b></b>' + aArray[aCounter].innerHTML + '<b class="bb"><b></b></b>';
				var img = aArray[aCounter].getElementsByTagName('img');
				if (img)
				{
					var width = img[0].offsetWidth - 16;
					if (width > 200) aArray[aCounter].style.width = width + 'px';
				}
			}
			else
			{
				var img = aArray[aCounter].getElementsByTagName('img');
				if (img)
				{
					var width = img[0].offsetWidth - 16;
					if (width > 200) aArray[aCounter].style.width = width + 'px';
				}
				aArray[aCounter].className = 'js';
				aArray[aCounter].innerHTML = '<b class="bt"><b></b></b>' + aArray[aCounter].innerHTML + '<b class="bb"><b></b></b>';
			}
		}
		aCounter++;
		setTimeout(recurring, 1);
	}
}

function iLikeUpdate(iLiked)
{
	var like = $("li.like").each(
		function()
		{
			var a = $(this).find("a")[0];
			var likeId = a.className;
			if (iLiked.indexOf("," + likeId + ",") > -1)
			{
				$(this).addClass("liked");
				if (languageCode == "dk")
					a.innerHTML = "Tak fordi du kunne lide billedet";
				else
					a.innerHTML = "Thank you for liking the image";
			}
			else
			{
				if (languageCode == "dk")
					a.innerHTML = "Klik hvis du kan lide billedet";
				else
					a.innerHTML = "Click if you like this image";
			}
			$(this).removeClass("nojsh");
		}
	);
}

var httpActive = false;
function iLike(e)
{
	if (!httpActive)
	{
		var id = e.className;
		var iLikeList = $.cookie("iLike") + '';
		if (iLikeList.indexOf(",") < 0) iLikeList = ",";
		if (iLikeList.indexOf("," + id + ",") < 0)
		{
			iLikeList = iLikeList + id + ",";
			$.cookie("iLike", iLikeList, { expires: 7000 });
			httpActive = true;
			if (languageCode == "dk")
				e.innerHTML = "Kontakter serveren...";
			else
				e.innerHTML = "Contacting server, please hold...";
			$.ajax({
				type: "GET",
				url: "/iLike/" + id + ".aspx",
				success: iLikeSet
			});
		}
	}
}

function iLikeSet()
{
		iLikeUpdate($.cookie("iLike") + '');
		httpActive = false;
}

function pageHeight()
{
	return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
}

function log(s)
{
	if (self.console)
		console.log(s);
	else
		if (alerting) alert(s);
}

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};