﻿(function() {
    var window = this, ent, km = {},
	$ = window.jQuery;
    /** event objects **/
    ent = {
        addProfileEvents: function() {
            if ($('.drop_menu_parent').length) {
                $('.drop_menu_parent').mouseover(function(e) {
                    $(this).find('.drop_menu').show();
                });
                $('.drop_menu_parent').mouseout(function(e) {
                    $(this).find('.drop_menu').hide();
                });
            }
        },

        addRssVideoEvent: function() {
            if ($('.rssYoutubeListItem').length) {

                function getYouTubeId(url) {
                    var youtube_id;
                    youtube_id = url.replace(/^[^v]+v.(.{11}).*/, "$1");
                    return youtube_id;
                }

                jQuery.fn.extend({

                    outerHtml: function(replacement) {
                        // We just want to replace the entire node and contents with
                        // some new html value
                        if (replacement) {
                            return this.each(function() { $(this).replaceWith(replacement); });
                        }

                        /*
                        * Now, clone the node, we want a duplicate so we don't remove
                        * the contents from the DOM. Then append the cloned node to
                        * an anonymous div.
                        * Once you have the anonymous div, you can get the innerHtml,
                        * which includes the original tag.
                        */
                        var tmp_node = $("<div></div>").append($(this).clone());
                        var markup = tmp_node.html();

                        // Don't forget to clean up or we will leak memory.
                        tmp_node.remove();
                        return markup;
                    }
                });

                var $htmlpattern = '<div class="confirm ui-corner-all">' +
			                    '<h2>%Title%</h2>' +
					            '<p>%Text% %Link%</p>' +
				            '</div>' +
				            '<div class="clearBoth"></div>';

                var $youtubehtml = '<object width="640" height="385"><param name="movie" value="http://www.youtube.com/v/%VideoId%&hl=sv_SE&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/%VideoId%&hl=sv_SE&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>';

                var $dialoghtml = '<div id="rss_video_dialog"></div>';

                var $dialog = $($dialoghtml)
		            .dialog({
		                autoOpen: false,
		                modal: true,
		                closeText: 'x',
		                width: 640,
		                beforeclose: function(event, ui) {
		                    $dialog.html($dialoghtml);
		                }
		            });

                $('.rssYoutubeListItem img').click(function(e) {
                    e.preventDefault();

                    var $id = getYouTubeId($(this).parent().find('input').val())
                    var $title = $(this).parent().parent().find('h3 span').text();
                    var $text = $(this).parent().parent().find('p').text();
                    var $link = $(this).parent().parent().find('a').outerHtml();

                    var $html = $htmlpattern.replace('%Title%', $title).replace('%Text%', $text).replace('%Link%', $link);

                    $dialog.html($youtubehtml.replace('%VideoId%', $id).replace('%VideoId%', $id) + $html)
                    $dialog.dialog('open');
                });
            }
        },

        addPageVisitTracking: function() {
            if ($('#pageVisitPageName').length && $('#pageVisitPageName').val() != '') {
                var $visits = new Array();
                var $value = $.cookie('_kommunalpvs');
                if ($value != null) {
                    $visits = $value.split(',');
                }

                var $newVisits = new Array();
                $newVisits[0] = $('#pageVisitPageName').val() + ';' + location.href;

                var j = 0;
                var k = 4;
                for (var i = 0; i < k; i++) {
                    if ($visits != null && $visits.length > i) {
                        if ($visits[i] != $newVisits[0]) {
                            $newVisits[j + 1] = $visits[i];
                            j++;
                        }
                        else
                            k++;
                    }
                }

                $.cookie('_kommunalpvs', $newVisits, { path: '/' });
            }

            var $value = $.cookie('_kommunalpvs');
            if ($value != null) {
                var $visits = new Array();
                $visits = $value.split(',');

                if ($('.drop_menu').length) {
                    var visit = new Array();
                    for (var i = 0; i < $visits.length; i++) {
                        visit = $visits[i].split(';');

                        var a = document.createElement("a");
                        a.innerHTML = visit[0];
                        a.href = visit[1];

                        var li = document.createElement("li");
                        li.appendChild(a);

                        $('.drop_menu').append(li);
                    }
                }
            }
        } 
    };

    /** 
    Class $ui
    All methods for manipulate user-interface
    **/
    km.ui = {
        status: {
            init: function() {
                ent.addProfileEvents();
            }
        },

        rssvideo: {

            addEvents: function() {
                ent.addRssVideoEvent();
            }
        },

        pagevisit: {

            addEvents: function() {
                ent.addPageVisitTracking();
            }
        }
    };

    $.extend(true, $.km.ui, km.ui);

    $(window).bindEvent({
        event: 'dom:ready',
        callback: [
            { n: '$.km.ui.status', f: 'init' },
			{ n: '$.km.ui.rssvideo', f: 'addEvents' },
			{ n: '$.km.ui.pagevisit', f: 'addEvents' }
    	]
    });

})();



