window.clickFlag = false;
var hasVideo = false;
var base = "http://www.designforlondon.gov.uk/";
var count = 0;
//*****  Permalink */
if (document.location.hash) {

	$(function(){
	
		var hash = document.location.hash;
		
		switch (document.location.pathname) {
			case "/who-we-are/people/":
			case "/news/":
				openStatic();
				break;
			case "/what-we-do/":
			case "/where-we-work/projects/":
			case "/where-we-work/boroughs/":
				openCatAndProject();
				break;
		}
	});
	
}

	/**
 * @author th
 * @project: design for london
 * @description: open ajaxed permalink
 * @version: 1.4.2
 */
function openStatic()
{
	
		window.hashed = true;
		var nhash = (document.location.pathname == "/news/") ? document.location.hash.substr(1) : document.location.hash.substr(9);
		
		var tbl = (document.location.pathname == "/news/") ? "tt_content"  : "tx_Peoplelist_list" ;    
		
		$.get(base + 'index.php?id=54&type=12345&tablename='+tbl+'&aliasstr='+nhash, function(data){
			//return data:
			var selector = (document.location.pathname == "/news/") ? "#newsid"+ data + " .single_news_date" :"#post"+data;    
	    var eachFlag = true;

			var slc = $(selector).children("a").attr("onclick");
			if (typeof slc == "function"){
				slc();
			}
			else { return false;
			}
		});

    window.hashed = false;
} // end openPerson()

function openCatAndProject()
{
	window.hashed = true;
	var nhash = document.location.hash.split("/");
	var link, menuClass;
	var ctbl = "tt_news_cat";
	var ptbl = "tt_news";
	switch(document.location.pathname)
	{
		case "/what-we-do/":
			link = "what-we-do/all/";
			break;
		case "/where-we-work/projects/":
			link = "where-we-work/projects/all/";
			break;
		case "/where-we-work/boroughs/":
			link = "where-we-work/boroughs/all/";
			break;
	}
	menuClass = '#subnav';
	// get cat&/borough
	var catID = $.get(base + 'index.php?id=54&type=12345&tablename='+ctbl+'&aliasstr='+nhash[0].substr(1), function(data){
		var initNav = $(menuClass + data).attr("onclick");
		if (typeof initNav == "function"){
			initNav();
		}
		if (nhash[1]){

			$.get(base+'index.php?id=54&type=12345&tablename='+ptbl+'&aliasstr=' + nhash[1], function(data){

				var openP = $("#post"+data +  " a").attr("onclick");
				if (typeof openP == "function"){
					openP();
				}
			});
		}
	});

	if (!document.location.hash) {
		document.location.hash = "all/";
  } 
  window.hashed = false;
} // end openCatAndProject
function getHash() {
	//return;	
}


// *********** change Image

function getImage(IMG, NUM, W, H, ALT){

   clearInterval(cSlides);
   if (window.clickFlag == true) {
        return;
    }
		if (window.slideLoaded == true){
		clickFlag = true;
		window.slideLoaded = false;
    if ('#videocontent') {
        $('#videocontent').remove();
        hasVideo = false;
        $('#imagecontent').fadeIn();
        $(".videoselecter").css({
            "background-color": "#000000"
        });
        $("#image_selecter_" + NUM + " img").attr({
            src: "fileadmin/templates/img/img_selected.png"
        });
    }
    if ($("#imagecontent img").attr("src") != IMG) {
        $("#imagecontent img").fadeOut(200, function(){
            $(this).remove();
            $(".imgsingleselecterlable").removeClass("imgsingleselecterlable").addClass("imgsingleselecterlable_selected");
            $(".mapselecterselected").removeClass("mapselecterselected").addClass("mapselecter");
            $('#imagecontent').css({
                "background-image": "url('fileadmin/templates/img/loading.gif')",
                "background-repeat": "no-repeat",
                "background-position": "50px 50%"
            });
        });
        var img = new Image();
        $(img).load(function(){
            $(this).css('display', 'none');
            
            $(".imgsingleselecterlable").removeClass("imgsingleselecterlable").addClass("imgsingleselecterlable_selected");
            $(".mapselecterselected").removeClass("mapselecterselected").addClass("mapselecter");
            $('#imagecontent').append(img);
            $(".imgsingleselecter img").attr({
                src: "fileadmin/templates/img/img_selecter.png"
            });
            $("#image_selecter_" + NUM + " img").attr({
                src: "fileadmin/templates/img/img_selected.png"
            });
            var fader = window.setInterval(function(){         
                if (W != 0 || W != '') {
                    $("#imagecontent img").attr({
                        width: W,
                        height: H,
                        "alt": decode(ALT)
                    });
                }
                $(img).fadeIn(500, function(){
                    $('#imagecontent').css({
                        "background-image": "none"
                    });   
                    clearInterval(fader);
										window.slideLoaded = true;
										count=0;
                });
            }, 500);
        }).attr({
            src: IMG,
            alt: NUM
        });
			}
			
    }
		else {
			count++;
			if(count>3){
				count=0;
				slideLoaded=true;
			}
		}
		clickFlag = false;
		cSlide();
    continueSlide();
    return false;

};

// *********** Get Map
function getMap(IMG, ALT){

    clearInterval(cSlides);
    if (window.clickFlag == true) {
        return;
    }
		clickFlag = true;
    window.clickFlag = true;
    if ('#videocontent') {
        $('#videocontent').remove();
        hasVideo = false;
        $('#imagecontent').fadeIn();
        $(".videoselecter").css({
            "background-color": "#000000"
        });
        
    }
    if ($("#imagecontent img").attr("src") != IMG) {
        $("#imagecontent img").fadeOut(300, function(){
            $(this).remove();
            $('#imagecontent').css({
                "background-image": "url('fileadmin/templates/img/loading.gif')",
                "background-repeat": "no-repeat",
                "background-position": "50px 50%"
            });
        });
        var img = new Image();
        $(img).load(function(){
            $(this).css('display', 'none');
            $('#imagecontent').append(img);
            var fader = window.setInterval(function(){
                $(img).fadeIn(300, function(){
                    $('#imagecontent').css({
                        "background-image": "none"
                    });
                    clearInterval(fader);
                });
            }, 500);
        }).attr({
            src: IMG,
            "class": "map",
            "alt": decode(ALT)
        });
    }
		clickFlag = false;
		slideLoaded = true;
		cSlide();
    continueSlide();
    return false;

}

function pclose(id, divTitle, divClasses, PID){

    if (!PID) {
        PID = "project";
    }
    if (window.clickFlag == true) {
        return;
    }
    window.clickFlag = true;
    
    $(".playerBG").css({
        "min-width": "0"
    });
    $(".playerBG").animate({
        width: 0,
        height: 0
    }, "slow", function(){
        if (typeof cSlides == "number") {
            clearInterval(cSlides);
        }
        $(".playerBG").replaceWith('<div id ="post' + id + '" class="' + PID + ' ' + divClasses + '"></div>');
        
        $("#post" + id).html('<a  onclick="loadContent(' + window.id + ',\'' + window.divTitle + '\',\'' + window.URL + '\',\'' + window.divClasses + '\',\'' + PID + '\');return false;" href="#">' + decode(window.divTitle) + '</a>');
        window.clickFlag = false;
        window.id = null;
        window.divTitle = null;
        window.URL = null;
        window.divClasses = null;
    });
    
    if (document.location.hash) {
        var hash = document.location.hash;
        tempHash = hash.split("/");
        document.location.hash = tempHash[0];
    }
    else {
        document.location.hash = "";
    }
		slideLoaded = true;
    continueSlide();
    return false;    

}

function loadContent(id, divTitle, URL, divClasses, PID)
{
    if (window.clickFlag == true) {
        return;
    }
    window.clickFlag = true;

    var myDiv = $("#post" + id);
    window.getid = id;
        
    var load_url;
    switch (PID) {
    
        case "people":
            load_url = "index.php?id=41&type=12345";
            PID = "people";
            break;        
        case "project":
            load_url = "index.php?id=26&type=12345";
            PID = "project";
            break;
        case "borough":
            load_url = "index.php?id=45&type=12345";
            PID = "borough";
            break;
        default:
            load_url = "index.php?id=26&type=12345";
            PID = "project";
    }

    $.ajax({
        url: base + load_url,
        type: "GET",
        dataType: "html",
        data: $.param({
            cid: window.getid
        }),
        
        beforeSend: function(){
            $(".playerBG").css({
                "min-width": "0"
            });
            $("#post" + window.id).parent().parent().animate({
                width: 0,
                height: 0
            }, "slow", function(){
                $(this).replaceWith('<div id ="post' + window.id + '" class="' + PID + ' ' + window.divClasses + '"></div>');
                $("#post" + window.id).html('<a onclick="loadContent(' + window.id + ',\'' + window.divTitle + '\',\'' + window.URL + '\',\'' + window.divClasses + '\',\'' + PID + '\');return false;" href="#">' + decode(window.divTitle) + '</a>');
            });
								slideLoaded = true;
        },
        
        success: function(data){
            window.data = data;
            //called when successful
            if (document.location.hash) {
                if (PID != "people") {
                    var hash = document.location.hash;
                    tempHash = hash.split("/");
                    document.location.hash = tempHash[0] + "/" + URL;
                }
                else 
                    if (!PID) {            
                    }
                    else {
                        document.location.hash = "details/" + URL;
                    }
            }
            else {
                if (PID != "people") {
                    var hash = document.location.hash;
                    tempHash = hash.split("/");
                    document.location.hash = tempHash[0] + "/" + URL;
                }
                else {
                    document.location.hash = "details/" + URL;
                }
            }
        },
        complete: function(){
            myDiv.wrap('<div class="playerBG"></div>');
            myDiv.animate({
                width: 290
            }, "slow", function(){
                $(this).css({
                    "clear": "left"
                });
                $(this).addClass("selected");
                $(".playerBG").css({
                    background: "#616161",
                    border: "thin solid #616161",
                    clear: "left",
                    width: 300,
                    marginBottom: 1
                });
                $(".playerBG").animate({
                    width: "100%",
                    height: 418
                }, "slow", function(){
                    window.clickFlag = false;
                    // Plot Data
                    $(this).replaceWith(window.data);
                    $("#ctext").jScrollPane();
                    cSlide();
                    if (typeof slideCats == "number") {
                        clearInterval(slideCats);
                    }
                    window.id = id;
                    window.divTitle = divTitle;
                    window.URL = URL;
                    window.divClasses = divClasses;
            				continueSlide();
                    $(".playerBG").css('min-width', 1000);
										$("#post"+id).children("a").css({"color":"#00CB00"});
										if ($.browser.msie && $.browser.version =="6.0"){
											if (!PID || PID == "project"){
											}
											$("#cimage").css({height: "40em"});
										}	
										else if ($.browser.msie && parseFloat($.browser.version) >=parseFloat("7.0")){
											if (PID == "people"){
												$(".playerBG").css({	height: "40.4em"	});
											}
											else if (!PID || PID == "project"){
												$(".playerBG").css({		height: "42.0em"	});
												$(".jScrollPaneContainer").css({	height: "40em"	})
											}
										}
                });
            });

            return false;
            //called when complete
        },
        error: function(){
            //called when there is an error
            return false;
        }
    });

}

/**
 *
 * @param {String} URL.flv
 * @param {Integer} W / H width and Height of the video
 * @param {Object} name
 * @param {Object} inserts the swf-object into the image container
 */
function getVideo(URL, W, H){

    if (hasVideo == false) {
        clearInterval(cSlides);
        
        $('#imagecontent').fadeOut("slow", function(){
            $('#imagecontent').before('<div id="videocontent"></div>');
            
            // TO DO:
            // CHANGE TO ACTIVE BUTTON!
            $(".imgsingleselecter img").attr({
                src: "fileadmin/templates/img/img_selecter.png"
            });
            $(".videoselecter").css({
                "background-color": "#616161"
            });
            
            $('#videocontent').flash({
                src: base + 'fileadmin/templates/swf/flvplayer.swf',
                width: W,
                height: H,
                flashvars: {
                    width: W,
                    height: H,
                    file: base + URL,
                    menu: true
                }
            }, {
                version: 8
            });
            hasVideo = true;
        });
        
        return false;
    }
}

function loadNav(id, name, description){
    if (window.clickFlag == true) {
        return;
    }
    window.clickFlag = true;
    str = "c" + id;
    //alert(str);
    
    $("#submenu .selected").removeClass("selected");
    $("#subnav" + id).parent().addClass("selected");
    $(".playerBG").animate({
        width: 0,
        height: 0
    }, "slow", function(){
        $(".playerBG").replaceWith('<div id ="post' + window.id + '" class="project ' + window.divClasses + '"></div>');
        $("#post" + window.id).html('<a onclick="loadContent(' + window.id + ',\'' + window.divTitle + '\',\'' + window.URL + '\',\'' + window.divClasses + '\');return false;" href="#">' + decode(window.divTitle) + '</a>');
        window.id = null;
        window.divTitle = null;
        window.URL = null;
        window.divClasses = null;
        
    });
    $(".project").show();
    if (str != "c0") {
        $(".project").not("." + str).hide("slow");
    }
    document.location.hash = name + "/";
    
    $("#descriptionText").text(decode(description));
    window.clickFlag = false;
    //parent.window.location.hash = name;
    return false;
    
}

function decode(str){
    return unescape(str.replace(/\+/g, " "));
    //return unescape(str.replace(/%26/g, "&"));
}
function decodeTitle(str){
	    return unescape(str.replace(/\-/g, " "));
}
/*** LOAD MAP CATS & PIC ***/
/**
 *
 * @param {Object} ID
 * @param {Integer} W / H width and Height of the video
 * @param {String} IMG image source
 * @param {Object} loads map cats and the cat pic
 */
function getCatMap(ID, NAME, IMG){
    if ($("#mapcontent img").attr("src") != IMG) {
    
        $("#mapcontent img").fadeOut(10, function(){
            $(this).remove();
            $('#mapcontent').css({
                "background-image": "url('fileadmin/templates/img/loading.gif')",
                "background-repeat": "no-repeat",
                "background-position": "50px 50%"
            });
        });
        if (IMG != 'uploads/pics/') {
        
            var img = new Image();
            $(img).load(function(){
                $(this).css('display', 'none');
                ;
                $('#mapcontent').append(img);
                var fader = window.setInterval(function(){
                    $(img).fadeIn(10, function(){
                        $('#mapcontent').css({
                            "background-image": "none"
                        });
                        clearInterval(fader);
                    })
                }, 500);
            }).attr({
                src: IMG,
                "class": "map",
								"alt": "map" + decodeTitle(NAME)
            });
        }
    }
}

var clicked = true;
function loadCat(id, name, IMG){

    if (window.clickFlag == true) {
        return;
    }
    window.clickFlag = true;
    
    if ($("#mapcontent img").attr("src") != IMG || clicked == true) {
        $("#mapcontent img").fadeOut(100, function(){
            $(this).remove();
            $('#mapcontent').css({
                "background-image": "url('fileadmin/templates/img/loading.gif')",
                "background-repeat": "no-repeat",
                "background-position": "50px 50%"
            });
        });
        
        if (IMG != 'uploads/pics/' && $(".map").length <= 1) {
        
            var img = new Image();
            $(img).load(function(){
                $(this).css('display', 'none');
                $('#mapcontent').html(img);
                $("#mapcontent img").fadeIn(100, function(){
                    $('#mapcontent').css({
                        "background-image": "none"
                    }); 
                });
            }).attr({
                src: IMG,
                "class": "map",
								"alt": "map: "+decodeTitle(name)
            });
        }
    }
    str = "c" + id;
    $(".catselected").removeClass("catselected");
    $("#cat" + id).addClass("catselected");
    
    if (window.mapsOnly == 0) {
        clicked = true;
        $(".playerBG").animate({
            width: 0,
            height: 0
        }, "slow", function(){
        
            $(".playerBG").replaceWith('<div id ="post' + window.id + '" class="project ' + window.divClasses + '"></div>');
            $("#post" + window.id).html('<a onclick="loadContent(' + window.id + ',\'' + window.divTitle + '\',\'' + window.URL + '\',\'' + window.divClasses + '\');return false;" href="#">' + decode(window.divTitle) + '</a>');
            window.id = null;
            window.divTitle = null;
            window.URL = null;
            window.divClasses = null;
        });
        $(".project").show();
        if (str != "c0") {
            $(".project").not("." + str).hide("slow");
        }
        parent.window.location.hash = name;
    }
    window.clickFlag = false;
    return false;

}

function loadBoCat(id, name, IMG){

    if (window.clickFlag == true) {
        return;
    }
    window.clickFlag = true;
    
    if ($("#boroughmap img").attr("src") != IMG || clicked == true) {
        $("#boroughmap img").fadeOut(100, function(){
            $(this).remove();
            $('#boroughmap').css({
                "background-image": "url('fileadmin/templates/img/loading.gif')",
                "background-repeat": "no-repeat",
                "background-position": "50px 50%"
            });
        });
        
        if (IMG != 'uploads/pics/' && $(".map").length <= 1) {
            var img = new Image();
            $(img).load(function(){
                $(this).css('display', 'none');
                $('#boroughmap').html(img);
                $("#boroughmap img").fadeIn(100, function(){
                    $('#boroughmap').css({
                        "background-image": "none"
                    });
                });
            }).attr({
                src: IMG,
                "class": "map",
								"alt": "map: "+ decodeTitle(name)
            });
        }
    }
	$(".playerBG").animate({
            width: 0,
            height: 0
        }, "slow", function(){
        
            $(".playerBG").replaceWith('<div id ="post' + window.id + '" class="project ' + window.divClasses + '"></div>');
            $("#post" + window.id).html('<a onclick="loadContent(' + window.id + ',\'' + window.divTitle + '\',\'' + window.URL + '\',\'' + window.divClasses + '\');return false;" href="#">' + decode(window.divTitle) + '</a>');
            window.id = null;
            window.divTitle = null;
            window.URL = null;
            window.divClasses = null;
        });
    str = "c" + id;
	$("#catmain").children(".catselected").removeClass("catselected");
	$("#subnav" + id).parent().addClass("catselected");
    $(".project").show();
    if (str != "c0") {
        $(".project").not("." + str).hide("slow");
    }
    parent.window.location.hash = name;

    window.clickFlag = false;
    return false;

}

/******* Slides */

window.slidesEnabled = true;
window.play;
window.slideLoaded = true;
function continueSlide(){
    if (typeof window.play == "number") {
        clearInterval(window.play);
    }
    window.play = window.setInterval(function(){
        if (typeof cSlides == "number") {
            clearInterval(cSlides);
        };
        if ($("#imagecontent" && typeof cSlides != "number") && !$("#videocontent")) {
            cSlide();
            clearInterval(window.play);
        }
    }, 30000);
}

var imgAmount;
var next = 1;
var cSlides;
function initIntro(){
    imgAmount = $(".imgsingleselecter").length;
}

function projSlide(){
	if(window.slideLoaded == true){
    if (next < imgAmount) {
        next++;
    }
    else {
        next = 1;
    };
    var nextSlide = $("#image_selecter_" + next).children("a").attr("onclick");
    if (typeof nextSlide == "function") {
        nextSlide();
    }
	}
}

function cSlide(){
    initIntro();
    if (imgAmount > 1) {
        if (typeof cSlides == "number") {
            clearInterval(cSlides);
        }
        cSlides = window.setInterval(function(){
            if (slidesEnabled == true) {
                var tempLoop = window.setInterval(function(){
                    $(".imgsingleselecter").click(function(){
                        clearInterval(cSlides);
                    });
                    clearInterval(tempLoop);
                }, 100);
                projSlide();
            }
        }, 4000);
    }
}


function loadNews(ID, URL){
	if (window.id != ID) {
		var loadURL = "index.php";
		var myDiv = $("#newsid" + ID);
		if (window.clickFlag == true) {
			return;
		}
		else {
			window.clickFlag = true;
		}
		$.ajax({
			url: base + loadURL,
			type: "GET",
			dataType: "html",
			data: $.param({
				id: 53,
				type: 12345,
				newsid: ID
			}),
			
			beforeSend: function(){
				if ($(".newsBG")) {
					$('.newsBG .single_news_title').removeClass("selected");
					$(".newsBG").css("min-width", "0").removeClass("newsBG").animate({
						height: "1.7em",
						width: "850px"
					}, "slow", function(){
						var newsdate = $("#newsid" + window.id).children(".single_news_date").html();
						$("#newsid" + window.id + " .single_news_gruppe").children(".single_news_text").remove();
						var newstitle = $("#newsid" + window.id + " .single_news_gruppe").html();
						$(this).replaceWith('<div class="single_news" id="newsid' + window.id +
						'"><div class="single_news_date">' +
						newsdate +
						'</div><div class="single_news_gruppe">' +
						newstitle +
						'</div></div>');
					});
				}
			},
			
			success: function(data){
				//called when successful
				window.data = data;
			},
			complete: function(){
				//called when complete
				myDiv.wrap('<div class="newsBG"></div>');
				$(".newsBG").css({
					"clear": "both",
					background: "#616161",
					border: "thin solid #616161",
					marginBottom: "1px",
					width: "850px"
				});
				
				$(".newsBG").animate({
					width: "100%",
					height: "16.5em"
				}, "slow", function(){
					$(this).replaceWith(window.data);
					$('.cnewstext').css("margin-left", 0);
					$('.newsBG .single_news_title').addClass("selected");
					window.id = ID;
					document.location.hash = URL;
					window.clickFlag = false;
					if ($.browser.msie && $.browser.version =="6.0" || $.browser.msie && $.browser.version =="7.0"){
						$(".newsBG").css({"width":"1600px"});
						if ($.browser.msie && $.browser.version =="6.0"){
							$('.cnewstext').css("margin-left", 0);
						}
					}
					
				});

				return false;
			},
			
			error: function(){
			//called when there is an error
			}
		});
	} else {
		$(".newsBG").css("min-width", "0").removeClass("newsBG").animate({
						height: "1.7em",
						width: "850px"
					}, "slow", function(){
						var newsdate = $("#newsid" + window.id).children(".single_news_date").html();
						$("#newsid" + window.id + " .single_news_gruppe").children(".single_news_text").remove();
						var newstitle = $("#newsid" + window.id + " .single_news_gruppe").html();
						$(this).replaceWith('<div class="single_news" id="newsid' + window.id +
						'"><div class="single_news_date">' +
						newsdate +
						'</div><div class="single_news_gruppe">' +
						newstitle +
						'</div></div>');
						window.id = null;
						document.location.hash = "#";
					});
	}

}