﻿
var loading_image_path = "/images/overlay/loading_animation.gif";

$(document).ready(function(){
    // preload images
    var images = [loading_image_path, "images/overlay/btn_close.gif"];
    for(var i = 0; i < images.length; i++){
        imgLoader = new Image();
	    imgLoader.src = images[i];
	}	
    
    var portfolioRequest = queryString(window.location.href, 'portfolio');
    if(portfolioRequest != ''){
        SetPortfolio(portfolioRequest);
    }
});

var host = 'http://' + window.location.host;

//called from portfolio examples (window.parent.SetExample();)
function SetPortfolioExample(w, h, file_path){ 
    var pb_window_height = h < 460 ? 460 : h; //window height should never be less than 500px (460 + padding-bottom:40 = 500)
    $('#PB_window iframe').fadeOut('fast').attr('src', '');
    $('#PB_window').animate({height: (pb_window_height + 'px'), width: (w + 'px'), marginLeft: '-' + (w/2) + 'px'}, 500, function(){
        $('#PB_window iframe').width(w).height(h).attr('src', (host + file_path)).fadeIn('slow');
    });
}

//called from Flash portfolio-browser on parent page - add 80px to width and 90 to the height of art
function SetPortfolio(projectName){
    switch(projectName){
       case 'super': //ANA Super
            PB_show('/portfolio/ana_super/ana_super_01.html', 730, 725);
            break;
       case 'army':
            PB_show('/portfolio/army/army_01.html', 808, 400);
            break;
       case 'cap':
            PB_show('/portfolio/cap/cap_01.html', 730, 615);
            break;
       case 'fox':
            PB_show('/portfolio/fox/fox_01.html', 760, 695);
            break;
       case 'gcm':
            PB_show('/portfolio/gcm/gcm_01.html', 648, 690);
            break;
       case 'genzyme':
            PB_show('/portfolio/genzyme/genzyme_01.html', 730, 710);
            break;
       case 'gsk':
            PB_show('/portfolio/gsk/gsk_01.html', 808, 410);
            break;
       case 'johnson':
            PB_show('/portfolio/johnson/johnson_01.html', 730, 690);
            break;
       case 'lacare':
            PB_show('/portfolio/lacare/lacare_01.html', 680, 485);
            break;
       case 'lottery':
            PB_show('/portfolio/lottery/lottery_01.html', 808, 350);
            break;
       case 'sce':
            PB_show('/portfolio/sce/sce_01.html', 730, 715);
            break;
       case 'toyota':
            PB_show('/portfolio/toyota/toyota_01.html', 592, 639);
            break;
       case 'ubh':
            PB_show('/portfolio/ubh/ubh_01.html', 648, 650);
            break;
       case 'uhc':
            PB_show('/portfolio/uhc/uhc_01.html', 730, 590);
            break;
       case 'versailles':
            PB_show('/portfolio/versailles/versailles_01.html', 730, 690);
            break;
       case 'westbasin':
            PB_show('/portfolio/wb/wb_01.html', 730, 690);
            break;
	   case 'sempra':
            PB_show('/portfolio/sempra/sempra_01.html', 580, 430);
            break;
       case 'sdge':
            PB_show('/portfolio/sdge/sdge_01.html', 680, 496);
            break;
    }
}

function queryString(url, param){
    if(arguments.length == 1){
        url = window.location.href;
    }
    var qs = url.substr((url.indexOf('?') + 1));
    qs = qs.split('#')[0];//account for named anchors
    var params = qs.split('&');
    for(var i = 0; i < params.length; i++){
        var pair = params[i].split('=');
        if(pair[0] == param){
            return pair[1];
        }
    }
    return '';
}

function PB_show(iframe_src, iframe_width, iframe_height) {
    if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
	    $("body","html").css({height: "100%", width: "100%"});
    }
	    if(document.getElementById("PB_overlay") === null){
		    $("body").append("<div id='PB_overlay'></div><div id='PB_window'></div>");
		    $("#PB_overlay").click(PB_remove);
	    }

    if(PB_detectMacXFF()){
	    $("#PB_overlay").addClass("PB_overlayMacFFBGHack");//use png overlay so hide flash
    }else{
	    $("#PB_overlay").addClass("PB_overlayBG");//use background and opacity
    }

    $("body").append("<div id='PB_load'><img src='" + loading_image_path + "' /></div>");//add loader to the page
    $('#PB_load').show();//show loader

    var htmlString = "\
        <div id='PB_header'>\
            <div id='PB_close_window'>\
                <a href='#' id='PB_closeWindowButton' title='Close'>\
                    <img border='0' src='/images/overlay/btn_close.gif'/>\
                </a>\
            </div>\
         </div>\
             <iframe scrolling='no' frameborder='0' hspace='0' id='PB_iframeContent' \
                 name='PB_iframeContent"+Math.round(Math.random()*1000)+"' onload='PB_showIframe()' px;'></iframe>\
        ";
        
    $("#PB_window").append(htmlString);
    $("#PB_closeWindowButton").click(PB_remove);
    //("#PB_window").css({marginLeft: '-' + parseInt((iframe_width / 2),10) + 'px', width: iframe_width + 'px'});
    SetPortfolioExample(iframe_width, iframe_height, iframe_src);
    if($.browser.safari){//safari needs help because it will not fire iframe onload
	    $("#PB_load").remove();
	    $("#PB_window").css({display:"block"});
    }
}

function PB_showIframe(){
	$("#PB_load").remove();
	$("#PB_window").css({display:"block"});
}

function PB_remove() {
	$("#PB_closeWindowButton").unbind("click");
	$("#PB_window").fadeOut("fast",function(){$('#PB_window,#PB_overlay,#PB_HideSelect').trigger("unload").unbind().remove();});
	$("#PB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function PB_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

//mac firefox previous to version 3
function PB_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox') != -1) {
    return true;
  }
  return false;
}
