/*****************/
// Showcase (portfolio) script v1.0, copyright webelement.co.nz
// 
//
/****************/

var NUM_SHOW = 6;
var IDX = 0;
var PICS_FLD = "/images/portfolio/one_pic/";

function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

function $$(tag) {
    return document.createElement(tag);
}

function clear(holder) {
	while(holder.hasChildNodes()) {
		holder.removeChild(holder.firstChild);
	}
}

var ScrollDirection = new function() {
    this.none = 0;
    this.prev = 1;
    this.next = 2;
    this.up = 3;
    this.down = 4;
}

shuffle = function(o){
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

var Site = function(name, domain) {    
    var _sfxPic = "-pic.jpg";
    var _sfxThumb = "-thumb.jpg";
    var _sfxScreenShot = "-web.jpg";

    this.name = name;
    this.domain = domain;        
    this.pic = PICS_FLD + this.name + _sfxScreenShot;
    this.url = 'www.' + this.domain;
    this.thumb = PICS_FLD + this.name + _sfxThumb;
}

var Portfolio = function(displayId, scrollerId, listId) {

    this.display = $(displayId);
    this.scroller = $(scrollerId);
    this.list = $(listId);
    this.sites = [];
    this.currentPage = 0;
    this.pageSize = 6;
     
    this.create = function() {
        if (!this.display)
            return;
        
        this.sites = [
            new Site("skazka", "skazka.co.nz")
            ,new Site("enterprise", "enterprise.co.nz")             
            ,new Site("nzthoroughbred", "nzthoroughbred.co.nz")
            ,new Site("thoroughbrednews", "thoroughbrednews.co.nz")
            ,new Site("careermarket", "careermarket.com.au")
            ,new Site("pencarrowstud", "pencarrowstud.co.nz")            
            ,new Site("haunuifarm", "haunuifarm.co.nz")
            ,new Site("jpmmedia", "jpmmedia.com.au")
            ,new Site("mwbloodstock", "mwbloodstock.com")
            ,new Site("racinghalloffame", "racinghalloffame.co.nz")
            ,new Site("theoaksstud", "theoaksstud.co.nz")
            ,new Site("trelawneystud", "trelawneystud.co.nz")
            ,new Site("data2000", "data2000.co.nz")
            ];
        
        shuffle(this.sites);
    }   
    
    this.scroll = function(direction) {
    
    }
    
    this.refresh = function(page) {
    
        if (page * this.pageSize > this.sites.length - 1 || page < 0)
            return false;
        
        this.currentPage = page;
        
        //this.show(page * this.pageSize);
        this.showList(page);
        this.itemClick(this.list.getElementsByTagName("A")[0], page * this.pageSize);        
        
        return true;
    }
    
    this.showList = function(page) {
        clear(this.list);
        var start = (page * this.pageSize) > this.sites.length ? 0 : (page * this.pageSize);
        var end = (start + this.pageSize) > this.sites.length ? this.sites.length : (start + this.pageSize);
        var thumb = null;
        for (i = start; i < end; i++) {
            thumb = this.createThumb(i);
            if (((i + 1) % 3) == 0) {
                this.addBreak();
                thumb.style.marginRight = '0px';
            }                
        }
    }
    
    this.addBreak = function() {
        var el = $$("DIV");
        el.style.clear = "both";
        this.list.appendChild(el);
    }
    
    this.show = function(idx) {
        if (idx < 0 || idx > this.sites.length)
            return;
                            
        clear(this.display);
        changeOpac(10, this.display.id);
            
        var site = this.sites[idx];
        
        var el = $$("IMG");
        el.src = site.pic;        
        
        this.display.appendChild(el);         
        this.addBtns(site);
        
        opacity(this.display.id, 10, 100, 500); 
    }
    
    this.addBtns = function(site) {    
        var holder = $$("DIV");
        holder.className = "list-btn";
        
        this.createMoreInfo(holder, site);
        this.createGoToSite(holder, site);
        
        var a = $$("A");
        a.href = 'http://' + site.url;
        a.innerHTML = site.domain;
        a.target = '_blank';
        a.style.fontSize = '10px';
        holder.appendChild(a);
        
        this.display.appendChild(holder);
    }
    
    this.createThumb = function(i) {
        var site = this.sites[i];
        
        var el = $$("A");
        
        var im = $$("IMG");
        im.src = site.thumb;
        im.className = "list-thumb";
        el.appendChild(im);
        
        /*el2 = $$("DIV");
        el2.innerHTML = site.name;
        el2.className = "list-name";
        el.appendChild(el2);*/
        
        el.className = "list-item";        
                
        if (el.addEventListener) {
            el.addEventListener("click", function(e){itemClick(el, i)}, false);            
	    }
	    else {
		    //el.onclick = function(){show(i);el.className = 'list-item opa-off';el.id = 'selected-item'};
		    el.onclick = function() {itemClick(el, i)};
		    el.onmouseover = function(){el.className = 'list-item opa-off'};
		    el.onmouseout = function(){opaOff(el)};
	    }
        this.list.appendChild(el);
        
        return el;
    }
    
    this.itemClick = function(item, idx) {
        show(idx);
        item.className = 'list-item opa-off';
        var items = this.list.getElementsByTagName("A");
        
        for (i = 0; i < items.length; i++) {
        
            if (items[i].id == 'selected-item') {
                items[i].id = i;
                items[i].className = 'list-item';
            }
        }
        item.id = 'selected-item';    
    }
    
    this.createMoreInfo = function(holder, site) {
        var a = $$("A");
        a.href = "/portfolio.aspx?site=" + site.domain;
        
        var btn = $$("IMG");
        btn.src = "/images/button-more-info.gif";
        a.appendChild(btn);
        
        holder.appendChild(a);        
    }
    
    this.createGoToSite = function(holder, site) {
        var a = $$("A");
        a.href = 'http://' + site.url;
        a.target = "_blank";
        
        var btn = $$("IMG");
        btn.src = "/images/button-visit-website.gif";
        a.appendChild(btn);
        
        holder.appendChild(a);        
    }
    
    this.opaOff = function(el) {
        if (el.id != 'selected-item') {
            el.className = 'list-item';
        }
        
    }
}

function opacity(id, opacStart, opacEnd, millisec) { 
    var speed = Math.round(millisec / 50); 
    var timer = 0; 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

function changeOpac(opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
} 

function shiftOpacity(id, millisec) { 
    if(document.getElementById(id).style.opacity == 0) { 
        opacity(id, 0, 100, millisec); 
    } else { 
        opacity(id, 100, 0, millisec); 
    } 
} 

var oPortfolio = null;
var g_Page = 0;

function show(idx) {
    
    oPortfolio.show(idx);    
}

function scroll(page) {
    if (oPortfolio.refresh(page))
        g_Page = page;
}

function createPortfolio() {
    oPortfolio = new Portfolio('display', 'scroller', 'list');
    oPortfolio.create();
    oPortfolio.refresh(0);    
}

function opaOff(el) {
    oPortfolio.opaOff(el);
} 

function itemClick(item, idx) {
    oPortfolio.itemClick(item, idx);
}

function toggle(id) {
    var obj = $(id);
    var img = $(id + "-img");
    if (obj.className.toLowerCase() == 'hidden') {
        obj.className = 'visible';
        img.src = '/images/minus1.gif';
    }
    else {
        obj.className = 'hidden';
        img.src = '/images/plus1.gif';
    }
}

function more(id) {
    if (!id)
        return;
        
    var obj = $(id);
    if (!obj)
        return;
        
    if (obj.className == 'hidden') {
        toggle(id);
    }
    
    window.location.hash = id;
}

function clickToMore() {
    
    if (window.location.href.toLowerCase().indexOf('site=') == -1)
        return false;
    
    var q = window.location.href.split('?');
    if (q.length < 2)
        return false;
        
    var domain = q[1].split('=')[1]; 
    
    var s = [];
    
    s["skazka.co.nz"] = 'ecom';
    s["enterprise.co.nz"] = 'job';
    s["nzthoroughbred.co.nz"] = 'cms';
    s["thoroughbrednews.co.nz"] = 'news';
    s["careermarket.com.au"] = 'job';
    s["pencarrowstud.co.nz"] = 'tnet';
    s["haunuifarm.co.nz"] = 'tnet';
    s["jpmmedia.com.au"] = 'directory';
    s["mwbloodstock.com"] = 'tnet';
    s["racinghalloffame.co.nz"] = 'tnet';
    s["theoaksstud.co.nz"] = 'tnet';
    s["trelawneystud.co.nz"] = 'tnet';
    s["data2000.co.nz"] = 'ecom';
    
    more(s[domain]);
}

window.onload = createPortfolio;

