
var VAL = window.VAL || {};

/*
 * Cookie management
 */
VAL.Cookies = {
	writeCookie: function(name, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},
	readCookie: function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	removeCookie: function(name) {
		VAL.Cookies.writeCookie(name, '', -1);
	},
	isEnabled: function() {
		var testCookieName = 'cookie-availability-test';
		VAL.Cookies.writeCookie(testCookieName, 1, 0);
		return (VAL.Cookies.readCookie(testCookieName) !== null);
	}
};

/*
 * Global and general utilities
 */
VAL.Utils = (function() {
	
	var uniqueInstance;
	
	function printWindow() {
		window.print();
	}
	
	function addBookmark() {
		
		var title = document.title;
		var url = document.location.href;		
		
    	if(document.all) { //MSIE    		
        	external.AddFavorite(url, title);			
    	}
		else if (window.sidebar) { // the others
			try {
				window.sidebar.addPanel(title, url, "");
			}
			catch(exception) {
				alert("Klicka Ctrl+D för att spara bokmärke");	
			}			
    	} 
    	else {
        	alert("Klicka Ctrl+D för att spara bokmärke");
    	}
	}
	
	function toggleContainer(target, hideSiblings, callback) {
		if (hideSiblings) {
			$(target).toggle().siblings().hide();
		}
		else {
			$(target).toggle();
		}
		if(callback !== undefined) {
			callback();
		}
	}
		
	function constructor() {
		return {
			printWindow: printWindow,
			addBookmark: addBookmark,
			toggleContainer: toggleContainer
		};
	}
	
	return {
		getInstance: function() {
			if(!uniqueInstance) {
				uniqueInstance = constructor();
			}
			return uniqueInstance;
		}
	};
	
})();

/*
 * Setup events
 */
VAL.Events = {
    init: function() {
        $("a.comments").click(function() {
            var comments = $("#comments");
            if (comments.length > 0) {
                comments.slideDown(function() {
                    location.hash = "comments";
                });
                return false;
            }
        });

        $("a.write-comment").click(function() {
            var comments = $("#comments");
            if (comments.length > 0) {
                comments.slideDown(function() {
                    location.hash = "write-comment";
                    comments.find("input:first").focus();
                });
                return false;
            }
        });

        $('a.fn_print').click(function(e) {
            e.preventDefault();
            VAL.Utils.getInstance().printWindow();
        });

        $('a.fn_add-favorite').click(function(e) {
            e.preventDefault();
            VAL.Utils.getInstance().addBookmark();
        });

        $('div.article_push, div.news-list li, div.push_image').hover(function() {
            $(this).toggleClass('hover')
			.click(function() {
			    // Locate the link in the header element of the push
			    var whereTo = $(this).find('h2 a:eq(0)').attr('href');
			    document.location = whereTo;
			});
        });

        $('.push_rss input').focus(function() {
            $(this).select();
        });

        $('a.tip-friend, a.share-page').click(function(e) {
            e.preventDefault();

            var $this = $(this);
            var aClass = $this.attr('class');
            
            VAL.Utils.getInstance().toggleContainer($(this).parents('div.post').find('div.social-links-popup-container').find('.' + aClass), true);

            // Only scroll into position the first time user clicks on a link
            if(!$(this).parents('div.social-links').hasClass('positioned')) {
                $('html, body').animate({
                    scrollTop: $this.offset().top
                }, 600, 'easeInExpo', function() {
                    $('div.social-links').addClass('positioned');
                });
            }
            return false;
        });

        $('.fn_target-blank').each(function() {
            $(this).attr('target', '_blank');
        });

        $("a.confirm").click(function() {
            return confirm("Är du säker?");
        });

        if ($("#post-form").length > 0) {
            $.getJSON(document.location + "&json=1", function(data) {
                var textBox =
                $("input.blogCategories").autocomplete(data.availibleCategories, {
                    minChars: 0,
                    multiple: true,
                    formatItem: function(item) {
                        return item.name;
                    }
                });
                textBox.focus(function() {
                    $(this).click();
                });
            });
        }
    }
};

VAL.TinyMce = {    
    fileManager: function(field_name, url, type, win) {
        if (type != "image") {
            return;
        }
        
        tinyMCE.activeEditor.windowManager.open({
            file: "/Templates/Pages/Blog/BlogFileManager.aspx?blogUrl=" + encodeURI(document.location),
            title: "Bildgalleri",
            width: 720,
            height: 600,
            close_previous: "yes"
        }, {
            window: win,
            input: field_name
        });
        
        return false;
    }
};

$(window).load(function() {
    if ($('.carousel').length > 0 && $('.carousel').find('li').length > 1) {
        $('.carousel').carousel({
            easing: ImageCarouselEasingEffect,
            animationSpeed: ImageCarouselAnimationSpeed,
            autoPlayInterval: ImageCarouselInterval,
            autoPlay: true
        });
    }
});
/* ----------------------------------
 * Document ready
---------------------------------- */
$(document).ready(function() {
	VAL.Events.init();
});

