var currentPage = 1;
var isClickable = true;

function nextPage (div, direction, updateCurrentPage, totalPages, nextButton, previousButton) {
    var content;
    if (direction == 'back') {
		if (currentPage == 1) {
			// Cannot go back any more:
		} else {
			// Back one page:
			$(div+(currentPage-1)).style.display = 'block';
            $(div+currentPage).style.display = 'none';
            currentPage = currentPage - 1;
            if (currentPage == 1) {
                $(previousButton).innerHTML = "<font class='feed_nonselected'>&lt;previous</font>";
		    } else {
                $(previousButton).innerHTML = "<a class='feed_selected' href='javascript:void(0);' onclick='nextPage(\"" + div + "\",\"back\",\"curr_div\",\"" + totalPages + "\",\"feed_front\",\"feed_back\");'>&lt;previous</a>";
            }
            if (currentPage != totalPages) {
                $(nextButton).innerHTML = "<a class='feed_selected' href='javascript:void(0);' onclick='nextPage(\"" + div + "\",\"forward\",\"curr_div\",\"" + totalPages + "\",\"feed_front\",\"feed_back\");'>next&gt;</a>";
            }
		}
    } else {
        if (currentPage == totalPages) {
			// Cannot go forward any more:
		} else {
			// Go forward:
			$(div+(currentPage+1)).style.display = 'block';
            $(div+currentPage).style.display = 'none';
            currentPage = currentPage + 1;
            if (currentPage == totalPages) {
                $(nextButton).innerHTML = "<font class='feed_nonselected'>next&gt;</font>";
		    } else {
                $(nextButton).innerHTML = "<a class='feed_selected' href='javascript:void(0);' onclick='nextPage(\"" + div + "\",\"forward\",\"curr_div\",\"" + totalPages + "\",\"feed_front\",\"feed_back\");'>next&gt;</a>";
            }
            if (currentPage != 1) {
                $(previousButton).innerHTML = "<a class='feed_selected' href='javascript:void(0);' onclick='nextPage(\"" + div + "\",\"back\",\"curr_div\",\"" + totalPages + "\",\"feed_front\",\"feed_back\");'>&lt;previous</a>";
            }
        }
    }
    $(updateCurrentPage).innerHTML = currentPage;
}

function GetPageContent (script, result) {
	// Fade out:
	url = script;   
	
	ajx = new Ajax.Request(url,
	{
		onSuccess: function(transport) {
			var data = transport.responseText;
			eval ("data=" + data);
			$(result).innerHTML = data["Content"];
			opacity('serviceContent', 0, 100, 500);
		},
		onFailure: function(transport) {}
	});
}

function GetTeamMember (script) {
	url = script;
	
	ajx = new Ajax.Request(url,
	{
		onSuccess: function(transport) {
			// $('serviceContent').innerHTML = transport.responseText;
			eval ("data=" + transport.responseText);
			
			$('teamPhoto').src = "http://www.graydotdev1.com/php/IMAGES/STUDIO_v2_Asset_Library/" + data["Photo"] + "---Image.jpg";
			$('capacityImage').src = "images/" + data["Capacity"].replace (" ", "_").toLowerCase() + ".png";
			
			for (i = 0; i < data["Content"].length; i++) {
				if (i == 0) {
					resultString = data["Content"][i];
				} else {
					resultString += data["Content"][i];
				}
			}
			$('serviceContent').innerHTML = resultString;
			opacity('serviceContent', 0, 100, 500);
			currentPage = 1;
			if (data["Pagination"] == null) {
				// do nothing
			} else {
				$('pagination').innerHTML = data["Pagination"];
			}
		},
		onFailure: function(transport) {alert(transport.responseText);}
	});
}

function GetExperience (script) {
	url = script;
	
	ajx = new Ajax.Request(url,
	{
		onSuccess: function(transport) {
			// $('serviceContent').innerHTML = transport.responseText;
			eval ("data=" + transport.responseText);
			
			$('thePhoto').src = "http://www.graydotdev1.com/php/IMAGES/STUDIO_v2_Asset_Library/" + data["Photo"] + "---Image.jpg";
			$('title').innerHTML = data["Title"];
			$('strapLine').innerHTML = data["StrapLine"];
			
			for (i = 0; i < data["Content"].length; i++) {
				if (i == 0) {
					resultString = data["Content"][i];
				} else {
					resultString += data["Content"][i];
				}
			}
			$('content_block').innerHTML = resultString;
			opacity('serviceContent', 0, 100, 500);
			currentPage = 1;
			if (data["Pagination"] == null) {
				// do nothing
			} else {
				$('pagination').innerHTML = data["Pagination"];
			}
		},
		onFailure: function(transport) {alert("Fail");}
	});
}

function ChangeSelectedItem (obj) {	
	if (last_selected) {
        $(last_selected).className = 'item';
        obj.className = 'item_selected';
        last_selected = obj;
    } else {
        $('service_item_0').className = 'item';
        obj.className = 'item_selected';
        last_selected = obj;
    }
}

function ChangeSelectedItem2 (obj) {
	if (last_selected) {
		var tester = last_selected;
        $(last_selected).className = 'item';
        obj.className = 'item_selected';
		
		// $(last_selected).childNodes[0].src = $(last_selected).childNodes[0].src.replace("_active.png", ".png");
		$(last_selected).childNodes[0].src = $(last_selected).childNodes[0].src.replace("_on.png", ".png");
		$(last_selected).childNodes[0].onmouseover = function() { 
			$(tester).childNodes[0].src = $(tester).childNodes[0].src.replace(".png", "_on.png");
		};
		$(last_selected).childNodes[0].onmouseout = function() { 
			$(tester).childNodes[0].src = $(tester).childNodes[0].src.replace("_on.png", ".png");
		};
		
		// $(obj).childNodes[0].src = $(obj).childNodes[0].src.replace("_on.png", "_active.png");
		// alert ($(obj).childNodes[0].src);
		//$(obj).childNodes[0].src = $(obj).childNodes[0].src.replace(".png", "_on.png");
		$(obj).childNodes[0].onmouseover = function() { 
			// nothing
		};
		$(obj).childNodes[0].onmouseout = function() { 
			// nothing
		};
        last_selected = obj;
    } else {
        $('service_item_0').className = 'item';
		// $('service_item_0').childNodes[0].src = $(obj).childNodes[0].src.replace(".png", "_active.png");
		$('service_item_0').childNodes[0].src = $(obj).childNodes[0].src.replace(".png", "_on.png");
        obj.className = 'item_selected';
        last_selected = obj;
    }
}

var isChangeable = 1;
function changeExperienceType(obj) {
	if (isChangeable == 1) {
		isChangeable = 0;
		if (curr == obj.id) {
			// do nothing
		} else {
			$(curr+'_list').style.display = 'none';
			$(curr+'_container').style.height = 2+'px';
			$(curr).className = 'ExperienceheaderOff';
			$(curr+'_img').src = 'images/global/side_arrow.png';
		}
	
		$(obj.id+'_list').style.display = 'block';
		$(obj.id).className = 'ExperienceheaderOn';
	    $(obj.id+'_img').src = 'images/global/down_arrow.png';
		scrollInterval = setInterval ('ExpandDown(\'' + obj.id + '\', ' + parseInt ($(obj.id+'_list').offsetHeight) + ')', 10);
		curr = obj.id;
	}
}

function ExpandDown (obj, height) {
	if (parseInt ($(obj+'_container').style.height) < height) {
		$(obj+'_container').style.height = parseInt ($(obj+'_container').style.height) + 5 + 'px';
	} else {
		clearInterval(scrollInterval);
		isChangeable = 1;
	}
}

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    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++;
        }
    }
}

// Change Opacity for all browsers:
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 SwitchImage (image_id, image2) {
	document.getElementById(image_id).src = image2;
}

function lbRedirect () {
	setTimeout("location.href='http://www.luxury-branding.com/';", 10000);
}