var http = getXMLHTTPRequest();

function getXMLHTTPRequest() {
	try {
		req = new XMLHttpRequest();
	} catch(err1) {
		try {
			req = new ActiveXObject("Msxm12.XMLHTTP");
		} catch (err2) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (err3) {
				req = false;
			}
		}
	}
	return req;
}

function addListeners() {
    
	var boot = document.getElementById("thumbs");
	if(null === boot){ } else {
	var all = boot.getElementsByTagName("span");
	for (var i=0;i<all.length;i++){
		all[i].onclick = galleryMajorChange;
		all[i].onmouseover = thumbGrey;
		all[i].onmouseout = thumbGrey;
	}
	}
	
	var boot = document.getElementById("articleThumbs");
	if(null === boot){ } else {
	var all = boot.getElementsByTagName("span");
	for (var i=0;i<all.length;i++){
		all[i].onclick = galleryMajorChange2;
		all[i].onmouseover = thumbGrey;
		all[i].onmouseout = thumbGrey;
	}
	}
	
	var frontpage = document.getElementById("features");
	if(null === frontpage){ } else {
		
	var adam = frontpage.getElementsByTagName("div");
	
	for(var i=0;i<adam.length;i++){
		
			adam[i].onmouseover = highlight;
			adam[i].onmouseout = highlight;
			adam[i].onclick = featureVisit;
 
		}

}

	var frontpage = document.getElementById("contact");
	if(null === frontpage){ } else {
		
	var adam = frontpage.getElementsByTagName("div");
	
	
	for(var i=0;i<adam.length;i++){
		
			adam[i].onmouseover = highlight;
			adam[i].onmouseout = highlight;
			adam[i].onclick = contactUs;
 
		}

}

	
	//var checkTrolley = document.getElementById("trolleyReview");
	//if(null === checkTrolley){
	//var run = document.getElementById("galleryBRinner");
	//var full = run.getElementsByTagName("div");
	//for (var i=0;i<full.length;i++){
	//	full[i].onmouseover = galleryHover;
	//	full[i].onmouseout = galleryHover;
	//}
	//}
	//else {
		
	//}

	document.getElementById("javascriptWarning").style.display = 'none';

}

articleImageA = 1;
hold = 'false';

function changeArticleImage(){
	if(hold === 'adam'){
		hold = 'false';
		setTimeout('changeArticleImage()',5000);
	}
	else {
	if(articleImageA === 5){ articleImageA = 1 }
	var id = articleImageA;
	var negative = id-1;
	var negative = negative*290;
	change = '-'+negative+"px 0px 0px 0px";
	document.getElementById('galleryMajor').style.margin=change;
	var upper = 'galleryMajor2';
	var fxBlind=new Spry.Effect.Fade(upper,{duration:500,from:'100%',to:0,toggle:false,horizontal:true,finish:resetArticleImage});
	fxBlind.start();
	}

}

function resetArticleImage(){
	
	document.getElementById('galleryMajor2').style.margin=change;
	var upper = 'galleryMajor2';
	var fxBlind=new Spry.Effect.Fade(upper,{duration:5,from:0,to:100,toggle:false,horizontal:true});
	fxBlind.start();
	articleImageA = articleImageA+1;
	setTimeout('changeArticleImage()',5000);
	
}

function highlight(e){
	var id = this.id;
	var highlight = document.getElementById(id);
	if(highlight.className == 'majorFeature'){ document.getElementById(id).setAttribute("class",'HmajorFeature'); }
	else if(highlight.className == 'HmajorFeature'){ document.getElementById(id).setAttribute("class",'majorFeature'); }
	else if(highlight.className == 'HminorFeature'){ document.getElementById(id).setAttribute("class",'minorFeature'); }
	else if(highlight.className == 'minorFeature'){ document.getElementById(id).setAttribute("class",'HminorFeature'); }

}

function featureVisit(e){

	var id = this.id;
	window.location = 'event.php?id='+id;
	
}

function contactUs(e){
	
	var id = this.id;
	if(id == 'emailUs'){
		window.location = 'mailto:create@inverve.co.uk';
	}
	else if ( id == 'twitterUs'){
		window.location = 'http://www.twitter.com/inverve'; 
	}
}

function galleryMajorChange(e){

	var id = this.id;
	var offset = 290*(id-1);
	var changeTo = '-'+offset+'px 0px 0px 0px';
	document.getElementById('galleryMajor').style.margin = changeTo;
	
}

function galleryMajorChange2(e){
	
	articleImageA = (parseInt(this.id))+1;
	
	hold = 'adam';

	var id = this.id;
	var offset = 290*(id-1);
	var changeTo = '-'+offset+'px 0px 0px 0px';
	document.getElementById('galleryMajor2').style.margin = changeTo;
	
}

function thumbGrey(e){

	var id = 'thumbGrey'+this.id;
	if(document.getElementById(id).style.display == 'inline'){ document.getElementById(id).style.display = 'none'; }
	else { document.getElementById(id).style.display = 'inline' }
	
}

function galleryHover(e){
	var hover = this.id;
	var currentStyle = document.getElementById(hover).className;
	if(currentStyle == 'foto'){
	document.getElementById(hover).setAttribute("class",'fotoSelected');
	}
	else {
	document.getElementById(hover).setAttribute("class",'foto');
	}
}

function bwcChange(e){
	
	var id = this.id;
	var pid=this.parentNode.id;
	var change = 'photo_'+pid;
	
	if(id=='color'){ 
		var changeTo = '0px 0px 0px 0px'; window['selected' + pid] = 'color'; 
		}
		
	else if(id=='bw'){ 
		var changeTo = '0px 0px 0px -114px'; window['selected' + pid] = 'bw';
		}
	
	else if(id=='buy'){ 
		
		if (typeof window['selected'+pid]=="undefined"){
			var typeIs = 'std';
		}
		else {			
			var typeIs = window['selected'+pid]; 
		}
		
	document.getElementById(pid).setAttribute("class",'fotoSelected');
	addPicture(pid,typeIs);	
	
		}
	
	document.getElementById(change).style.margin=changeTo;

}
            
window.addEventListener('load', addListeners, false);

venueCount = 1;
numberCount = 0;

function addVenue(){
	
	venueCount = venueCount+1;
	var currentVenue = venueCount;
	var newVenue = 'venue'+currentVenue;
	document.getElementById(newVenue).setAttribute("class",'visible');

}

function addNumber(){
	
	numberCount = numberCount+1;
	var currentNumber = numberCount;
	var newNumber = 'number'+currentNumber;
	document.getElementById(newNumber).setAttribute("class",'visible');

}

function facebook_onlogin(){
	document.getElementById('facebookLogin').innerHTML = '<p>Thank you for connecting, you will now be able to publish your favorite pictures straight to your Facebook wall!';	
	
}

currentpagemargin = 0;

function pagenext(){
	
		currentpagemargin = currentpagemargin - 600;	
		document.getElementById('bookPage').style.marginTop = currentpagemargin + 'px';
		
}

function pageprevious(){

		if(currentpagemargin === 0){ }

		else {

			currentpagemargin = currentpagemargin + 600;
			document.getElementById('bookPage').style.marginTop = currentpagemargin + 'px';

		}
	
}

function add(){
	
	document.getElementById('olderFeatures').style.display = 'inline';
	
}

function doone(){
	
	alert('why?');
	
}

trolleyNumber = 0;

function addchosen(uid){
	
		var uid = uid;
		var nowshow = 'chosen_'+uid;
		var notification = uid+'_notification';
	
		
		if(window['prechosen' + uid] == 'chosen'){ }
		
		else {
		
		document.getElementById('chosenA_'+uid).style.display='none';
		document.getElementById('chosenB_'+uid).style.display='inline';
		
		var myurl = "ajax/addPicture.php";
		var rand = parseInt(Math.random()*999999999);
		var myurl = myurl+"?rand="+rand;
		var params = "uid="+uid;

		document.getElementById('imageCount').value = parseInt(document.getElementById('imageCount').value)+1;
		var currentCount = document.getElementById('imageCount').value;

		document.getElementById('trolleyNumber').innerHTML = currentCount+' images @ £0.99';
		document.getElementById('trolleyTotal').innerHTML = 'Total £'+Math.round((currentCount*.99)*100)/100;
		http.open("POST",myurl,true);
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
		http.onreadystatechange = function(){ if(http.readyState==4){ if(http.status==200){ added(); } } };
		http.send(params);
		
		window['prechosen' + uid] = 'chosen';
		
		}
		
}

function added(){
		
}
