function initIphoneHomepage(){
	var homepage_icons = $('#homepage .homepage-icon');
	var homepage_fulls = $('#homepage-full .homepage-full');
	var icon_height = 51;
	var icon_width = 61;
	var animation_duration = 300;
	var animate_flag = false;
    

	homepage_icons.each(function(i,e){
        /*
        if( $(e).attr('id') == "resorts-block" || $(e).attr('id') == "office-block"){
        }
        else{
            return;
        }*/
		e = $(e);
		var full_img = $(homepage_fulls[i]);
		var cropper = $(full_img.find(".cropper"));
		var positioner = $(full_img.find(".positioner"));
		var temp_img = $(full_img.find("img"));
		var target_height = temp_img.height();
		var target_width = temp_img.width();
		var initial_position = cropper.position();
		var initial_top = initial_position.top;
		var initial_left = initial_position.left;

        //USE webkit-transformations to position the cropped image
        
		
			cropper.css({
                 
			    top: 0,
			    left: 0,
			    height: target_height,
			    width: target_width
                            //,"-webkit-transform":"translate3d(-"+initial_left+"px, -"+initial_top+"px,0)"
                
			});
            positioner.css({
			   left:0,
                //"-webkit-transform":"translate3d("+initial_left+"px, "+initial_top+"px,0)"
			   top: 0
                
			});
            cropper.css({
			    top: initial_top,
			    left: initial_left,
			    height: icon_height,
			    width: icon_width
            });
            positioner.css({
			    top: -1*initial_top,
			    left: -1*initial_left
            });

        full_img.addClass("webkit");
		$(e.find(".icon-container")).mouseover(function(){
		//$(e).mouseover(function(){
			target_height = Math.max( $(document).height(), $(window).height());
			target_width = Math.max( $(document).width(), $(window).height());
            
				
			if(animate_flag){return;}
			else{animate_flag= true;}
			full_img.css("visibility","visible");

			cropper.css({
                 
			    top: 0,
			    left: 0,
			    height: target_height,
			    width: target_width
                            //,"-webkit-transform":"translate3d(-"+initial_left+"px, -"+initial_top+"px,0)"
                
			});
            positioner.css({
			   left:0,
                //"-webkit-transform":"translate3d("+initial_left+"px, "+initial_top+"px,0)"
			   top: 0
                
			});
		});
		

        
        $(full_img.find("img")).click(function(){
            cropper.css({
			    top: initial_top,
			    left: initial_left,
			    height: icon_height,
			    width: icon_width
            });
            positioner.css({
			    top: -1*initial_top,
			    left: -1*initial_left
            });
			animate_flag = false;
			setTimeout( function(){full_img.css("visibility","hidden");}, 1000);
		});
    });
}

function isIphone(){
    if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) { 
        return true;
    }
}

function initHomepage(){
    if($("#homepage").length <= 0 ){return;}
    
    if(isIphone() && $.browser.safari){
        initIphoneHomepage();
        return;
    }
	var homepage_icons = $('#homepage .homepage-icon');
	var homepage_fulls = $('#homepage-full .homepage-full');
	var icon_height = 51;
	var icon_width = 61;
	var animation_duration = 300;
	var animate_flag = false;
    var timeout_flag = false;
    
    /*
	var document_height = document.body.clientHeight;
	var window_height = window.innerHeight;
	var document_width = document.body.clientWidth;
	//var window_width = window.innerWidth;
	var window_width = $(window).width();
		console.log(window_height + "," + window_width);
		//console.log(document_height + "," + document_width);
    */
	homepage_icons.each(function(i,e){
        /*
        if( $(e).attr('id') == "resorts-block" || $(e).attr('id') == "office-block"){
        }
        else{
            return;
        }
        */
		e = $(e);
		var full_img = $(homepage_fulls[i]);
		var cropper = $(full_img.find(".cropper"));
		var positioner = $(full_img.find(".positioner"));
		var temp_img = $(full_img.find("img"));
		//console.log(temp_img);
		var target_height = temp_img.height();
		var target_width = temp_img.width();
		//console.log(target_height + "," + target_width);
		var initial_position = cropper.position();
		var initial_top = initial_position.top;
		var initial_left = initial_position.left;
		//console.log(initial_top + "," + initial_left);
		
		$(e.find(".icon-container")).mouseover(function(){
			target_height = Math.max( $(document).height(), $(window).height());
			target_width = Math.max( $(document).width(), $(window).height());
				
        
			if(animate_flag ){return;}
			else{animate_flag= true;}
        
            cropper.css({
			    top: initial_top+"px",
			    left: initial_left+"px",
			    height: icon_height+"px",
			    width: icon_width+"px"
            });
            positioner.css({
                left: (-1 * initial_left)+"px",
                top: (-1 * initial_top)+"px"
            });
			full_img.css("visibility","visible");
			cropper.animate({
			    top: "0",
			    left: "0",
			    height: ""+target_height+"px",
			    width: ""+target_width+"px"
			}, {
			    duration: animation_duration,
			    queue:false,
			    step: function(now, fx) {
			        if (fx.prop == "left") {
			               positioner.css("left", ""+ (-1 *now) + "px");
					//console.log(-1 * now);
			        }
			        else if (fx.prop == "top") {
			               positioner.css("top", ""+ (-1* now) + "px");
			        }
			    },
                complete: function(){
			        animate_flag = false;
                }
			});
                    setTimeout(function(){
                        timeout_flag =true;
                    }, 50);
		});
		
		$(full_img.find(".homepage-icon")).mouseleave(function(){
            if(!timeout_flag){return;}
            else{
                timeout_flag = false;
            }
			animate_flag = false;
			cropper.animate({
			    top: initial_top+"px",
			    left: initial_left+"px",
			    height: icon_height+"px",
			    width: icon_width+"px"
			}, {
			    duration: animation_duration,
			    queue:false,
			    step: function(now, fx) {
			        if (fx.prop == "left") {
			               positioner.css("left", (-1 *now)+"px");
			        }
			        else if (fx.prop == "top") {
			               positioner.css("top", (-1* now)+"px");
			        }
			    },
			    complete: function(){
				full_img.css("visibility","hidden");
				animate_flag = false;
			    }
			});
		});
       
		$(full_img.find("img")).click(function(){
            //console.log("triggered");
			animate_flag = false;
			cropper.animate({
			    top: initial_top,
			    left: initial_left,
			    height: icon_height,
			    width: icon_width
			}, {
			    duration: animation_duration,
			    queue:true,
			    step: function(now, fx) {
			        if (fx.prop == "left") {
			               positioner.css("left", -1 *now);
			        }
			        else if (fx.prop == "top") {
			               positioner.css("top", -1* now);
			        }
			    },
			    complete: function(){
				full_img.css("visibility","hidden");
				//animate_flag = false;
			    }
			});
		});
        $(full_img.find("img")).mousemove(function(event){
    		    $(full_img.find(".homepage-icon")).trigger("mouseleave");
        });
	});

}


function initDropDowns(){
    if($('select').length <= 0){return;}
    $('select').selectmenu({style:'dropdown', maxHeight:262, width:165 });

}

var zoom_factor = 1.75;
function initGalleryIcons(){
    if($("ul.gallery").length <= 0){return;}
    //console.log("gallery icons");
    var all_gallery_icons = $(".gallery li a");
    //console.log(all_gallery_icons);
    all_gallery_icons.each(function(i,e){
        e=$(e);
        var img_e = e.find("img");
            if(img_e.length == 0){return;}
            var current_img_height = img_e.height();
            var current_img_width = img_e.width();
            
            var target_img_height = current_img_height * zoom_factor;
            var target_img_width = current_img_width * zoom_factor;
        var inject_position = img_e.position();
        var inject_x = inject_position.left;
        var inject_y = inject_position.top;
        var offset_x = (target_img_width - current_img_width) /2;
        var offset_y = (target_img_height - current_img_height) /2;
    
            //copy element
            //var new_e = img_e.clone();
            //var new_e = e.find("a").clone();
            var new_e = e.clone();
            var new_e_img = new_e.find("img");
            new_e.find(":not(img)").remove();

            new_e_img.css({
                position: "absolute",
                top: inject_y+"px",
                left: inject_x+"px"
            });
            e.append(new_e);
        new_e_img.mouseover(function(){
            //console.log("width,height: "+ current_img_width+ ", "+current_img_height);
            new_e_img.css("z-index","100");
            new_e_img.delay(600).animate({
                height: target_img_height+"px",
                width: target_img_width+"px",
                top: (inject_y-offset_y)+"px",
                left: (inject_x-offset_x)+"px"
            },{
			    duration: 200,
			    queue:true
            });
        });
        new_e_img.mouseleave(function(){
            new_e_img.clearQueue();
            new_e_img.animate({
                height: current_img_height+"px",
                width: current_img_width+"px",
                top: inject_y+"px",
                left: inject_x+"px"
            },{
			    duration: 200,
			    queue:false,
                step: function(now, fx) {
			        if (fx.prop == "width") {
                        if((now*.75) <= current_img_width){
                            new_e_img.css("z-index","10");
                        }
			        }
                }
                //complete: function(){ new_e_img.css("z-index","10");}
            });
        });
    });
}


var default_trunc_height = 200;
function initNewsList(){
    if($(".news-short").length<= 0 ){return;}
    var all_news = $(".news-list li");
    all_news.each(function(i,e){
        var truncate_obj = $(e).find(".news-short");
        var reference_height = $(e).find(".visual").height();
            if(reference_height == 0){
                reference_height = default_trunc_height;
            }
        var header_height = $(e).find(".news-header").outerHeight(true) + 17;
        var link_height = $(e).find(".link-holder").outerHeight(true) + 17;
        var trunc_height = reference_height - header_height - link_height;
        trunc_height  = parseInt(trunc_height/17) * 17;
            if(truncate_obj.height() > trunc_height){
                truncate_obj.css("height", trunc_height+'px');
            }
        /*
        console.log("reference_height: "+ reference_height);
        console.log("header_height: "+ header_height);
        console.log("link_height: "+ link_height);
        console.log("trunc_height: "+ trunc_height);
        */
    });
}

function initEventsList(){
    if($(".events-short").length<= 0 ){return;}
    var all_events = $(".events-list li");
    all_events.each(function(i,e){
        var truncate_obj = $(e).find(".events-short");
        var reference_height = $(e).find(".visual").height();
            if(reference_height == 0){
                reference_height = default_trunc_height;
            }
        var header_height = $(e).find(".events-header").outerHeight(true) + 17;
        var link_height = $(e).find(".link-holder").outerHeight(true) + 17;
        var trunc_height = reference_height - header_height - link_height;
        trunc_height  = parseInt(trunc_height/17) * 17;
            if(truncate_obj.height() > trunc_height){
                truncate_obj.css("height", trunc_height+'px');
            }
        /*
        console.log("reference_height: "+ reference_height);
        console.log("header_height: "+ header_height);
        console.log("link_height: "+ link_height);
        console.log("trunc_height: "+ trunc_height);
        */
    });
}


var mapHilightSpeed = 300;
function initMapHilight(){
    if($('.map img, .map-us img').length <= 0){return;}
    //$('.map img').maphilight();
    $("area").mouseover(function(event){
        
        $(".map-hilight."+img_class).clearQueue();
        var img_class = $(event.target).attr("class");
        //console.log(img_class);
        //$("#map-hilight").attr("class", img_class);
        if($.browser.msie){
            $(".map-hilight."+img_class).show();
        }
        else{
            $(".map-hilight."+img_class).fadeIn(mapHilightSpeed);
        }
        //console.log("new img class: "+ $("#map-hilight").attr("class"));


        $("area").mouseleave(function(){
            $(".map-hilight."+img_class).clearQueue();
            //console.log("mouse_leave");
 
            if($.browser.msie){
                $(".map-hilight."+img_class).hide();
            }
            else{
                $(".map-hilight."+img_class).fadeOut(mapHilightSpeed, function(){
                    $(".map-hilight."+img_class).css("opacity","1"); //forces opacity if animation is dequeued
                });
            }
        });
    });

}

function showProjectLoader(){
    $("#grid").hide();
    $(".map").hide();
    $(".map-us").hide();
    $("#grid-loader").show();
}
function showProjectGrid(){
    $("#grid").show();
    $(".map").hide();
    $("#grid-loader").hide();
    //the moment this is called, we no longer use map loader background image
    $('#grid-loader').css("background-image","url(/images/map/load-gray.png)");
}
function showWorldMap(){
    $("#grid").hide();
    $(".map").show();
    $(".map-us").hide();
}
function showUSMap(){
    $("#grid").hide();
    $(".map").hide();
    $(".map-us").show();
}
function initProjectSearch(){
    var error_msg = "<div class='wysiwyg' style='width:auto; margin-top:-3px;'><p><strong>No results match your query. Please change your search criteria and click again to search.</strong></p>";
    if($('.projects-search').length <= 0){return;}

    //if the particular project dropdown is selected, redirect them to that page
    $('#all-projects').change(function(){
        //console.log("all projects changed: "+ $("#all-projects").val());
        var new_path =  $("#all-projects").val();
        if(new_path != ''){
            window.location = new_path;
        }
    });
    
    var filter_params = ["project_client","city","state","country","project_type"];
    $('input.submit').click(function(){
            /*
            console.log("Client: "+ $("#client").val());
            console.log("State: "+ $("#state").val());
            console.log("Country: "+ $("#country").val());
            console.log("Project type: "+ $("#project-type").val());
            console.log("Current url: "+ window.location.pathname);
            */
            var filter_path = window.location.pathname+'?';
            for(var i= 0; i< filter_params.length; i++){
                    
                    if(i != 0){
                        filter_path+="&";
                    }
                    filter_path+=filter_params[i]+"="+ $("#"+filter_params[i]).val();
            }
            //console.log("filter_path: "+ filter_path);
            showProjectLoader();
            $('#grid').load(filter_path +' #filtered-grid', function(){
                if($('#grid').html().length == 0){
                    //display error message
                    $("#grid").html(error_msg);
                }
                //reinit gallery icons
                showProjectGrid();
                //initGalleryIcons();
            });
            return false;
    });

    
    // Handle a map click
    $(".map area").each(function(i, e){
        e = $(e);
        e.click(function(event){
            e.trigger("mouseleave");
            var img_class = $(event.target).attr("class");
                
                // First translate one-word country keywords to full names
                var is_usa = false;
                if (img_class == "usa") {
                    img_class = "united states";
                    is_usa = true;
                }
                else if (img_class == "netherlands") {img_class = "the netherlands";}
                else if (img_class == "south_korea") {img_class = "south korea";}
                setDropdown($("select#country"), img_class); // Remake dropdown with country selected
                if(is_usa){
                    showUSMap();
                }
                else{
                    $('input.submit').trigger("click");
                }
            return false;
        });
    });


    // Handle a map click
    $(".map-us area").each(function(i, e){
        e = $(e);
        e.click(function(event){
            e.trigger("mouseleave");
            var img_class = $(event.target).attr("class");

                // First translate one-word country keywords to full names
                img_class = img_class.replace("_"," ");
                if(img_class != "world-map"){
                    setDropdown($("select#state"), img_class); // Remake dropdown with country selected
                    $('#grid-loader').css("background-image","url(/images/map/us/load-us-map-gray.png)");
                    $('input.submit').trigger("click");
                }
                else{
                    showWorldMap();
                }
            return false;
        });
    });
}

function setDropdown(element, value){
    $(element).selectmenu("destroy");
    $(element).find("option").each(function(i,e){
        if($(e).text().toLowerCase() == value){
            $(e).attr("selected","selected");
        }
    });

    $(element).selectmenu({style:'dropdown', maxHeight:262, width:165 });

}

function initCustomScroll(){
    var adjDraggerHeight = 0;
    var minDraggerHeight = 0;
    var animSpeed = 0; 
	$customScrollBox=$("#customScrollBox");
	if ($customScrollBox.length == 0) {return;}
	$customScrollBox_container=$("#customScrollBox .container");
	$customScrollBox_content=$("#customScrollBox .content");
	$dragger_container=$("#dragger_container");
	$dragger=$("#dragger");

	visibleHeight=$customScrollBox.height();
	if($customScrollBox_container.height()>visibleHeight){ //enable scrollbar if content is long
		totalContent=$customScrollBox_content.height();
		minDraggerHeight=$dragger.height();
		draggerContainerHeight=$dragger_container.height();
		adjDraggerHeight=totalContent-((totalContent-visibleHeight)*1.3); //adjust dragger height analogous to content
            if(adjDraggerHeight<= 20){
                adjDraggerHeight = 25;
            }
		if(adjDraggerHeight>minDraggerHeight){ //minimum dragger height
			$dragger.css("height",adjDraggerHeight+"px").css("line-height",adjDraggerHeight+"px");
		} else {
			$dragger.css("height",minDraggerHeight+"px").css("line-height",minDraggerHeight+"px");
		}
		if(adjDraggerHeight<draggerContainerHeight){ //maximum dragger height
			$dragger.css("height",adjDraggerHeight+"px").css("line-height",adjDraggerHeight+"px");
		} else {
			$dragger.css("height",draggerContainerHeight-10+"px").css("line-height",(draggerContainerHeight-10)+"px");
		}
		animSpeed=400; //animation speed
		easeType="easeOutCirc"; //easing type
		bottomSpace=1.05; //bottom scrolling space
		targY=0;
		draggerHeight=$dragger.height();
		$dragger.draggable({ 
			axis: "y", 
			containment: "parent", 
			drag: function(event, ui) {
				Scroll();
			}, 
			stop: function(event, ui) {
				DraggerOut();
			}
		});
		//scrollbar click
		$dragger_container.click(function(e) {
			$this=$(this);
			//$this.css("background-color","#555");
			var mouseCoord=(e.pageY - $this.offset().top);
			var targetPos=mouseCoord+$dragger.height();
			if(targetPos<$dragger_container.height()){
				$dragger.css("top",mouseCoord+"px");
				Scroll();
			} else {
				$dragger.css("top",($dragger_container.height()-$dragger.height())+"px");
				Scroll();
			}
		});

		//mousewheel
		$(function($) {
			$customScrollBox.bind("mousewheel", function(event, delta) {
				vel = Math.abs(delta*10);
				$dragger.css("top", ($dragger.position().top-(delta*vel))+"px");
				Scroll();
				if($dragger.position().top<0){
					$dragger.css("top", 0+"px");
					$customScrollBox_container.stop();
					Scroll();
				}
				if($dragger.position().top>$dragger_container.height()-$dragger.height()){
					$dragger.css("top", ($dragger_container.height()-$dragger.height())+"px");
					$customScrollBox_container.stop();
					Scroll();
				}
				return false;
			});
		});

		//scroll
		function Scroll(){
			var scrollAmount=(totalContent-(visibleHeight/bottomSpace))/(draggerContainerHeight-draggerHeight);
        //alert(scrollAmount);
			var draggerY=$dragger.position().top;
			targY=-draggerY*scrollAmount;
			var thePos=$customScrollBox_container.position().top-targY;
			$customScrollBox_container.stop().animate({top: "-="+thePos+"px"}, animSpeed, easeType); //with easing
			//$customScrollBox_container.css("top",$customScrollBox_container.position().top-thePos+"px"); //no easing
		}

		$dragger.mouseup(function(){
			DraggerOut();
		}).mousedown(function(){
			DraggerOver();
		});

		function DraggerOver(){
			//$dragger.css("background-color", "#ccc").css("color", "#666").css("border-left-color", "#fff").css("border-right-color", "#555");
            $dragger.addClass("active");
		}

		function DraggerOut(){
			//$dragger.css("background-color", "#999").css("color", "#666").css("border-left-color", "#ccc").css("border-right-color", "#555");
            $dragger.removeClass("active");
		}
	} else { //disable scrollbar if content is short
		$dragger.css("display","none");
		$dragger_container.css("display","none");
	}
}

function initSectionImages(){
    if( !($("#outer_container ul.lister").length > 0 && $("#right_container ul.lister").length > 0) ){
        //if we cannot find a ul.lister in both those containers, we can conclude that its not a section page
        return;
    }
    var all_images = $("#right_container ul.lister li .image"); //an array of the images in the right

    var all_links = $("#outer_container ul.lister li a"); //an array of links in the left
    
    all_images.css("display","none"); //hide all images
    $(all_images[0]).css("display","block");

    all_links.each(function(i,e){ //iterate through array
        //call on an anonymouse function with args i,e
        //where i == index of the current element
        // e == current element;
        e = $(e); //convert dom element into jquery obj
        e.mouseover(function(){
            all_images.css("display","none"); //hide all images incase the mouseleave event didn't trigger
            $(all_images[i]).css("display","block");
        });
        /*
        e.mouseleave(function(){
            $(all_images[i]).css("display","none");
        });*/

    });
}


function popWindow(element){
        $(element).click(function(){

            window.open(this.href);
            return false; 
        }); 
}
function initPopupWindows(){
        $('a.target-blank').each(function(index, element){popWindow(element);});
}

function externalLinkInit(){
    //console.log(location.protocol+"//"+location.host);
    //$('a:regex(href, (?<!'+location.host+').* )').each(function(index, element){
    $('a:not(.target-blank)').each(function(index, element){
        var temp_str = $(element).attr('href');
        if(temp_str == undefined){return;}
        if((temp_str.charAt(0)!= "#" && !temp_str.match(location.host) && temp_str.charAt(0)!="/" && !temp_str.match("mailto:")) || temp_str.match(".pdf")){
            //$(element).css("target-name", "new"); //not support as of yet
            //$(element).css("target-new", "tab"); //not supported as of yet 
            $(element).click(function(){window.open(this.href); return false;});
        }
    });
    
}

function initVideo(){
    if($(".fe-video, .fe-audio").length == 0){return;}
    
    var temp_video = $( '.fe-video, .fe-audio' ).frontendVideo({
        jwplayer_params:{
            flashplayer: '/javascript/jwplayer/player.swf',
            skin: '/images/flash/beelden/beelden.xml'
        }
    });
}




$( document ).ready( function () {
    initDropDowns();
    initMapHilight();


    initProjectSearch();
    //initGalleryIcons();
    initHomepage();
    initNewsList();
	initEventsList();
    initCustomScroll();
    initSectionImages();
    externalLinkInit();
    if($('.frontend-slideshow').length > 0){
        $( '.frontend-slideshow' ).frontendSlideshow();
    }
    initVideo();

    return;
});

$(window).load(function(){
});

