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";
            
            if (IEflag) {
                var img = new Image();
                img.src = "images/" + data["Capacity"].replace (" ", "_").toLowerCase() + ".png";
                $('capacityImage').style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'images/" + data["Capacity"].replace (" ", "_").toLowerCase() + ".png\', sizingMethod='scale')";
                $('capacityImage').style.width = img.width;
            } else {
                $('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 GetPublication (script) {
	url = script;

	ajx = new Ajax.Request(url,
	{
		onSuccess: function(transport) {
			// $('serviceContent').innerHTML = transport.responseText;
			eval ("data=" + transport.responseText);
			
			$('previewPhoto').src = ""+data["Photo"];
                        
                        $('contentText').innerHTML = data["Content"];
                        $('title').innerHTML = data["Page Title"];
                        
                        $('downloadLink').href = ""+data["Link"];
                        $('downloadLink2').href = ""+data["Link"];
                        
			opacity('serviceContent', 0, 100, 500);
		},
		onFailure: function(transport) {alert(transport.responseText);}
	});
}

function GetFilm (script) {
	url = script;

	ajx = new Ajax.Request(url,
	{
		onSuccess: function(transport) {
			// $('serviceContent').innerHTML = transport.responseText;
			eval ("data=" + transport.responseText);
			
                        $('title').innerHTML = ""+data["Page Title"];
                        $('contentText').innerHTML = ""+data["Content"];
                        $('video').innerHTML = ""+data["Youtube Embed Code"];
                        
			opacity('serviceContent', 0, 100, 500);
		},
		onFailure: function(transport) {alert(transport.responseText);}
	});
}

function GetNews (script, contentResults, contentBanner, contentBannerOriginal) {
	url = script;

	ajx = new Ajax.Request(url,
	{
		onSuccess: function(transport) {
			// $('serviceContent').innerHTML = transport.responseText;
			eval ("data=" + transport.responseText);
			
                        $(contentResults).innerHTML = ""+data["Content"];
                        
                        $(contentBanner).style.display= "block";
                        $(contentBannerOriginal).style.display= "none";
                        $(contentBanner).innerHTML = ""+data["Banner"];
                        
			opacity('serviceContent', 0, 100, 500);
		},
		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);
			
			if ($('thePhoto')) {
                                $('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, isImg) {
        var IEflag = true;
        
        if ($(obj).childNodes[0].src) {
                IEflag = false
        }
    
        if (last_selected) {
		var tester = last_selected;
                $(last_selected).className = 'item';
                obj.className = 'item_selected';
                
                if (isImg != "no") { // If we are using images and then the image src needs to change too
                        if (IEflag) {
                                $(last_selected).childNodes[0].style.filter = $(last_selected).childNodes[0].style.filter.replace("_on.png", ".png");
                        } else {
                                $(last_selected).childNodes[0].src = $(last_selected).childNodes[0].src.replace("_on.png", ".png");
                        }
                        
                        $(last_selected).childNodes[0].onmouseover = function() { 
                                
                                if (IEflag) {
                                        $(tester).childNodes[0].style.filter = $(tester).childNodes[0].style.filter.replace(".png", "_on.png");
                                } else {
                                        $(tester).childNodes[0].src = $(tester).childNodes[0].src.replace(".png", "_on.png");
                                }
                        };
                        $(last_selected).childNodes[0].onmouseout = function() { 
                    
                                if (IEflag) {
                                        $(tester).childNodes[0].style.filter = $(tester).childNodes[0].style.filter.replace("_on.png", ".png");
                                } else {
                                        $(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';
		
                if (isImg != "no") { // If we are using images and then the image src needs to change too
                        if (IEflag) {
                                $('service_item_0').childNodes[0].style.filter = $(obj).childNodes[0].style.filter.replace(".png", "_on.png");
                        } else {
                                $('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.visibility = 'hidden';
			$(curr+'_container').style.height = 0+'px';
			$(curr).className = 'ExperienceheaderOff';
			$(curr+'_img').src = 'images/global/side_arrow.png';
		}
	
		$(obj.id+'_list').style.visibility = 'visible';
		$(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;
}