// JavaScript Document


i = 0;
deg2rad = Math.PI * 2 / 360;

function setRotation(obj, deg) {

    rad = deg * deg2rad;

    costheta = Math.cos(rad);
    sintheta = Math.sin(rad);
	try {
		if (editmode!='content') {
			obj.filters.item(0).M11 = costheta;
    		obj.filters.item(0).M12 = -sintheta;
    		obj.filters.item(0).M21 = sintheta;
    		obj.filters.item(0).M22 = costheta;
		}
		else {
			//$('body').addClass('norotation');
		}

	}
	catch(err) {
		//$('body').addClass('norotation');
	}

 	//obj.style.visibility='visible';
}
var pictureZindex = 2;
var bwidth;

function getStyle(el,styleProp)
{
/*
	if (el.currentStyle)
		var y = el.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
	*/
	var y = el.style[styleProp];

	y = parseInt(y);
	if (isNaN(y)) y=0;
	return y;
	is_int
}


function showPicture(obj) {
	//obj.id=pile_img_id;
	imgobj=document.getElementById(obj.getAttribute('imgid'));
	obj.style.left=20+Math.round(Math.random()*30)+'px';
	obj.style.top=30+Math.round(Math.random()*30)+'px';
	var rotation = (Math.round(Math.random()*10)%2)?0-Math.random()*5:Math.random()*5;

	obj.rotation = rotation;


	obj.origwidth = getW(imgobj)-20;
	obj.origheight = getH(imgobj)-20;

}

var ie=document.all;
var nn6=document.getElementById&&!document.all;

var isdrag=false;
var x,y;
var dobj;
var noZone=false;
var dragBalk=false;
var imgobj;

function movemouse(e)
{
  if (isdrag)
  {
	try {
	    var currentmousex = nn6 ? e.clientX  : event.clientX;

		var ratio = (currentmousex+40)/dobj.startPos
	    // var ratio = (bwidth - dobj.startPos)/(bwidth-currentmousex);
		if (ratio>1) ratio=1;


	  	dobj.style.left = (nn6 ? tx + e.clientX - x : tx + event.clientX - x) + 'px';
	    dobj.style.top  = (nn6 ? ty + e.clientY - y : ty + event.clientY - y) + 'px';

		if (dobj.origwidth<30) {
			dobj.origwidth = getW(imgobj)-20;
			dobj.origheight = getH(imgobj-20);

		}

		// document.getElementById('contenttitle').innerHTML=bwidth+": "+ratio;
		imgobj.style.width=(ratio*dobj.origwidth)+'px';
		imgobj.style.height=(ratio*dobj.origheight)+'px';
		dobj.style.marginLeft=dobj.dx-(dobj.dx*ratio)+'px';
		dobj.style.marginTop=dobj.dy-(dobj.dy*ratio)+'px';
		// document.getElementById('contenttitle').innerHTML=dobj.style.;
		imgobj.style.borderWidth=ratio*10+'px';

	    return false;
	}
	catch(err) {

	}
  }
}

function selectmouse(e)
{
  var fobj       = nn6 ? e.target : event.srcElement;
  var topelement = "HTML";


  while (fobj.tagName != topelement && !fobj.className.match(/picture/))
  {
    fobj = nn6 ? fobj.parentNode : fobj.parentElement;
  }

  if (fobj.className.match(/picture/) && !noZone)
  {
  	imgobj=document.getElementById(fobj.getAttribute('imgid'));

	x = nn6 ? e.clientX : event.clientX;
    y = nn6 ? e.clientY : event.clientY;
  	if (!fobj.startPos) fobj.startPos=getX(fobj);
	pictureZindex++;
	//document.getElementById('contenttitle').innerHTML=fobj.origwidth;
	if (fobj.origwidth<30) {
		fobj.origwidth = getW(imgobj)-20;
		fobj.origheight = getH(imgobj-20);

	}

	fobj.style.zIndex=pictureZindex;
	if (!fobj.dx) fobj.dx=x-getX(fobj);
	if (!fobj.dy) fobj.dy=y-getY(fobj);

    isdrag = true;
    dobj = fobj;
    var margintop = getStyle(dobj,'marginTop');
    var marginleft = getStyle(dobj,'marginLeft');



    tx = parseInt(parseInt(dobj.offsetLeft)-marginleft);
    ty = parseInt(parseInt(dobj.offsetTop)-margintop);

   // document.getElementById('contenttitle').innerHTML=margintop+' '+marginleft;

    document.onmousemove=movemouse;
    return false;
  }
}

document.onmousedown=selectmouse;
document.onmouseup=new Function("isdrag=false");

function createPictureOnPile(obj, imgid,realclick) {
	var new_pile_img_id = 'img_'+obj.id;

	pictureZindex++;


	var tmpimgid = $('img',obj).attr('src');

	if (typeof imgarr[tmpimgid]!='undefined') {

		new_pile_img_id = imgarr[tmpimgid];

	}

	var newimgobj = document.getElementById(new_pile_img_id);
	if (!newimgobj) {

		$('#pictures').append('<div imgid="'+imgid+'" class="picture" id="'+new_pile_img_id+'"><img id="'+imgid+'" src="'+$('img',obj).attr('src')+'"/></div>');
		var newimgobj = document.getElementById(new_pile_img_id);
		imgarr[$('img',obj).attr('src')]=new_pile_img_id;
		if (newimgobj) {
			showPicture(newimgobj);
			newimgobj.style.zIndex=pictureZindex;

		}
	}
	else {

		newimgobj.style.zIndex=pictureZindex;

		if (realclick) {
			if (newimgobj.origwidth<30 || newimgobj.origheight<30) {
				imgobj=document.getElementById(newimgobj.getAttribute('imgid'));

				newimgobj.origwidth = getW(imgobj)-20;
				newimgobj.origheight = getH(imgobj)-20;

			}


			var newleft=20+Math.round(Math.random()*30)+'px';
			var newtop=30+Math.round(Math.random()*30)+'px';
			$(newimgobj).animate({left: newleft, top: newtop, marginLeft: '0px', marginTop: '0px'},500,'easeOutExpo');
			if (newimgobj && newimgobj.origheight) {
				$('img',newimgobj).animate({width: newimgobj.origwidth+'px', height: newimgobj.origheight+'px'},500,'easeOutExpo');
			}
			else {
				$('img',newimgobj).css('width',(newimgobj.origwidth)+'px');
				$('img',newimgobj).css('height',(newimgobj.origheight)+'px');
			}


			//$(newimgobj).css('marginLeft','0px');
			//$(newimgobj).css('marginTop','0px');
			$('img',newimgobj).css('borderWidth','10px');
		}
	}
}

function tickInit() {
	bwidth = getW(document.body);

	if (editmode != 'content') moveToPile();
	$('#pictures').css('visibility','visible');

}




var unique_img_id = 1;
var pile_img_id = 1;
var imgarr = new Array();
var realclick;
var isHome=false;

function convertToPlayer() {
	var obj = document.getElementById('background_music_control');
	if (obj) {
	var flashobj = document.createElement('div');
		flashobj.innerHTML='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,47,0" width="100%" height="100%" id="wimpy_button_85769" name="wimpy_button_85769" ><param name="movie" value="blueprint/audio/wimpy_button.swf?theFile=server/files/bg_music.mp3&autoplay=yes&loopMe=yes" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><param name="wmode" value="transparent" /><embed src="blueprint/audio/wimpy_button.swf?theFile=server/files/bg_music.mp3&autoplay=yes&loopMe=yes" width="100%" height="100%" quality="high" bgcolor="#ffffff" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="transparent"  name="wimpy_button_85769" /></object>';
		flashobj.className='mp3_play_button';
		flashobj.id='background_music';
		document.body.appendChild(flashobj);
		$(obj).addClass('playing');
		playing=true;
		var mp3timer=setTimeout('moveMp3()',500);

	}
}
function moveMp3() {
	var obj = document.getElementById('background_music');
	obj.style.marginTop = '-2px';
}

var playing;
function togglePlay() {
	var mp3 = document.getElementById('background_music');
	if (playing) {
		$('#background_music object, #background_music param, #background_music').remove();

		playing=false;
		var obj = document.getElementById('background_music_control');
		if (obj) {
			$(obj).removeClass('playing');
		}
	}
	else {
		convertToPlayer();

	}
	// return false;

}
function moveToPile() {

		$('#content img').each(function(){
			unique_img_id++;

			if (getW(this)>getH(this)) this.className='reverse';
			$(this).before('<a id="tmpitem_'+unique_img_id+'" class="img_container"></a>');
			$(this).appendTo('#tmpitem_'+unique_img_id);
			var currentimg = this;
			$('#tmpitem_'+unique_img_id).click(function(){


				createPictureOnPile(this,this.getAttribute('imgid'),true);
			});

			createPictureOnPile(document.getElementById('tmpitem_'+unique_img_id),'imgid_'+unique_img_id, false);


		});



	}
$(function() {

	$('html').addClass('js-enabled');

	jQuery.easing.def = "easeOutBounce";
	if (isHome) {
		$(document.body).addClass('home');
	}

	



	function ajaxContent() {
		$('#topmenu a').click(function(){

			$.get("index.php?window=content&menuid="+$(this).attr('menuid'),function(data){
				$(document.body).removeClass('home');
				document.getElementById('ajax_content').innerHTML=data;
				ajaxContent();
				moveToPile();

			});
			this.href='#menuid_'+$(this).attr('menuid');

		});
	}
	if (editmode == '') ajaxContent();


	convertToPlayer();


});



