/*!
 * Haunted Site jQuery Plugin | Random Ghost Pop-up Elements v2.0
 * Copyright © 2010-2011, Alessio Caporale
 * Twitter: @Alex_Workshop E-mail: ale887it@hotmail.it
 * Includes jQuery 1.4+ http://jquery.com/
 * The plugin is NOT freeware, the license to use it is sold only at http://codecanyon.net/
 * Further information on how to buy it can be found at http://codecanyon.net/user/AlexBay
 */
var modalGhosts=0;(function($){$.fn.ghostify=function(g){var h={'minDuration':3,'maxDuration':10,'minInterval':15,'maxInterval':30,'swing':10,'modal':false,'static':false,'left':-1,'top':-1,'trigger':null,'float':false,'istant':false,'style':1,'priority':0};if(g){h=$.extend({},h,g)}var i=$(window);var j=i.width(),winHei=i.height(),prior=h.priority+1000,minDur,maxDur,maxInt,minInt=h.minInterval*1000;if(!h.static){minDur=h.minDuration*1000;maxDur=h.maxDuration;if(maxDur<1)maxDur=1000;else maxDur=maxDur*1000;maxInt=h.maxInterval*1000;if(minInt<maxDur){minInt=maxDur+100}}else{minDur=h.left;maxDur=0;maxInt=h.top}if(h.modal){if($('.ghostModalBg').length==0){var k=document.createElement('div');k.setAttribute("class","ghostModalBg");document.body.appendChild(k)}}this.each(function(){var b=document.createElement('div');b.appendChild(this);document.body.appendChild(b);var c=document.createElement('img');c.setAttribute("src","ghost/x_"+h.style+".png");c.setAttribute("alt","X");c.setAttribute("class","closeGhost");b.appendChild(c);var d=$(b);d.toggleClass("ghostStyle_"+h.style).css({'position':'absolute','display':'none','padding':'10px','z-index':prior});if(h.trigger!=null){var e=$("#"+h.trigger);e.css('cursor','pointer');e.click(function(){initGhost(d,$(c),j,winHei,minDur,maxDur,minInt,maxInt,h.swing,h.modal,h.float,h.istant,h.style);var a=$(this);a.css('cursor','auto');a.unbind('click')})}else{var f=getLoadChild(this);if(f==null||f.complete||f.readyState=="complete"){initGhost(d,$(c),j,winHei,minDur,maxDur,minInt,maxInt,h.swing,h.modal,h.float,h.istant,h.style)}else{$(f).load(function(){initGhost(d,$(c),j,winHei,minDur,maxDur,minInt,maxInt,h.swing,h.modal,h.float,h.istant,h.style)})}}})};function initGhost(b,c,d,e,f,g,h,i,j,k,l,m,n){b.css({'float':'left','left':-4000,'display':'inline'});var o=b.width();var p=d-o,maxTop=e-b.height(),rMaxDur,rMaxInt,rInt;if(g!=0){rMaxDur=g-f;rMaxInt=i-h;rInt=h+Math.floor(Math.random()*rMaxInt)}else{rMaxDur=0;rMaxInt=i;rInt=h;if(f=="center"){if(!l){b.css('left',(p/2)+getScrollX())}else{b.css('left',(p/2))}}else{if(f=="right"){if(!l){b.css('left',(p+getScrollX()-35))}else{b.css('left',(p-35))}}}if(i=="center"){if(!l){b.css('top',(maxTop/2)+getScrollY())}else{b.css('top',(maxTop/2))}}else{if(i=="bottom"){if(!l){b.css('top',(maxTop+getScrollY()-j-10))}else{b.css('top',(maxTop-j-10))}}}}b.css({'float':'','opacity':0});if(l){b.css('position','fixed')}if($.browser.msie&&(parseFloat($.browser.version)<=8)){b.css('padding','20px');c.css({'left':(b.width()+20)+'px','top':'0px'})}else{if(n==0){c.css({'left':(o)+'px','top':'0px'})}else{c.css({'left':(o+10)+'px','top':'-10px'})}}c.css({'position':'absolute','cursor':'pointer'});var q=new timerPair();c.click(function(){var a=$(this).parent();a.stop(true).fadeOut(500);clearTimeout(q.spawner);clearTimeout(q.stopper);if(k){modalGhosts--;if(modalGhosts==0){$(".ghostModalBg").fadeOut(500)}}setTimeout(function(){document.body.removeChild(a[0])},550)});if(m){spawnGhost(b,p,maxTop,f,rMaxDur,h,rMaxInt,l,j,k,q)}else{setTimeout(function(){spawnGhost(b,p,maxTop,f,rMaxDur,h,rMaxInt,l,j,k,q)},rInt-g)}}function spawnGhost(a,b,c,d,e,f,g,h,i,j,k){var l,randY,rDur,rInt,start="+=",end="-=",fading;if(e!=0){l=Math.floor(Math.random()*(b-15));randY=Math.floor(Math.random()*(c-15));rDur=d+Math.floor(Math.random()*e);rInt=f+Math.floor(Math.random()*g);if(rDur<500)rDur=500}else{rDur=2000;if(d==-1&&g==-1){l=Math.floor(Math.random()*(b-15));randY=Math.floor(Math.random()*(c-15));rInt=0}else{l=d;randY=g;rInt=-1}}if(Math.random()<0.5){start="-=";end="+="}if(!h&&rInt!=-1){l+=getScrollX();randY+=getScrollY()}fading=(rDur<2000)?250:500;if(l!="center"&&l!="right"){a.css('left',l+'px')}if(randY!="center"&&randY!="bottom"){a.css('top',randY+'px')}if(j){$(".ghostModalBg").fadeTo(fading,0.5);modalGhosts++}if(i!=0){a.fadeTo(fading,1,function(){swingGhost(a,start,end,i)})}else{a.fadeTo(fading,1)}if(e!=0){k.stopper=setTimeout(function(){a.stop(true).fadeOut(fading);if(j){modalGhosts--;if(modalGhosts==0){$(".ghostModalBg").fadeOut(fading)}}},rDur-(fading*2));k.spawner=setTimeout(function(){spawnGhost(a,b,c,d,e,f,g,h,i,j,k)},rInt)}}function swingGhost(a,b,c,d){a.animate({top:b+d},2000,function(){a.animate({top:c+d},2000,function(){swingGhost(a,b,c,d)})})}function getLoadChild(a){var b=a.getElementsByTagName('*');for(var i=0,len=b.length;i<len;i++){if(b[i].src){return b[i]}}return null}function timerPair(){this.stopper=null;this.spawner=null}function getScrollX(){var a=document.body.scrollLeft;if(a==0){if(window.pageXOffset){a=window.pageXOffset}else{a=(document.body.parentElement)?document.body.parentElement.scrollLeft:0}}return a}function getScrollY(){var a=document.body.scrollTop;if(a==0){if(window.pageYOffset){a=window.pageYOffset}else{a=(document.body.parentElement)?document.body.parentElement.scrollTop:0}}return a}})(jQuery);
