//var view = "port";

// Create a namespace for our utilities
var UTIL = UTIL || {};
UTIL.popup = UTIL.popup || {};

/**
 * Open popup window
 *
 * Opens a popup window using as little as a URL. An optional params object can
 * be passed.
 *
 * @param {String} href
 * @param {Object} params
 * @return {WindowObjectReference}
 */
UTIL.popup.open = function (href, params)
{
    // Defaults (don't leave it to the browser)
    var defaultParams = {
        "width":       "600",   // Window width
        "height":      "600",   // Window height
        "top":         "0",     // Y offset (in pixels) from top of screen
        "left":        "0",     // X offset (in pixels) from left side of screen
        "directories": "no",    // Show directories/Links bar?
        "location":    "no",    // Show location/address bar?
        "resizeable":  "no",   // Make the window resizable?
        "menubar":     "no",    // Show the menu bar?
        "toolbar":     "no",    // Show the tool (Back button etc.) bar?
        "scrollbars":  "yes",   // Show scrollbars?
        "status":      "no"     // Show the status bar?
    };

    var windowName = params["windowName"] || "new_window";

    var i, useParams = "";

    // Override defaults with custom values while we construct the params string
    for (i in defaultParams)
    {
        useParams += (useParams === "") ? "" : ",";
        useParams += i + "=";
        useParams += params[i] || defaultParams[i];
    }

    return window.open(href, windowName, useParams);
};

$(function() {         

    $(".navHover").hover(
	    function(){
			this.src = this.src.replace(".jpg","-o.jpg");
		},
		function(){
	    	this.src = this.src.replace("-o.jpg",".jpg");
		}
	);

    // Apply this code to each link with class="popup"
    $("a.popup").each(function (i){

        // Add an onClick behavior to this link
        $(this).click(function(event) {

            // Prevent the browser's default onClick handler
            event.preventDefault();

            // Grab parameters using jQuery's data() method
            var params = $(this).data("popup") || {};            

            // Use the target attribute as the window name
            if ($(this).attr("target"))
            {
                params.windowName = $(this).attr("target");
            }

            // Pop up the window
            var windowObject = UTIL.popup.open(this.href, params);

            // Save the window object for other code to use
            $(this).data("windowObject", windowObject);
        });
    });
    
    $.easing.custom = function (x, t, b, c, d) { 
    	var s = 1.70158;  
    	if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b; 
    	return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; 
	}

    // initialize scrollable  
    $("div.scrollable").scrollable({ 
        size: 1,
        //easing: 'custom',
        //speed: 700,
        
        // when seek starts make items little transparent 
        onBeforeSeek: function() { 
            //this.getItems().fadeTo(300, 0.2);         
        }, 
         
        // when seek ends resume items to full transparency 
        onSeek: function() { 
            //this.getItems().fadeTo(300, 1); 
        } 
        
    });
    
    var api = $("div.scrollable").scrollable();
    
	//initialize
    
    if(current==1){
    	var limit = 16;
    } else {
    	var limit = 6;
    }

	if (view=="portfolio"){    
	    $("#caption").html(captionArray[0]);
	
	    $("#prevArrow").css({"visibility":"hidden"});
	    var currRollover = "#portSeek00";
	    $(currRollover).attr("src","img/nav/1-o.jpg");
	   	var currPage = api.getIndex();
	   	var workingRollover;
	
	   	$("#portSeek01").addClass("navHover");
	   	$("#portSeek02").addClass("navHover");
	   	$("#portSeek03").addClass("navHover");
	   	$("#portSeek04").addClass("navHover");
	   	$("#portSeek05").addClass("navHover");
	   	$("#portSeek06").addClass("navHover");
	   	$("#portSeek07").addClass("navHover");
	   	$("#portSeek08").addClass("navHover");
	   	$("#portSeek09").addClass("navHover");
	   	$("#portSeek10").addClass("navHover");
	   	$("#portSeek11").addClass("navHover");
	   	$("#portSeek12").addClass("navHover");
	   	$("#portSeek13").addClass("navHover");
	   	$("#portSeek14").addClass("navHover");
	   	$("#portSeek15").addClass("navHover");

	}
	    
    function writeCaption(num){
	    $("#caption").html(captionArray[num]);
    }

    function pageCheck(num){
    
    	//alert((num+'').length);
    	
    	if ((num+'').length==1){
			workingRollover = "#portSeek0"+num;
		} else {
			workingRollover = "#portSeek"+num;
		}
    	$(workingRollover).removeClass("navHover");
    	$(currRollover).addClass("navHover");
    	
		imgReplace = $(currRollover).attr("src");
		imgReplace = imgReplace.replace("-o.jpg",".jpg");
    	$(currRollover).attr("src",imgReplace);
    	
    	if(num<=0){
    		$("#prevArrow").css({"visibility":"hidden"});
    		$("#nextArrow").css({"visibility":"visible"});
    	} else if (num>=(limit-1)){
    		$("#nextArrow").css({"visibility":"hidden"});
    		$("#prevArrow").css({"visibility":"visible"});
    	} else {
    		$("#nextArrow").css({"visibility":"visible"});
    		$("#prevArrow").css({"visibility":"visible"});    	
    	}
		
    	currRollover = workingRollover;
    	$(currRollover).attr("src","img/nav/"+(num+1)+"-o.jpg");
    	
      	writeCaption(num);
      	api.seekTo(num);
      	currPage = num;
    }
    
	$("#prevArrow").click(function () {
	  num = currPage - 1;
    	if(num>=0){
    		pageCheck(num);
    	}
    });
    
	$("#nextArrow").click(function () {
	  num = currPage + 1;
  		if(num<=(limit-1)){
    		pageCheck(num);
    	}
    });

	$("#portSeek00").click(function () {
      pageCheck(0);
    });

	$("#portSeek01").click(function () { 
      pageCheck(1);
    });

	$("#portSeek02").click(function () {
      pageCheck(2);
    });

	$("#portSeek03").click(function () {;
      pageCheck(3);
    });
    
    $("#portSeek04").click(function () {
      pageCheck(4);
    });
    
    $("#portSeek05").click(function () {
      pageCheck(5);
    });
    
    $("#portSeek06").click(function () {
      pageCheck(6);
    });
    
    $("#portSeek07").click(function () {
      pageCheck(7);
    });
    
    $("#portSeek08").click(function () {
      pageCheck(8);
    });
    
    $("#portSeek09").click(function () {
      pageCheck(9);
    });
    
    $("#portSeek10").click(function () {
      pageCheck(10);
    });
    
    $("#portSeek11").click(function () {
      pageCheck(11);
    });
    
    $("#portSeek12").click(function () {
      pageCheck(12);
    });
    
    $("#portSeek13").click(function () {
      pageCheck(13);
    });
    
    $("#portSeek14").click(function () {
      pageCheck(14);
    });
    
    $("#portSeek15").click(function () {
      pageCheck(15);
    });

    if (view=="pop"){
  		$("#pop").css({"visibility":"visible"});
	  	$("#port").empty();
    } else {
    	$("#pop").empty();
    }

});