(function($) {
    var kff = {},
    map,
        $ = jQuery;

    kff.indexFlashLoop = function() {

        if ($("#flashLookAlike")) {

            var counter = 0,
	            divs = $("#group1, #group2, #group3");

            function showGroup() {
                divs.hide()
		            .filter(function(index) { return index == counter % 3; })
		            .fadeIn("slow");

                counter++;
            };

            showGroup();

            var slideShow = setInterval(function() {
                showGroup();
            }, 10 * 1000);

            $(".squares li").click(function() {
                clearInterval(slideShow);
            });
        }
    },

	kff.indexFlashCallback = function() {
	    $("#flashLookAlike .placeHolder .first").click(function() {
	        $(this).parent().parent().parent().hide();
	        $("#group1").show();
	    });

	    $("#flashLookAlike .placeHolder .second").click(function() {
	        $(this).parent().parent().parent().hide();
	        $("#group2").show();
	    });

	    $("#flashLookAlike .placeHolder .third").click(function() {
	        $(this).parent().parent().parent().hide();
	        $("#group3").show();
	    });
	},

	kff.kffMap = function(data) {
	    if (GBrowserIsCompatible()) {
	        var info = data,
	            l = info.length,
				i, lat, pLong, pName, pUrl, address, img, point, winInfo, singleCords, pType;
	        map = new GMap2(document.getElementById("gMap"));

	        for (i = 0; i < l; i += 1) {
	            lat = data[i].Latitude,
                pLong = data[i].Longitude,
                pName = data[i].Name,
                pUrl = data[i].Link,
                pType = data[i].ObjectType,
                address = (data[i].Address !== null) ? data[i].Address : '',
                img = data[i].Active,
	            point = new GLatLng(lat, pLong),
	            winInfo =	    
					'<p id="kffObjInfo">' + pName + '</p>' +
					'<a id="pType" href="' + pUrl + '">' + pType + '<\/a>'
	            if (img === 1) {
	                singleCords = lat + ',' + pLong;
	            }

	            map.addOverlay(kff.createMarker(point, img, winInfo));
	        }

	        if ($('#mainMapCords').length) {
	            var mainCordsInput = $('#mainMapCords').val(),
	                mainCords = mainCordsInput.split('^'),
	                setMapLat = mainCords[0],
				    setMapLong = mainCords[1],
				    setMapZoom = mainCords[2];
	        }
	        else if (!$('#mainMapCords').length) {
	            if (singleCords !== 'undefined' || singleCords !== null) {
	                var mainCords = singleCords.split(','),
	                    setMapLat = mainCords[0],
				        setMapLong = mainCords[1],
				        setMapZoom = '14';
	            }
	            else {
	                var mainCordsInput = $('#fallbackCords').val(),
	                    mainCords = mainCordsInput.split('^'),
	                    setMapLat = mainCords[0],
				        setMapLong = mainCords[1],
				        setMapZoom = mainCords[2];
	            }
	        }

	        map.setCenter(new GLatLng(setMapLat, setMapLong), parseInt(setMapZoom));
	        map.addControl(new GLargeMapControl3D());
	        map.addControl(new GMapTypeControl());
	    }
	},

	kff.kffMapList = function(data) {
	    if (GBrowserIsCompatible()) {
	        var info = data,
	            l = info.length,
				map, pId, i, lat, pLong, pName, pUrl, address, img, point, winInfo, singleCords;

	        for (i = 0; i < l; i += 1) {
	            map = new GMap2(document.getElementById("gMap" + data[i].pId)),
	            pId = data[i].pId,
	            lat = data[i].Latitude,
                pLong = data[i].Longitude,
                pName = data[i].Name,
                pUrl = data[i].Link,
                address = (data[i].Address !== null) ? data[i].Address : '',
                img = data[i].Active,
	            point = new GLatLng(lat, pLong),
	            winInfo =
	                '<div id="kffObjInfo' + data[i].pId + '">' +
	                    '<p class="name">' + pName + '</p>' +
	                    '<p>' + address + '</p>' +
					'</div>'
				;

	            if (img === 1) {
	                singleCords = lat + ',' + pLong;
	            }

	            map.addOverlay(kff.createMarker(point, img, winInfo));

	            if (singleCords !== 'undefined' || singleCords !== null) {
	                var mainCords = singleCords.split(','),
                    setMapLat = mainCords[0],
			        setMapLong = mainCords[1],
			        setMapZoom = '14';
	            }

	            if (setMapLat.length > 1 && setMapLong.length > 1) {
	                map.setCenter(new GLatLng(setMapLat, setMapLong), parseInt(setMapZoom));
	                map.addControl(new GLargeMapControl3D());
	                map.addControl(new GMapTypeControl());
	            }
	            else {
	                $("#gMapContainer" + pId).hide();
	            }
	        }
	    }
	},

	kff.createMarker = function(point, img, winInfo) {
	    var icon = new GIcon(),
		    checkImg = function() {
		        var result = "liten";
		        if (parseInt(img, 10) === 1) {
		            result = "stor";
		        }
		        return result;
		    },
		    marker;

	    icon.image = "/UI/Images/poi." + checkImg() + ".png";
	    icon.iconAnchor = new GPoint(13, 43);
	    
	    // openExtInfoWindow needs iconsize
	    if (checkImg() == 'liten') {
	        icon.infoWindowAnchor = new GPoint(96, 37);
	        icon.iconSize = new GSize(35, 31);
	    }
	    else { // blue
	        icon.infoWindowAnchor = new GPoint(103, 38);
	        icon.iconSize = new GSize(52, 44);
	    }

	    marker = new GMarker(point, { icon: icon });
	    //marker.openInfoWindowHtml(winInfo);
	    // Uses extinfowindow.js to create custom info window style
	    // see http://gmaps-utility-library-dev.googlecode.com/svn/trunk/extinfowindow/docs/reference.html
	    if (checkImg() == 'liten') {
			GEvent.addListener(marker, 'click', function() {
				marker.openExtInfoWindow(
				map,
				"simple_example_window",
				winInfo,
				{ beakOffset: 3}
				);
			});

			GEvent.addListener(marker, 'mouseover', function() {
				marker.openExtInfoWindow(
				map,
				"simple_example_window",
				winInfo,
				{ beakOffset: 3}
				);
			});
		}

	    return marker;
	},

	kff.ajaxTabs = function() {
	    $("#theTabs a").click(function(e) {
	        e.preventDefault();
	        var clickedA = $(this),
		        theHref = clickedA.attr("href"),
		        aTags = $("#theTabs a");

	        $("#goesHere").load(theHref + ' #goesHere', function(data) {
	            aTags.removeClass("active");
	            clickedA.addClass("active");
	            kff.changeImg();
	        });
	    });
	},

    kff.changeImg = function() {
        var oldPic = "/UI/images/lediga.lokaler.gif",
		    newPic = "/UI/images/lediga.lokaler2.gif",
		    logo = "/UI/images/logotype.gif",
		    newLogo = "/UI/images/logotype.hover.gif";

        $(".tabBox li a").mouseover(function() {
            $(".ledigaLokalerImg", this).attr("src", newPic);
        });

        $(".tabBox li a").mouseout(function() {
            $(".ledigaLokalerImg", this).attr("src", oldPic);
        });

        $("#header .logo").mouseover(function() {
            $("img", this).attr("src", newLogo);
        });

        $("#header .logo").mouseout(function() {
            $("img", this).attr("src", logo);
        });
    },

	kff.press = function() {
	    $("#pressPage .pressListing ul").hide();

	    $("#pressPage .pressItem:last").css("backgroundImage", "none");

	    $("#pressPage .pressListing h3").click(function() {
	        if ($(this).siblings("ul").is(":hidden")) {
	            $(this).addClass("paHover");
	            $(this).siblings("ul").slideToggle("fast");
	        }
	        else {
	            $(this).removeClass("paHover");
	            $(this).siblings("ul").slideToggle("fast");
	        }
	    });
	},

    //Sparas för framtida behov, lägger till en dynamisk kalenderlösning till vald textbox
    //	kff.calendar = function() {
    //	    var input1 = $("#news .calanderFrom").attr("id"),
    //		    input2 = $("#news .calanderTo").attr("id");

    //	    $('#' + input1).datepicker({ showOn: 'button', buttonImage: '/UI/images/date.picker.gif', buttonImageOnly: true });
    //	    $('#' + input2).datepicker({ showOn: 'button', buttonImage: '/UI/images/date.picker.gif', buttonImageOnly: true });
    //	},

	kff.calendarDate = function() {
	    var input3 = $("#ctl00_FullWidthArea_ManageFaultReport .right .calanderDate").attr("id");

	    $('#' + input3).datepicker({ showOn: 'button', buttonImage: '/UI/images/date.picker.gif', buttonImageOnly: true });
	},

    kff.externalLinks = function() {
        $("a[rel=external]").attr("target", "_blank");
    },

    kff.tableFix = function() {
        $('#rightCol .aboutBox thead td').attr('colspan', '2');
    };

    $.kff = {};

    $.kff.kffMap = kff.kffMap;
    $.kff.kffMapList = kff.kffMapList;
    $.kff.tableFix = kff.tableFix;

    //    if ($('#news')) {
    //        kff.calendar();
    //    }

    if ($('#ctl00_FullWidthArea_ManageFaultReport')) {
        kff.calendarDate();
    }

    if ($("#pressPage").length) {
        kff.press();
    }

    if ($('#flashLookAlike').length) {
        kff.indexFlashLoop();
        kff.indexFlashCallback();
    }

    kff.changeImg();
    kff.externalLinks();

    if ($('#theTabs').length && !$('#rentalTabs').length && !$('#tabBoxModule').length) {
        kff.ajaxTabs();
    }

} (jQuery));

