//these functions are pollyfilla functions required for CSS transformations


function sortable() {
	/*function helper(elem) {
		//find out what values we are sorting on
		var td=elem,child=0,arr=[],row=elem.parentNode;
		while(td=td.previousSibling) {
			child++;
		}
		//get the following rows and values
		while(row=row.nextSibling) {
			if (row.nodeType !== 1 || row.nodeName.toLowerCase() !== "tr") continue; 
			td=row.childNodes[child];
			arr[arr.length] = row.innerHTML;
		}
		var sort=elem.getAttribute("data-sort");
		if (sort == "num") arr.sort(function(a, b) { return b - a; });
		else arr.sort();
		if (elem.getAttribute("data-sortdir") == "reverse") arr.reverse();
		var table=$(elem).parent("table").clone();
		var orow=elem.parentNode;
		row=table.getElementsByTagName("tr")[0];
		while(row.nextSibling) {
			row.parentNode.removeChild(row.nextSibling);
		}
		for (var i=0;n=arr.length;i<n; i++) {
			row.parentNode.appendChild($(orow.childNodes[arr[i]]))
			if (row.nodeType !== 1 || row.nodeName.toLowerCase() !== "tr") continue;
			row.childNodes[child] = arr.push();
		}
		td=elem=child=arr=row=sort=null;
	}
	$("[data-sort]").click(function(){helper($(this));});*/
	
}

function calendar() {
	
	var apply_date;
	var current_month;
	var current_year;
	var current_day;
	var display_month=false;
	var display_year=false;
	var elem;
	
	
	function drawCal() {
		$("#webwam_calendar").remove();
		var dt,dv,st,mx,opts="",set,ld;
		dt = new Date(display_year,display_month, 0);
		ld=dt.getDate();
		dv=document.createElement("div");
		dv.id="webwam_calendar";
		dv.className = "webwam_calendar";
		var mxdt=new Date();
		st= mxdt.getFullYear() - 20;
		mx=st + 40;
		while(st <= mx) {
			opts += "<option value='" + st + "'";
			if (st == display_year) opts += " selected='true' ";
			opts += ">" + st + "</option>";
			st++;
		}
		mxdt=null;
		dv.innerHTML = "<div class='webwam_calendar_title'><a class='webwam_calendar_close'>X</a><a class='webwam_calendar_prev'>&lt;</a><a class='webwam_calendar_next'>&gt;</a><span id='webwam_calendar_date'></span> <select id='webwam_calendar_select' class='year'>" + opts + "</select></div>";
		dv.innerHTML += "<table><thead><tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr></thead><tbody></tbody></table>";
		set=$(elem).offset();
		dv.style.left = (set.left) + "px";
		dv.style.top = (set.top + elem.offsetHeight) + "px";
		document.body.appendChild(dv);
		dv=$("#webwam_calendar")[0];
		dt.setDate(1);
		//get the day of the week
		w=dt.getDay();
		var str=[];
		while(w--) {
			str[str.length] = "<td>&nbsp;</td>";
		}
		for (var i=1; i<=ld; i++) {
			if (i == parseInt(current_day) && display_month == current_month && display_year == current_year) {
				str[str.length] = "<td data-date='" + i + "' class='webwam_calendar_date_active'>" + i + "</td>";
			} else {
				str[str.length] = "<td data-date='" + i + "' class='webwam_calendar_date'>" + i + "</td>";
			}
		}
		var mod=str.length % 7;
		for (var i=0;i<mod; i++) {
			str[str.length] = "<td>&nbsp;</td>";
		}
		//arr now contains a complete list of dates
		var fstr="";
		while(str.length > 0) {
			fstr += "<tr>";
			for (var i=0;i<7;i++) {
				fstr += str.shift();
			}
			fstr += "</tr>";
		}
		var mth=['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
		$(dv).find("#webwam_calendar_date").html(mth[dt.getMonth()]);
		$(dv).find("tbody").html(fstr);
		dv.scrollIntoView(false);
		$(dv).click(function() {
			in_use=true;
			return false;
		});
		fstr=d=cd=w=l=str=mth=null;
		$(".webwam_calendar_close").click(function(){$("#webwam_calendar").remove();elem=null;});
		$(".webwam_calendar_prev").click(function(){
			display_month--;
			if (display_month < 0) {
				display_month=11;
				display_year--;
			}
			drawCal();
		});
		$(".webwam_calendar_next").click(function(){
			display_month++;
			if (display_month > 11) {
				display_month = 0;
				display_year++;
			}
			drawCal();
		});
		
		$("#webwam_calendar_select").change(function(){
			display_year = this.value;
			drawCal();
		});
		$("[data-date]").click(function() {
			$(elem)[0].value = this.getAttribute("data-date") + "/" + display_month + "/" + display_year;
			$("#webwam_calendar").remove();
			elem=null;
		});
		dv=null;
	}
	$("[type='date']").focus(function(){
		var dt = new Date();
		if (this.value.trim() != "") {
		 var vl=this.value.split(" ");
		 var pts=vl[0].split("/");
		 current_year = pts[2].trim();
		 current_month = pts[1].trim();
		 current_day = pts[0].trim();
		}
		if (!current_year || isNaN(parseInt(current_year))) current_year = dt.getFullYear();
		if (!current_month || isNaN(parseInt(current_month))) current_month = dt.getMonth();
		if (current_day || isNaN(parseInt(current_day))) current_day = dt.getDate();
		display_month = current_month;
		display_year = current_year;
		elem=this;
		drawCal();
		pts=vl=dt=null;
	});
}
 
function autoAjax(urn) {
	var el=$("[data-urn='" + urn + "']")[0];
	var time=$(el).attr("data-ajaxauto");
	var url = location.href;
	url += (url.indexOf("?") > -1) ? "&" : "?";
	url += "ajaxid=" + urn;
    var dataAjaxListener = $(el);	
    var ajaxType = el.getAttribute("data-ajaxlistenertype");
    $.ajax({
		type: 'GET',
		url: url,
		success: function(response) {		
    		if (ajaxType == "prepend") $(dataAjaxListener).html($(dataAjaxListener).html() + response);
    		else if (ajaxType == "shift") $(dataAjaxListener).html(response + $(dataAjaxListener).html());
    		else $(dataAjaxListener).html(response); // or alert(response);
		}
	});	
    setTimeout("autoAjax(" + urn + ");", el.getAttribute("data-ajaxauto"));
    el=null;
}

function ajaxQueries() {
	    function loadAjax(response) {
	    };
	    
		$('form[data-ajax]').submit(function(event) {
			// stop form from submitting normally 
			event.preventDefault();
			
			var dataAjax = $(this).attr('data-ajax');		
			var url = $(this).attr('action');
			var dataString = $(this).serialize();			
		      
		    $('*[data-ajaxlistener]').each(function() {			    
				if ($(this).attr("data-ajaxlistener") == dataAjax) {					
					var dataAjaxListener = $(this);		
					var ajaxType = dataAjaxListener.getAttribute("data-ajaxlistenertype");
					$.ajax({
						type: 'POST',
						url: url,
						data: dataString,
						success: function(response) {	
						    if (ajaxType == "prepend") $(dataAjaxListener).html($(dataAjaxListener).html() + response);
						    else if (ajaxType == "shift") $(dataAjaxListener).html(response + $(dataAjaxListener).html());
						    else $(dataAjaxListener).html(response); // or alert(response);
						}
					});											
				}	    
			});

			return false;		
		});
		
		var tgs=$('[data-ajaxauto]');
		var i=tgs.length;
		while(i--){
			setTimeout("autoAjax(" + tgs[i].getAttribute("data-urn") + ");", tgs[i].getAttribute("data-ajaxauto"));
		};
		tgs=null;
		
		$('a[data-ajax]').click(function(event) {
			event.preventDefault();
			
			var dataAjax = $(this).attr('data-ajax');		
			var url = $(this).attr('href');
		      
		    $('*[data-ajaxlistener]').each(function() {			    
				if ($(this).attr("data-ajaxlistener") == dataAjax) {				
					var dataAjaxListener = $(this);		
					var ajaxType = dataAjaxListener.getAttribute("data-ajaxlistenertype");
					$.ajax({
						type: 'GET',
						url: url,
						success: function(response) {	
							if (ajaxType == "prepend") $(dataAjaxListener).html($(dataAjaxListener).html() + response);
							else if (ajaxType == "shift") $(dataAjaxListener).html(response + $(dataAjaxListener).html());
							else $(dataAjaxListener).html(response); // or alert(response);
						}
					});	
				}	    
			});

		    return false;
		});		
	}  

function scrollContent() {
			// loop scroller starts here
			function loopScroll() {
	        	$jScroller = {
					config: {
						obj : [],
						refresh: 120,
						regExp: { px: /([0-9,.\-]+)px/ }
					},
	
					cache: {
						timer: 0,
		        		init: 0
					},
		
		    		add: function(parent, child, direction, speed, mouse) {
						if ($(parent).length && $(child).length && direction && speed >= 1) {
							$(parent).css({ "overflow": "hidden" });
							$(child).css({ "position": "relative", "left": 0, "top": 0 });
							
		    		    	$jScroller.config.obj.push({ 
		        		    	parent: $(parent),
		        		    	child: $(child),
								direction: direction,
								speed: speed,
								pause: false
							});	
		        		}
		    		},
		
					start: function() {
						if ($jScroller.cache.timer === 0 && $jScroller.config.refresh > 0) {
							$jScroller.cache.timer = window.setInterval($jScroller.scroll, $jScroller.config.refresh);
						}
					},			
		
					get: {
		    			px: function(value) {
			    			var result = '';
			
			    			if (value) {
			        			if (value.match($jScroller.config.regExp.px)) {
									if (typeof value.match($jScroller.config.regExp.px)[1] !== 'undefined') {
										result = value.match($jScroller.config.regExp.px)[1];
									}
								}
							}
			
			    			return result;
		    			}
		        	},
		
					scroll: function() {
						for (var i in $jScroller.config.obj) {
							if ($jScroller.config.obj.hasOwnProperty(i)) {
								var obj = $jScroller.config.obj[i],
									left = Number(($jScroller.get.px(obj.child.css('left'))||0)),
		 							top = Number(($jScroller.get.px(obj.child.css('top'))||0)),
		        		            min_height = obj.parent.height(),
		        		            min_width = obj.parent.width(),
		        		            height = obj.child.height(),
		        		            width = obj.child.width();	
								
	        		        	switch (obj.direction) {
	        		            	case "vertical":
	            		            	if (top <= -1 * height) { top = min_height; }
	        		                 	obj.child.css('top',top - obj.speed);
	        		                	break;        		              
	
	        		                case "horizontal":
	            		                if (left <= -1 * width) { left = min_width; }
	        		                 	obj.child.css('left',left - obj.speed);
	        		                	break;	        		               
	        	               	}        		          	
	    		          	}
			          	}
	        		}
			 	};
	
	        	var $wrappers = $("[data-scroll][data-loop]");
	
				$wrappers.each(function() {				
					$jScroller.add($(this), $(this).children(), $(this).attr("data-scroll"), 
						($(this).attr("data-speed") !== undefined) ? $(this).attr("data-speed") : 1);
				});
	        
	        	$jScroller.start();
	        }
			// loop scroller ends here
            
            // auto scroller starts here
        	function autoScroll() {
                function maxWidth(obj) {
                    var childrenNumber = obj.children().length;                
    				var width = obj.children(':nth-child(1)')[0].offsetWidth + parseInt(obj.children(':nth-child(1)').css("margin-left")) + parseInt(obj.children(':nth-child(1)').css("margin-right"));

    				for (var i = 2; i <= childrenNumber; i++) {					
    					var currElWidth = obj.children(':nth-child(' + i + ')')[0].offsetWidth + parseInt(obj.children(':nth-child(1)').css("margin-left")) + parseInt(obj.children(':nth-child(1)').css("margin-right"));

    					if (currElWidth > width) {
    						width = currElWidth;
    					}						
    				}
    				
    				return width;
                }

                function maxHeight(obj) {
                    var childrenNumber = obj.children().length;                                
    				var height = obj.children(':nth-child(1)')[0].offsetHeight;
                    
    				for (var i = 2; i <= childrenNumber; i++) {					
    					var currElHeight = obj.children(':nth-child(' + i + ')')[0].offsetHeight;

    					if (currElHeight > height) {
    						height = currElWidth;
    					}						
    				}

    				return height;
                }            

    			// This is used to correctly display the <p>, <ul> etc.
               // $('[data-scroll] > *').css({ 'margin': 0, 'padding': 0, 'border': 'none' });

                $('*[data-scroll=horizontal]').each(function() {				
    				var currentPosition = 0;					
    				var slideWidth = maxWidth($(this));					
    				var slideHeigth = maxHeight($(this));
    			    var frames = this.getAttribute("data-scrollframes");
    			    if (!frames) frames=1;

    				$(this).css('width', slideWidth * frames);
    				$(this).css('height', slideHeigth);
    				$(this).children().wrap('<div></div>');
    				var slides = $(this).children();					
    				var numberOfSlides = slides.length;
    				var index = $(this).index();

    				// display a child at one time
    				$(this).css('overflow', 'hidden');				
    				
    				// Wrap all slides(children) with #slideInner div
    				// Float left to display horizontally, readjust slides width				
    				slides.wrapAll('<div id="slideInner'+ index  +'"></div>')	
    					.css({ 'float' : 'left', 'width' : slideWidth });

    				$('#slideInner' + index).css('width', slideWidth * numberOfSlides);

    				var currDataUrn = $(this).attr('data-urn');    				
    				var speed = $(this).attr('data-automatic');					

    				$('*[data-scrollnext=' + currDataUrn + ']').bind('click', function() {
    				    currentPosition++;					    
                        if (currentPosition > numberOfSlides - frames && !this.getAttribute("data-loop")) {
                        	currentPosition = numberOfSlides - frames;
                        	return false;
                        }
    					if (currentPosition == numberOfSlides) {
    						currentPosition = 0;							
    						$('#slideInner' + index).css('marginLeft', slideWidth).animate({ 'marginLeft' : 0 }, speed);							
    					} else {
    						$('#slideInner' + index).animate({ 'marginLeft' : slideWidth * (-currentPosition) }, speed);
    					}
    				});
    					
    				$('*[data-scrollprev='+ currDataUrn + ']').bind('click', function() {						
    					currentPosition--;
    				    if (currentPosition == -1 && !this.getAttribute("data-loop")) {
    				    	currentPosition = 0;
    				    	return false;
    				    }
    					if (currentPosition == -1) {
    						currentPosition = numberOfSlides - 1;						
    						$('#slideInner' + index).css('marginLeft', -slideWidth * numberOfSlides)
    							.animate({'marginLeft': -slideWidth * (numberOfSlides - 1)}, speed);
    						} else {
    							$('#slideInner' + index).animate({ 'marginLeft' : -slideWidth * currentPosition }, speed);
    						}							    
    					});				
                	});

                	$('*[data-scroll=vertical]').each(function() {				
	    				var currentPosition = 0;					
	    				var slideWidth = maxWidth($(this));					
	    				var slideHeigth = maxHeight($(this));
	    				var slides = $(this).children();					
	    				var numberOfSlides = slides.length;
	    				var index = $(this).index();
	
	    				// display a child at one time
	    				$(this).css('width', slideWidth);
	    				$(this).css('height', slideHeigth);				
	    				$(this).css('overflow', 'hidden');
	    				
	    				var currDataUrn = $(this).attr('data-urn');
	    				var speed = $(this).attr('data-automatic');									
	    					    					
	    				slides.wrapAll('<div id="slideInner'+ index  +'"></div>')	
	    					.css({ 'float' : 'left', 'height' : slideHeigth });
	    		
	    				$('#slideInner' + index).css('height', slideHeigth * numberOfSlides);
	    					
	    				$('*[data-scrollnext='+ currDataUrn + ']').bind('click', function() {
	    				    currentPosition++;					    
	    				    if (currentPosition > numberOfSlides - frames && !this.getAttribute("data-loop")) {
	    				    	//minus again to compensate for clicking again
	    				    	currentPosition = numberOfSlides - frames;
	    				    	return false;
	    				    }
	    					if (currentPosition == numberOfSlides) {
	    						currentPosition = 0;							
	    						$('#slideInner' + index).css('marginTop', slideHeigth).animate({ 'marginTop' : 0 }, speed);							
	    					} else {
	    						$('#slideInner' + index).animate({ 'marginTop' : slideHeigth * (-currentPosition) }, speed);
	    					}
	    				});
	    					
	    				$('*[data-scrollprev='+ currDataUrn + ']').bind('click', function() {						
	    				    currentPosition--;
	    				    if (currentPosition == -1 && !this.getAttribute("data-loop")) {
	    				    	currentPosition = 0;
	    				    	return false;
	    				    }
	    					if (currentPosition == -1) {
	    						currentPosition = numberOfSlides - 1;						
	    						$('#slideInner' + index).css('marginTop', -slideHeigth * numberOfSlides)
	    							.animate({'marginTop': -slideHeigth * (numberOfSlides - 1)}, speed);
	    					} else {
	    						$('#slideInner' + index).animate({ 'marginTop' : -slideHeigth * currentPosition }, speed);
	    					}							    
	    				});				
	                });           
	            }
            	// auto scroller ends here

            loopScroll();
     		autoScroll();
        }


  function openCustomWindow() {
		$('a[data-window]').click(function (event) {
      	event.preventDefault();

      	var defaultWindowWidth = 800;
      	var defaultWindowHeight = 600;
          
          var url = $(this).attr("href");
          var windowName = "popUp";               

			var windowWidth = ($(this).attr("data-width") !== undefined) ?
					$(this).attr("data-width") : defaultWindowWidth;
			var windowHeight = ($(this).attr("data-height") !== undefined) ?
					$(this).attr("data-height") : defaultWindowHeight;
			var windowToolbars  = ($(this).attr("data-toolbars") !== undefined) ?
					$(this).attr("data-toolbars") : "no";			

			// the window will open to the center of the browser
			var posLeft = ($(document).width() - windowWidth) / 2;
			var posTop = ($(document).height() - windowHeight) / 2;
			
			var windowParams = 
				"width=" + windowWidth + 
				",height=" + windowHeight +			
				",left=" + posLeft + 
				",top=" + posTop + 
				",toolbar=" + windowToolbars +
				",resizeable=yes,scrollbars=yes";				
          
          customWindow = window.open(url, windowName, windowParams);                          
      });
	}

  
 function popUpLoadingIcon(dv) {
		if ($(".darkCover").length == 0 && $(".darkContent").length == 0) {	
			//create de div
			var box = $('<div class="darkCover"></div>').css({
				"position": "fixed", "left": "0", "top": "0",
				"z-index": "100", "background-color": "black",
				"opacity": "0", "width": "100%", "height": "100%"		
			});
					
			box.click(function() {
				$(this).fadeOut();			
				content.fadeOut();
				$(this).remove();
				content.remove();						
			});

			// make first child to the body
			$("body").prepend(box);
			box.fadeTo("fast", 0.8);

			// create the content 
			var content = $('<div class="darkContent"></div>').css({
				"position": "fixed", "background-color": "black",
				"padding": "8px", "overflow": "hidden",			
				"opacity": "0", "border-radius": "8px",
				"-moz-border-radius": "8px", "-webkit-border-radius": "8px",		
				"width": "16px", "height": "16px", "z-index": "101",
				"left": "50%", "top": "50%", "margin-left": "-8px", "margin-right": "-8px"
			});

			// prepend the content
			if (!dv) dv="body";
		    $(dv).prepend(content);
			content.prepend('<img src="images/ajax-loader.gif" />');
			content.fadeTo("fast", 1);
		} else {
			$(".darkCover").remove();
			$(".darkContent").remove();
		}	
}

  
var slideshow = {
 cdv : null,
 init : function(dv){
	if (!dv || !dv.childNodes) dv=this;
	//remove any blank spaces between child nodes
	var i=dv.childNodes.length;
	while(i--) {
		if (dv.childNodes[i].nodeType != 1) dv.removeChild(dv.childNodes[i]);
	}
	slideshow.cdv=dv.firstChild;
	if (!dv.getAttribute("data-speed")) dv.setAttribute("data-speed", "5");
	if (!dv.getAttribute("data-ease")) dv.setAttribute("data-ease", "2");
	$(dv.firstChild).fadeIn(dv.getAttribute("data-speed") * 100);
	window.setTimeout(slideshow.next, 5000);
 },
 next : function() {
	 var dv=slideshow.cdv;
	 var speed=dv.parentNode.getAttribute("data-speed") * 100;
	 $(dv).fadeOut(speed);
	 dv=(dv.nextSibling) ? dv.nextSibling : dv.parentNode.firstChild;
	 $(dv).fadeIn(speed, function() {slideshow.timer=window.setTimeout(slideshow.next, slideshow.cdv.parentNode.getAttribute("data-ease") * 1000);});
	 slideshow.cdv=dv;
	dv=speed=null;
 },
 clear : function() {
	 if (slideshow.timer) window.clearTimeout(slideshow.timer);
	 slideshow.timer=null;
	 slideshow.cdv=null;
 }	 
}

var cal ={
  curFocus : null,
  init : function(dv) {
	var off=set.offset(dv);
    var cdv = document.createElement("div");
    dv.id="calendar";
    cdv.className = "ui-calendar";
    cdv.style.top=off[0] + "px";
    cdv.style.left=(off[1] + off[2] + 15 ) + "px";
  	cdv.setAttribute("data-date", dv.value);
  	cdv.innerHTML + "<div>Header</div><div></div>";
    document.body.appendChild(cdv);
    $(cdv).fadeIn('fast');
    cal.curFocus=dv;
    cdv=off=dv=null;
 },
 draw : function(dv) {
	 var date=dv.getAttribute("data-date");
	 htm += "<table><tr><td>Su</td><td>Mo</td><td>Tu</td><td>We</td><td>Th</td><td>Fr</td><td>Sa</td></tr>";
	 var dto = new Date();
	 //use data date to set attributes
	 dto.setDate(1);
	 dto.setFullYear(this.date.getFullYear());
	 dto.setMonth(this.date.getMonth());
	 day = dto.getDay();
	 st=0;
	 while (dto.getMonth() == this.curmonth) {
	    htm += "<tr>";
	    for (var i=0; i<7; i++) {
	  	   if (i==day && st==0) st=1;
	  	   if (st==1) {
	  	     dt = dto.getDate();
	  	     htm += "<td id='date/" + dt + "' style='text-align:center;cursor:pointer;'>" + dt + "</td>";
	  	     dto.setTime(dto.getTime() + this.day);
	  		} else {
	  		 htm += "<td>&nbsp;</td>";
	  		}
	  	    if (dto.getMonth() != this.curmonth) st=2;
	  	  }
	  	htm += "</tr>";
	  }
	 htm += "</table>";
	 dv.removeChild(dv.lastChild);
	 dv.innerHTML += htm;
	 dv=htm=dto=null;
   },
   change : function(dv, type, val) {
	   if (type === "year") {
	   } else if (type==="month") {
	   } else {
	   }
   },
   apply : function() {
	   //get element with current focus
	   cal.curFocus = this.value;
   },
   close : function() {
	   $("id='calendar").fadeOut("fast");
   }
}

function changeSelect() {
	var val=this.value;
	$("#" + document.getElementById(this.getAttribute("data-change"))).find("optgroup").each(function(){
	  if (this.getAttribute("data-value") == val) $(this).css("display", "inline");
	  else $(this).css("display", "none");
	});
}

function copy() {
	var orig=document.getElementById(this.getAttribute("data-copy"));
	var cln=orig.cloneNode(true);
	$(cln).find("input").each(function() {
	  if (this.type == "checkbox" || this.type == "radio") this.checked=false;
	  else this.value == "";
	});
	$(cln).find("textarea").each(function() {
		 this.value == "";
	});
	orig.parentNode.appendChild(cln);
	cln=orig=null;
}

var drop = {
  init : function() {
	var tgs=$("[data-dropdown]");
	var i=tgs.length;
	while(i--) {
		$(tgs[i]).hover(drop.show, drop.hide);
		$(tgs[i]).css("z-index", 300);
	}
  },
  show : function() {
	 $(this).children("ul").show();
	 $(this).children("ul").css("z-index", 500);
	 $(this).children("ul").children("li").hover(drop.show, drop.hide);
  },
  hide : function() {
	 $(this).children("ul").hide();
	 $(this).children("ul").css("z-index", 0);
	 $(this).children("ul").children("li").hover(null, null);
  }
}

function changeLinkDefaultAction() {
	// this inner function will only be used once per page load
	function addIFrame() {
		// add an iFrame, invisible by default
		$("body").append('<div id="iframe_zone" class="popup"><iframe id="my_iframe" name="my_iframe"></iframe></div>');
		$("#my_iframe").css("display", "none");	
	}

	// this inner function will set the initial size and 
	// other properties for the iFrame
	function initIFrame() {			
		$("#my_iframe").css("width", "100px");
		$("#my_iframe").css("height", "100px");
		$("#my_iframe").css("position", "absolute");
		$("#my_iframe").css("margin", "auto");
		$("#my_iframe").css("border", "none");		
		$("#my_iframe").css("overflow", "hidden");		
	}
	
	// check if the iFrame wasn't yet inserted in the html
	if ($("#my_iframe").length == 0) {
		addIFrame();
	}		

	initIFrame();

	var docWidth = $(document).width();
	var docHeight = $(document).height();

	var iFrameWidth = $("#my_iframe").width(); 
	var iFrameHeigth = $("#my_iframe").height();
	
	var leftPos = (docWidth - iFrameWidth) / 2;
	var topPos = (docHeight - iFrameHeigth) / 2;		
	
	$("#my_iframe").css("left", leftPos);
	$("#my_iframe").css("top", topPos);
		
	// apply the iFrame targetting for all <a> 
	// tags that have a "data-popup" attribute
	$("a[data-popup]").click(function(event) {
		//event.preventDefault();

		// change the default value of the "target" attribute
		$(this).attr("target", "my_iframe");
		$(this).attr("src", this.href);
		$("body").css("background-color", "#686868");	
				
		// when page is finished to load animate the iFrame
		$("#my_iframe").load(function() {
				$("#my_iframe").css("display", "inline")
				.delay(200)
				.animate({ "top": "0", "height": docHeight
			}, 500).animate({ "width": docWidth, "left": 0 });
		});
	});
}

function hideAndShow() {
	function helper(elem, tp) {
		$(elem).addClass("current");
		var target=$("[data-urn='" + $(elem).attr("data-visibility") + "']")[0];
		var currentGroup = $(target).attr("data-group");
		if (currentGroup !== undefined) {
			// get the "data-group" value for the current element
			$("[data-group='" + currentGroup + "']").each(function() {
					// this will hide all the coresponding elements,
					// including the current one								
					$(this).fadeOut(1);
					//$(this).slideUp();							
			});
		}
		$("[data-urn='" + $(".current").attr("data-visibility") + "']").each(function() {
			  if (tp == "mouseout") $(this).fadeOut(1);
			  else if (tp=="mouseover" || tp == "onchange") $(this).fadeIn(1);
			  else $(this).fadeToggle(1);							
		});						

		$(elem).removeClass("current");	
	}
    $("[data-visibility]").each(function() {
        $("[data-urn='" + $(this).attr("data-visibility") + "']").each(function() {
        	if ($(this).attr("data-default") != "1") $(this).css("display", "none");
        });
        
        $(this).mouseover(function() {
			$(this).css("cursor", "pointer");
		});
        
        if (this.tagName.toLowerCase() == "option") {
        	var sel=$(this).parent("select")[0];
        	if (!sel.onchange) $(sel).change(function(){helper($(this), "onchange")});
        	this.setAttribute("data-group", sel.name);
        	sel=null;
        } else {
          if(this.type == "radio") this.setAttribute("data-group", this.name);
          
		  if ($(this).attr("data-action") == "onmouseover") {
			$(this).mouseover(function() {
				helper($(this), "mouseover");
			});		

			$(this).mouseout(function() {
				helper($(this), "mouseout");
			});
		  } else {
			//click is the default action
			$(this).click(function() {
				helper($(this), "click");
			});
		  }
        }
    });
}

function changeSrc() {
	function helper(elem, tp) {
		if (!$(elem).attr("data-src")) {
			$(elem).attr("data-src", $(elem).attr("src"));
		}
		if (tp=="mouseover") {
			$(elem).attr("src", $(elem).attr("data-mouseoversrc"));
		} else if (tp=="mouseout") {
			$(elem).attr("src", $(elem).attr("data-src"));
		}
	}
	$("[data-mouseoversrc]").each(function() {
		$(this).mouseover(function() {
			helper($(this), "mouseover");
		});
		$(this).mouseout(function() {
			helper($(this), "mouseout");
		});
	});    
}

$(document).ready(function(){
	drop.init();
	$("[data-effect='crossfade']").each(slideshow.init);
	//set any popups
	$("[data-closepopup]").click(function() {$("#my_iframe", window.parent.document).css("display", "none");});
	calendar();
	changeLinkDefaultAction();
	hideAndShow();	
	scrollContent();
	openCustomWindow();
	changeSrc();
	ajaxQueries();
	tgs=i=null;
});
