/*
*
* @name jportal-functions.js
* @version $Id: jportal-functions.js,v1.0.1 20/08/2011 $
*
* @copyright (c) 2011 CaniDev
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

//Common functions
var pLang={'rsz-enlarge':'Enlarge','rsz-close':'Close','side-title':'Expand/Contract'};
jPortal(function(a){a.fn.gethref=function(){var b=a(this).attr("href");var c=b.indexOf("#");if(c!=-1){return b.substring(c,b.length)}return b};a.create_accordion=function(b,c){c=a.extend({header:"h5",active:0,autoHeight:false,icons:{header:"ui-icon-circle-arrow-e",headerSelected:"ui-icon-circle-arrow-s"}},c);a(b).accordion(c)}})

//Adjust Images
jPortal(function(a){a.fn.adjustImages=function(b,c){function j(){if(!f){return}a("#rsz-fullsize").remove();f=false;a(window).unbind("resize",j);a("object").css("visibility","visible")}function i(b){if(f){return}f=true;img=a(this).parent(".resized").find("img");a("object").css("visibility","hidden");d_width=parseInt(a(document).width());d_height=parseInt(a(document).height());i_top=a(document).scrollTop()+40;i_width=parseInt(img.data("orig-width"));i_height=parseInt(img.data("orig-height"));if(i_width>d_width-40){i_width=d_width-40}if(i_top+i_height>d_height-40){d_height=i_top+i_height+40}m_width=i_width+10;m_height=i_height+10;el=a(e.overlay);el.css({width:d_width,height:d_height});initial_width=40;initial_height=40;initial_left=d_width/2-initial_width/2;final_width=i_width+10;final_left=d_width/2-final_width/2;el.find(".rsz-img").css({left:initial_left,top:a(document).scrollTop()+40,width:initial_width,height:initial_height}).addClass("ui-corner-all").append('<img src="'+img.attr("src")+'" style="display: none; margin:5px; width:'+i_width+'px" />');a("body").append(el);el.show();final_height=a(".rsz-img img").height()+10;a(".rsz-img").animate({left:final_left,width:final_width,height:final_height},500,function(){a(this).children("img").show();a(".rsz-close").show();a(window).bind("resize",j)});a("#rsz-overlay, .rsz-close").click(function(b){a(this).unbind();j();b.preventDefault()});a(".rsz-close").attr("title",pLang["rsz-close"]);b.preventDefault()}var d={active:1,maxwidth:450};var e={loading:'<div class="rsz-loading portal-loading"></div>',resize:'<div class="resized">'+'<a style="display: none;" class="rsz-button" href="#"></a>'+"</div>",overlay:'<div id="rsz-fullsize" style="position:absolute; left:0; top:0; z-index: 99996; display: none;">'+'<div id="rsz-overlay" class="ui-widget-overlay" style="z-index: 99997;"></div>'+'<div class="rsz-img" style="position:absolute; z-index: 99998;">'+'<a style="z-index: 99999; display: none;" class="rsz-close" href="#" ></a>'+"</div>"+"</div>"};d=a.extend(d,c);if(!d.active){return}var f=false;var g=a(this);var h=parseInt(d.maxwidth);if(h==0){d.active=0}if(d.active){g.find(b).hide().after(a(e.loading))}a(window).load(function(){g.each(function(){b_width=parseInt(a(this).innerWidth())-50;t_maxwidth=h>b_width?b_width:h;a(this).find(b).each(function(){t=a(this);if(t.parent().is("a")){var b=t.parent()}else{var b=t}original_width=parseInt(t.width());original_height=parseInt(t.height());if(d.active&&original_width>t_maxwidth){el=a(e.resize);t.css({width:t_maxwidth,height:"auto"});t.next(".rsz-loading").remove();b.clone().appendTo(el);el.find("img").data({"orig-width":original_width,"orig-height":original_height}).show();el.css({width:t_maxwidth,height:t.height()});el.find(".rsz-button").attr("title",pLang["rsz-enlarge"]).click(i);if(t_maxwidth<=200){el.find(".rsz-button").addClass("small")}b.replaceWith(el);el.mouseenter(function(){a(this).find(".rsz-button").fadeIn("fast")}).mouseleave(function(){a(this).find(".rsz-button").fadeOut("fast")})}else if(d.active){t.next(".rsz-loading").remove();t.show()}})});if(window.ActiveXObject){a("table").each(function(a){this.refresh()})}});return this}})

//Collapsable
jPortal(function(a){a.fn.collapsableBlock=function(b){function h(){var b=c.cookiename+"=";var d=document.cookie.split(";");for(i=0;i<d.length;i++){var e=d[i];while(e.charAt(0)==" "){e=e.substring(1,e.length)}if(e.indexOf(b)==0){f=e.substring(b.length,e.length).split(":");break}}for(i=0;i<f.length;i++){a("#block-"+f[i]).find(c.container).hide();a("#block-"+f[i]).find("."+c.classname).addClass("collapsed")}}function g(){e.each(function(){e_display=a(this).find(c.container).css("display");parent_id=a(this).attr("id");if(parent_id!=undefined){c_data=parent_id.replace("block-","");pos=a.inArray(c_data,f);if(e_display=="none"&&pos==-1){f.push(c_data)}else if(e_display=="block"&&pos!=-1){f.splice(pos,1)}}});var b=90;var d=new Date;d.setTime(d.getTime()+b*24*60*60*1e3);expires="; expires="+d.toGMTString();document.cookie=c.cookiename+"="+f.join(":")+expires+c.cookiesettings}var c={header:".block_title",container:".block-content",classname:"b-side",cookiename:"blockcookie",cookiesettings:"; path=/",active:true};c=a.extend(c,b);var e=a(this);var f=new Array;if(!c.active){return this}e.each(function(){b_id=a(this).attr("id");button=a('<div id="'+c.classname+"-"+b_id+'" class="'+c.classname+'" title="'+pLang["side-title"]+'"></div>');a(this).find(c.header).prepend(button)});a("."+c.classname).click(function(b){d=a("#"+a(this).attr("id").replace(c.classname+"-","")).find(c.container);if(d.css("display")=="block"){d.slideUp("slow",g);a(this).addClass("collapsed")}else{d.slideDown("slow",g);a(this).removeClass("collapsed")}b.preventDefault()});a(document).ready(h);return this}})

//Dock
function distToSqEdge(a,b,c,d,e){var f=Math.sqrt((b-d)*(b-d)+(c-e)*(c-e));vx=d-b;vy=e-c;var g=0;var h=0;if(vx>vy){if(vx>-vy){g=1}else{h=1}}else{if(vx>-vy){h=-1}else{g=-1}}vlength=Math.sqrt(vx*vx+vy*vy);vux=vx/vlength;vuy=vy/vlength;cosA=vux*g+vuy*h;centreToSqEdge=Math.abs(.5*a/cosA);mouseToSquareEdge=vlength-centreToSqEdge;return mouseToSquareEdge}jPortal(function(a){a.fn.dockmenu=function(b){var c={size:40,maxsize:70,distance:70,setHeight:true};a.extend(c,b);var d=c.setHeight;var e=a(this);var f=e.find("img");var g=parseInt(c.size);var h=parseInt(c.maxsize);var i=parseInt(c.distance);var j=h-g;var k=j/i;var l=false;e.hide();a(document).ready(function(){f.css("width",g);if(d){f.css("height",g)}e.show()});a(window).load(function(){a(document).mousemove(function(b){f.each(function(){var c=a(this).offset();var e=distToSqEdge(this.width,c.left+this.width/2,c.top+this.height/2,b.pageX,b.pageY);var f=g;if(e<i){if(e<0){e=0}l=true;f+=j-e*k}a(this).css("width",f);if(d){a(this).css("height",f)}})})})}})

//Marquee
jPortal(function(a){a.fn.marquee=function(b){function n(){clearInterval(i)}function m(){clearInterval(i);i=setInterval(function(){switch(j.direction){case"left":case"right":size1=g;size2=e;css="left";break;case"up":case"down":size1=h;size2=f;css="top";break;default:clearInterval(i);return;break}pos=parseInt(d.css(css));if(j.direction=="left"||j.direction=="up"){if(pos+size1>-1){pos=pos-j.fps;if(!j.loop&&pos<=0){clearInterval(i);return}d.css(css,pos)}else{d.css(css,size2)}}else{if(pos<size2){pos=pos+j.fps;if(!j.loop&&pos>=size2-1){clearInterval(i);return}d.css(css,pos)}else{d.css(css,0-size1)}}},parseInt(j.delay))}var c,d,e,f,g,h;var i;var j={direction:"left",delay:60,fps:2,background:"none",height:"auto",align:"left",loop:true,type:"scroll",padding:"2px"};var k={display:"block",overflow:"hidden",position:"relative",width:"100%"};var l={position:"absolute",display:"none"};j=a.extend(j,b);c=a(this);d=c.children(".marquee-container");if(!d.is("div")){return}j.fps=parseInt(j.fps);c.css(a.extend(k,{background:j.background}));d.css(a.extend(l,{padding:j.padding}));g=parseInt(d.outerWidth());h=parseInt(d.outerHeight());e=parseInt(c.width());f=j.height=="auto"?h:parseInt(j.height);pos={top:0,left:0};if(j.type=="static"){switch(j.align){case"center":pos.left=e/2-g/2;break;case"right":pos.left=e-g;break}}else{switch(j.direction){case"left":pos.left=e;break;case"right":pos.left=0-g;break;case"up":pos.top=f;g=e-1;break;case"down":pos.top=0-h;g=e-1;break}}pos=a.extend(pos,{width:g,height:h,"text-align":j.align});c.css("height",f);d.css(pos).show();if(j.type=="scroll"){if(j.direction=="left"||j.direction=="right"){a(window).resize(function(){e=parseInt(c.width());size2=e})}c.bind("mouseover",n).bind("mouseout",m);m()}}})


