//MAYHEM POETS JAVASCRIPT FUNCTIONS

var baseurl="http://mayhempoets.com/";

function viewCart(){
    document.getElementById('view_cart').submit();
}

//set the display state of a supplied (blocklevel) element
function switchViz(eid){
    var el = document.getElementById(eid);
    if (el.style.display == "none" || el.style.display == '') {
        el.style.display = "block";
    } else {
        el.style.display = "none";
    }
}

//change product image
function changeProductImage(ipath, iname){
    document.getElementById('pimg').innerHTML = '<img src="' + ipath + '" width="365" height="406" alt="' + iname + '" />';
}

function nextItem(){
    $.post(baseurl + "scripts/advanceitem.php", {
        dir: 1
    }, function(data){
        window.location = data;
    }, "text");
    
}

function previousItem(){
    $.post(baseurl + "/scripts/advanceitem.php", {
        dir: -1
    }, function(data){
        window.location = data;
    }, "text");
}

//read more... link on the about page
function readMore(){
	document.getElementById("hiddentxtarea").style.display="block";
	document.getElementById("readmore").style.display="none";
}

//------------------------
//ANIMATION FUNCTIONS
//------------------------
var cur = 0;
var moving = false;
var xpos = 0;
var dx = 0;
var targx = -841;
var timer;
var ease = .1;
var speed = 1;
var rampup = 1.2;
var rampdwn = .99;
var flip = false;
var cdir = 1;
//imagelist and theslide are both defined inside an in-page script tag via php
function startAnimation(){
    if (!timer) timer = setInterval(moveSlide, 30);
}

function stopAnimation(){
    if (!timer) return false;
    clearInterval(timer);
    timer = null;
}

function changeSlide(idir){
    if (!moving) {
        cdir = idir;
        speed = 1;
        if (cdir==1) {
            targx = -841;
        } else {
            targx = 841;
        }
        theslide = document.getElementById("photo1");
        cur += idir;
        xpos = 0;
        if (cur > imglist.length - 1) {
            cur = 0;
        } else if (cur < 0) {
            cur = imglist.length - 1
        }
        moving = true;
        startAnimation();
		document.getElementById("slidecount").innerHTML="<strong>"+(cur+1)+ " of "+imglist.length+"</strong>";
		document.getElementById("photo_desc").innerHTML=imglist[(cur)].desc;	
		
    }
}
//preload rollover images
var tp, tn, tpr, tnr;
if (document.images){
	//normals
	tp = new Image();//tall_prev
  	tn = new Image();//tall_next
  	tp.src = baseurl + "images/layout/prev_slide.gif";
  	tn.src = baseurl + "images/layout/next_slide.gif";
	//rollovers
  	tpr = new Image();//tall_prev roll
  	tnr = new Image();//tall_next roll
  	tpr.src = baseurl + "images/layout/prev_slide_roll.gif";
  	tnr.src = baseurl + "images/layout/next_slide_roll.gif";
}
//basic rollover
function swap(img,newImg){
	if (document.images) {
		document.getElementById(img).src=eval(newImg).src;
		//document.images[img].src = 
	}
}

function moveSlide(){
    if (moving) {
        //update speed
        if (flip) {
            speed = Math.max(speed * rampdwn, 1);
        } else {
            speed = Math.min(speed * rampup, 75);
        }
        
        //check bounds
        if (cdir==1) {
            //MOVING TO THE NEXT IMAGE
            xpos -= speed;
            if (xpos < targx) {
                if (targx == -841) {
                    xpos = 841;
                    //load new image while offscreen
                    document.getElementById("photodiv").innerHTML = "<img src="+baseurl+"images/photos/" + imglist[cur].path + " width=" + imglist[cur].w + " height=" + imglist[cur].h + " alt=\"\" />";
                    targx = 0;
                    flip = true;
                } else {
                    //we arrived at the viewing location with the new image loaded
                    if (targx == 0) {
                        xpos = 0;
                        moving = false;
                        stopAnimation();
                        flip = false;
                    }
                }
                
            }
        } else {
            //MOVING TO THE PREV IMAGE
            xpos += speed;
            if (xpos > targx) {
                if (targx == 841) {
					//load new image while offscreen
                    xpos = -841;
                    document.getElementById("photodiv").innerHTML = "<img src="+baseurl+"images/photos/" + imglist[cur].path + " width=" + imglist[cur].w + " height=" + imglist[cur].h + " alt=\"\" />";
                    targx = 0;
                    flip = true;
                } else {
                    //we arrived at the viewing location with the new image loaded
                    if (targx == 0) {
                        xpos = 0;
                        moving = false;
                        stopAnimation();
                        flip = false;
                    }
                }
            }
        }
        if (theslide != null) {
            theslide.style.left = xpos + "px";
        }
    }
}
