function getXMLHttp()
{
	var XMLHttp = null;
	if (window.XMLHttpRequest) {
    try {
    XMLHttp = new XMLHttpRequest();
    } catch (e) { }
	} else if (window.ActiveXObject) {
    try {
    XMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try {
    XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e) { }
    }
	}
	return XMLHttp;
}
var xmlhttp = getXMLHttp()


function givemeimagepath(razdel,id) 
{
	var request="id="+id;
	xmlhttp.open('POST', '/im.php', true);
	xmlhttp.onreadystatechange = function() 
	{
	if (xmlhttp.readyState == 4) 
	{
	if(xmlhttp.status == 200) 
	{
	var arrr = xmlhttp.responseText.split("splitter");
	sbp2('images/'+arrr[0]+'.jpeg',razdel,id,arrr[1]);
	}
	}
	};
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(request);
}

function givemevideopath(id)
{
	var request="id="+id;
	xmlhttp.open('POST', '/vid.php', true);
	xmlhttp.onreadystatechange = function() 
	{
	if (xmlhttp.readyState == 4) 
	{
	if(xmlhttp.status == 200) 
	{
	var path = xmlhttp.responseText;
	show_video('video',path,id)
	}
	}
	};
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(request);
}

function closevideo()
{
	$('#videocontainer').hide();
	$('#videodivop').animate({height: '1px'},200,"linear",function(){
	$('#videodivop').hide();
	var link1 = window.location.href.split("#");
	var link2 = link1[1].split("-");
	window.location = '/#'+link2[0];
	document.title = 'VIDEO';
	$('#videodivop, #videocontainer, #videoinfo').remove();
	play();
	});
}




function show_video(razdel,path,id)
{
	//var videofile=path.replace(".jpeg",".flv");
	window.location='/#'+razdel+'-'+id;
	document.title='VIDEO';
	pause();
	
	var videodivop = document.createElement('div');
	videodivop.id = 'videodivop';
	videodivop.className = 'videodivop';
	videodivop.setAttribute("onclick","closevideo();");
	videodivop.onclick = function(){closevideo();};
	document.body.appendChild(videodivop); 

	$('#videodivop').animate({height: '100%'},500,"linear",function(){

	var videocontainer = document.createElement('div');
	videocontainer.id = 'videocontainer';
	videocontainer.className = 'videocontainer';
	videocontainer.innerHTML = path;
	document.body.appendChild(videocontainer); 
	var wi = /width="([0-9]{1,4})"/i
	var he = /height="([0-9]{1,4})"/i
	var mywi = path.match(wi);
	var myhe = path.match(he);
	var ml = mywi[1]/2;
	var mt = myhe[1]/2;
	//alert(mywi[1]+' - '+myhe[1]);
	$('#videocontainer').css("width",mywi[1]+"px");
	$('#videocontainer').css("height",myhe[1]+"px");
	$('#videocontainer').css("marginLeft","-"+ml+"px");
	$('#videocontainer').css("marginTop","-"+mt+"px");

	

		
	var videoinfo = document.createElement('div');
	videoinfo.id = 'videoinfo';
	videoinfo.className = 'videoinfo';
	videoinfo.innerHTML = '<div class="return" onclick="closevideo();"><span>Close</span></div>';
	document.getElementById('videocontainer').appendChild(videoinfo); 
	$('#videoinfo').css("width",mywi[1]+"px");

	$('.return').hover(function(){
	$('.return').animate({opacity:"1"}, 300 );
	},
	function(){
	$('.return').animate({opacity:"0.3"}, 300 );
	}
	)
	})
}


function closeoverlay()
{
	$('#myoverlaydivloading').hide();
	$('#mydivforimagestuff').hide();
	$('#myoverlaydiv').fadeOut(500,function(){
	$('#myoverlaydiv').hide();
	var link1 = window.location.href.split("#");
	var link2 = link1[1].split("-");
	window.location = '/#'+link2[0];
	document.title = link2[0].toUpperCase();
	$('#myoverlaydiv, #myoverlaydivloading, #mydivforimagestuff, #mypicname, #mypicnameop, #mysharebuttondiv, #mybigimage, #returnbutton, #previmage, #nextimage').remove();
	});
}



function myimageload(w,h)
{
//alert(w+' '+h);
if(w=='0')
	{
		$('#mydivforimagestuff').css("width","10px");
		$('#mydivforimagestuff').css("height","10px");
		$('#mydivforimagestuff').css("overflow","hidden");
		$('#mydivforimagestuff').css("visibility","hidden");
		$('#mydivforimagestuff').css("display","block");

		w = $('#mybigimage').width();
		h = $('#mybigimage').height();
		
		$('#mydivforimagestuff').css("overflow","visible");		
		$('#mydivforimagestuff').css("display","none");
		$('#mydivforimagestuff').css("visibility","hidden");
		//alert(w+' '+h);
	}


	$('#mybigimage').css("visibility","hidden");
	var windowwidth = document.body.clientWidth;
	var windowheight = document.body.clientHeight;
	var mybigimagemaxwidth = windowwidth-70;
	var mybigimagemaxheight = windowheight-10;
	if(w>mybigimagemaxwidth)
	{
		mywidth = mybigimagemaxwidth;
	}
	else
	{
		mywidth = w;
	}
	
	
	if(h>mybigimagemaxheight)
	{
		myheight = mybigimagemaxheight;
	}
	else
	{
		myheight = h;
	}
	
		
	ratio = w/mywidth;
	mynewheight = Math.round(myheight/ratio);
	
	ratio2 = h/myheight;
	mynewwidth = Math.round(mywidth/ratio2);
	
	$('#mybigimage').attr("Width",mynewwidth+"px");
	$('#mybigimage').attr("Height",mynewheight+"px");
	$('#mybigimage').css("width",mynewwidth+"px");
	$('#mybigimage').css("height",mynewheight+"px");
	
	$('#mydivforimagestuff').css("width",mynewwidth+"px");
	$('#mydivforimagestuff').css("height",mynewheight+"px");
		
	$('#mydivforimagestuff').css("marginLeft","-"+mynewwidth/2+"px");
	$('#mydivforimagestuff').css("marginTop","-"+mynewheight/2+"px");
	
	$('#mydivforimagestuff').css("display","block");
	$("#mybigimage, #previmage, #nextimage, #myiframe, #mysharebuttondiv, #returnbuttondiv, #mypicnameop, #mypicname, #mysharebuttondiv").css("visibility","visible");
	
}




function lj()
{
var imgsrc = $('#mybigimage').attr("src");
var host = window.location.href.split("#");
var request = host[1].split("-");
var subject = encodeURIComponent(document.title);
var event = escape('<a href="'+host[0]+'b.php?r='+request[0]+'&id='+request[1]+'"><img src="'+host[0]+imgsrc+'"></a>');
var str = 'http://www.livejournal.com/update.bml?subject='+subject+'&event='+event;
window.open(str,'_blank');
}


function recheck()
{
var dddd = $("#myiframe").contents();
dddd.find("#atic_livejournal").unbind("click").removeAttr('onclick');
dddd.find("#atic_livejournal").bind("click",function(){ lj();});
}

function dosomething()
{
$("#myiframe").css("visibility","hidden");
var iFrameDOM = $("#myiframe").contents();

//iFrameDOM.find(".addthis_button_compact").bind("mouseover",function(){ setTimeout("recheck();","500");});
iFrameDOM.find("#addthisbuttondivimage1").bind("mouseover",function(){ setTimeout("recheck();","500");});


iFrameDOM.find("span").css("backgroundImage",'url(/share.png)');
//iFrameDOM.find(".atic_livejournal").remove();
iFrameDOM.find(".addthis_button_compact").css("fontFamily","tahoma");
iFrameDOM.find(".addthis_button_compact").css("fontSize","10px");
iFrameDOM.find(".addthis_button_compact").css("color","#fff");
iFrameDOM.find(".addthis_button_compact").css("fontWeight","normal");
iFrameDOM.find(".addthis_button_compact").css("lineHeight","20px");
iFrameDOM.find(".addthis_button_compact").css("textDecoration","none");
iFrameDOM.find(".addthis_button_compact").css("textTransform","lowercase");
iFrameDOM.find("span").after("<div style=\"float: left; border: solid 0px red; height: 20px; width: 15px; background: url('/css/share.png') left center no-repeat;\"></div>");
$("#myiframe").css("visibility","visible");
}



function sbp2(path,razdel,id,picname)
{
	window.location='/#'+razdel+'-'+id;
	document.title=picname;
	var myoverlaydiv = document.createElement('div');
	myoverlaydiv.id = 'myoverlaydiv';
	myoverlaydiv.className = 'myoverlaydiv';
	myoverlaydiv.setAttribute("onclick","closeoverlay();");
	myoverlaydiv.onclick = function(){closeoverlay();};
	document.body.appendChild(myoverlaydiv); 

	$('#myoverlaydiv').fadeIn(500,function(){

	var myoverlaydivloading = document.createElement('div');
	myoverlaydivloading.id = 'myoverlaydivloading';
	myoverlaydivloading.className = 'myoverlaydivloading';
	myoverlaydivloading.innerHTML = '<div></div>';
	document.body.appendChild(myoverlaydivloading); 


	var mydivforimagestuff = document.createElement('div');
	mydivforimagestuff.id = 'mydivforimagestuff';
	mydivforimagestuff.className = 'mydivforimagestuff';
	document.body.appendChild(mydivforimagestuff); 

	var mypicname = document.createElement('div');
	mypicname.id = 'mypicname';
	mypicname.className = 'mypicname';
	mypicname.innerHTML = '<span>'+picname+'</span>';
	document.getElementById('mydivforimagestuff').appendChild(mypicname); 

	var mypicnameop = document.createElement('div');
	mypicnameop.id = 'mypicnameop';
	mypicnameop.className = 'mypicnameop';
	document.getElementById('mydivforimagestuff').appendChild(mypicnameop); 

	var mysharebuttondiv = document.createElement('div');
	mysharebuttondiv.id = 'mysharebuttondiv';
	mysharebuttondiv.className = 'mysharebuttondiv';
	mysharebuttondiv.innerHTML = '<iframe onload="dosomething();" id="myiframe" class="myiframe" allowtransparency="true" style="background-color:transparent" src="/b.php?r='+razdel+'&id='+id+'"></iframe>';
	document.getElementById('mydivforimagestuff').appendChild(mysharebuttondiv); 
	
	
	var nextimage = document.createElement('div');
	nextimage.id = 'nextimage';
	nextimage.className = 'nextimage';
	document.getElementById('mydivforimagestuff').appendChild(nextimage); 
	
	var previmage = document.createElement('div');
	previmage.id = 'previmage';
	previmage.className = 'previmage';
	document.getElementById('mydivforimagestuff').appendChild(previmage); 


	var mybigimage = document.createElement('img');
	mybigimage.id = 'mybigimage';
	mybigimage.className = 'mybigimage';
	mybigimage.src = path;
	mybigimage.onload = function(){myimageload(this.width,this.height); };
	//mybigimage.setAttribute("onload","myimageload(this.width,this.height);");
	document.getElementById('mydivforimagestuff').appendChild(mybigimage);
	
	var returnbutton = document.createElement('div');
	returnbutton.id = 'returnbuttondiv';
	returnbutton.className = 'returnbuttondiv';
	returnbutton.innerHTML = '<div class="returnbutton" onclick="closeoverlay();"><span>Close</span></div>';
	document.getElementById('mydivforimagestuff').appendChild(returnbutton); 
	
	
	
	
	var mypath = path.replace(".jpeg","-s.jpeg");
	var myid = id;
	var mypicname = picname;
	
	if(razdel==='advertising') { var myimagesarray = advertisingimages;}
	if(razdel==='fashion') { var myimagesarray = fashionimages;}
	if(razdel==='beauty') { var myimagesarray = beautyimages;}
	if(razdel==='portraits') { var myimagesarray = portraitsimages;}
	if(razdel==='still_life') { var myimagesarray = still_lifeimages;}
	
	$('#nextimage').click(function()
	{
	$("#previmage, #nextimage, #myiframe, #mysharebuttondiv, #returnbuttondiv, #mypicnameop, #mypicname, #mysharebuttondiv").css("visibility","hidden");
	var str = mypath+'|'+myid+'|'+mypicname;
		
	//var pos = myimagesarray.indexOf(str);
	var pos = jQuery.inArray(str, myimagesarray);
	
	var l = myimagesarray.length-1;
	if(pos===l)
	{
	pos = -1;
	}
	var newpath = myimagesarray[pos+1].split("|");
	var mynewpath = newpath[0].replace("-s.jpeg",".jpeg");
	$('#mypicname span').text(newpath[2]);
	document.title = newpath[2];
	window.location = '/#'+razdel+'-'+newpath[1];
	
	
	$('#mybigimage').remove();
	var mybigimage = document.createElement('img');
	mybigimage.id = 'mybigimage';
	mybigimage.className = 'mybigimage';
	
	mybigimage.src = mynewpath+'#'+Math.random();
	//mybigimage.setAttribute("onload","myimageload(this.width,this.height);");
	mybigimage.onload = function(){myimageload(this.width,this.height); };
	
	document.getElementById('mydivforimagestuff').appendChild(mybigimage);
	$('#mybigimage').css("visibility","hidden");
	$('#mybigimage').css("display","none");
	$('#mybigimage').load(function(){
	$('#mybigimage').css("display","block");
	$('#mybigimage').css("visibility","visible");
		
	mypath = mynewpath.replace(".jpeg","-s.jpeg");
	myid = newpath[1];
	mypicname = newpath[2];
	
	$('#mysharebuttondiv').remove();
	var mysharebuttondiv = document.createElement('div');
	mysharebuttondiv.id = 'mysharebuttondiv';
	mysharebuttondiv.className = 'mysharebuttondiv';
	mysharebuttondiv.innerHTML = '<iframe onload="dosomething();" id="myiframe" class="myiframe" allowtransparency="true" style="background-color:transparent" src="/b.php?r='+razdel+'&id='+myid+'"></iframe>';
	document.getElementById('mydivforimagestuff').appendChild(mysharebuttondiv); 
	
	})
	})
	
	
	
	
	
	
	$('#previmage').click(function()
	{
	$("#previmage, #nextimage, #myiframe, #mysharebuttondiv, #returnbuttondiv, #mypicnameop, #mypicname, #mysharebuttondiv").css("visibility","hidden");
	var str = mypath+'|'+myid+'|'+mypicname;
	//var pos = myimagesarray.indexOf(str);
	var pos = jQuery.inArray(str, myimagesarray);
	
	if(pos===0)
	{
	pos = myimagesarray.length;
	}
	
	
	var newpath = myimagesarray[pos-1].split("|");
	var mynewpath = newpath[0].replace("-s.jpeg",".jpeg");
	$('#mypicname span').text(newpath[2]);
	document.title = newpath[2];
	window.location = '/#'+razdel+'-'+newpath[1];
		
	$('#mybigimage').remove();
	var mybigimage = document.createElement('img');
	mybigimage.id = 'mybigimage';
	mybigimage.className = 'mybigimage';
	mybigimage.src = mynewpath+'#'+Math.random();
	//mybigimage.setAttribute("onload","myimageload(this.width,this.height);");
	mybigimage.onload = function(){myimageload(this.width,this.height); };
	document.getElementById('mydivforimagestuff').appendChild(mybigimage);
	$('#mybigimage').css("visibility","hidden");
	$('#mybigimage').css("display","none");
	$('#mybigimage').load(function(){
	$('#mybigimage').css("display","block");
	$('#mybigimage').css("visibility","visible");
			
	mypath = mynewpath.replace(".jpeg","-s.jpeg");
	myid = newpath[1];
	mypicname = newpath[2];
	
	$('#mysharebuttondiv').remove();
	var mysharebuttondiv = document.createElement('div');
	mysharebuttondiv.id = 'mysharebuttondiv';
	mysharebuttondiv.className = 'mysharebuttondiv';
	mysharebuttondiv.innerHTML = '<iframe onload="dosomething();" id="myiframe" class="myiframe" allowtransparency="true" style="background-color:transparent" src="/b.php?r='+razdel+'&id='+myid+'"></iframe>';
	document.getElementById('mydivforimagestuff').appendChild(mysharebuttondiv); 
	
	$("#myiframe").css("display","block");
	})
	})
	
	
	$('#nextimage, #previmage').hover(function(){
	$(this).animate({opacity:"1"}, 300 );
	},
	function(){
	$(this).animate({opacity:"0.2"}, 300 );
	}
	)
	
	
	
	
	$('.returnbutton').hover(function(){
	$('.returnbutton').animate({opacity:"1"}, 300 );
	},
	function(){
	$('.returnbutton').animate({opacity:"0.3"}, 300 );
	}
	)
	});
	
}
