/*
 * jScroller 0.3 - Scroller Script
 *
 * Copyright (c) 2007 Markus Bordihn (http://markusbordihn.de)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2007-11-15 13:00:00 +0100 (Thu, 15 Nov 2007) $
 * $Rev: 0.3 $
 */

jQuery(document).ready(function($){
   this.defaults = {
     scroller: {
       interval:  0,    // Dont touch !
       refresh:   50,  // Refresh Time in ms
       direction: "left", // down,right,left,up
       speed:     2,    // Set the Scroller Speed
       id:        "#scroller",
       cont_id:   "#scroller_container",
	   hover_id:  "#partnersmain",
	   moreinfo_id: "#partnersmore",
	   url: "",
	   data: null
     }
   }
   
   

   var jscroller_config = $.extend(this.defaults), jscroller_scroller = $(jscroller_config.scroller.id), jscroller_scroller_cont = $(jscroller_config.scroller.cont_id), jscroller_scroller_moreinfocont = $(jscroller_config.scroller.moreinfo_id),jscroller_scroller_hovercont = $(jscroller_config.scroller.hover_id)
   

   
   /*if(jscroller_config.scroller.url){
   	$.ajax({
		   type: 'GET',
		   url: jscroller_config.scroller.url,
		   data: jscroller_config.scroller.data,
		   dataType: 'xml',
		   success: function(xml) {
			   var jscroller_scroller_idArray = new Array();
			   $(xml).find("id").each(function() {
											   alert($(this))
											   //jscroller_scroller_idArray = $(this).attr("lat");
				});
			   }
			   });
	}*/
	
	
   
   
   /*jQ.ajax({ type: “POST”, url: “AddressFinder.aspx”, data: “number=1&postcode=wn58ln”, dataType: “xml”, success: function(xml){

//This simple XPath XML function looks through the returned XML data
//All tags can now be accessed within the loop
var _number = “”;
var _line1 = “”;
var _town = “”;
var _city = “”;
var _country = “”;

//This function will loop for each match on addresses/address
jQ(”/addresses/address”, xml).each(function(){

_number = jQ(”number”, this).text();
_line1 = jQ(”line1?, this).text();
_town = jQ(”town”, this).text();
_city = jQ(”city”, this).text();
_country = jQ(”country”, this).text();

jQ(”#text11?).val(_line1);
jQ(”#text12?).val(_town);
jQ(”#text13?).val(_city);
jQ(”#text14?).val(_country);
});
*/

   
   var elementLink
   
   if (jscroller_scroller && jscroller_scroller_cont) {
      jscroller_scroller.css({float: 'left', position: 'relative', left: 0, top: 0});
      jscroller_init();
   }

   function jscroller_startScroll() {
	   /*if(jscroller_scroller_moreinfocont.children()){
	   jscroller_scroller_moreinfocont.children().remove()
	   }
	   this.moreinfochildren.appendTo(jscroller_scroller_moreinfocont);*/
	   
     if(!jscroller_config.scroller.interval) {
      jscroller_config.scroller.interval=window.setInterval(jscroller_doScroll,jscroller_config.scroller.refresh);
     }
   }

   function jscroller_stopScroll() {
	   if (jscroller_config.scroller.interval) {
      window.clearInterval(jscroller_config.scroller.interval);
      jscroller_config.scroller.interval=0;
     }
   }
   
  

   function jscroller_init() {
	   
	   
	    
	   
	   
	   this.scroller_dom = jscroller_getElem(jscroller_scroller.attr("id"))
	   
	   this.children = $(this.scroller_dom).children();
	   
	   this.moreinfochildren = jscroller_scroller_moreinfocont.children()
	   
	   
	    var childCount = this.children.length
		if (childCount > 1) {
			for (i=1;i<childCount;i++) {
				this.children = $(this.scroller_dom).children();

				(this.children.eq(i-1)).after(this.children.eq(i))
				//(pos).after(obj);
				
			}
			this.children = $(this.scroller_dom).children();
			(this.children.eq(1)).before(this.children.eq(0))
		}
		
	   this.min_height=jscroller_scroller_cont.height()
	   this.min_width=jscroller_scroller_cont.width()
	   this.speed=jscroller_config.scroller.speed
	   this.p_height=$(this.scroller_dom).height()
	   this.p_width=$(this.scroller_dom).width()
	   this.direction=jscroller_config.scroller.direction
	   this.jscroller=jscroller_scroller
	   
	   
	   
	   switch(this.direction) {
		 case 'up':
		 	this.currentchild = this.children.eq(0);
			var olddisplayType = this.currentchild.css('display')
			this.currentchild.css('display','none');
			this.currentchildoffset = this.p_height - $(this.scroller_dom).height();
			this.currentchild.css('display',olddisplayType);
		 	break;
		 case 'right':
		 	this.currentchild = this.children.eq(this.children.length-1);
			var olddisplayType = this.currentchild.css('display')
			this.currentchild.css('display','none');
			this.currentchildoffset = this.p_width - $(this.scroller_dom).width();
			this.currentchild.css('display',olddisplayType);
		  break;
		 case 'left':
		 	this.currentchild = this.children.eq(0);
			
			var olddisplayType = this.currentchild.css('display')
			this.currentchild.css('display','none');
			this.currentchildoffset = this.p_width - $(this.scroller_dom).width();
			this.currentchild.css('display',olddisplayType);
			break;
		 case 'down':
		 	this.currentchild = this.children.eq(this.children.length-1);
			var olddisplayType = this.currentchild.css('display')
			this.currentchild.css('display','none');
			this.currentchildoffset = this.p_height - $(this.scroller_dom).height();
			this.currentchild.css('display',olddisplayType);
		 	break;
		 }
	 
	 
	 
	 // Pause autoscrolling if the user moves with the cursor over the clip.
    jscroller_scroller_hovercont.hover(function() {
        jscroller_stopScroll();
    }, function() {
        jscroller_startScroll();
    });
	
	$.fn.itemHoverOver1 = function() {
		//var clonedelement = $(this).clone(true).appendTo(jscroller_scroller_cont)
		//.append($(this).clone(true))
		
		if(jscroller_scroller_moreinfocont.children()){
	   jscroller_scroller_moreinfocont.children().remove()
	   }
	   
		/*if(elementLink){
		elementLink.remove()
		}*/
		var elementLinkText = ($(this).attr('title'))? 'View '+($(this).attr('title') + ' profile'):'View profile'
		elementLink = $('<a href="' + $(this).attr('href')+ '">' + elementLinkText + '</a>').appendTo(jscroller_scroller_moreinfocont);
		//clonedelementchildren.remove()
		//jscroller_stopScroll();
	}
	
	$.fn.itemHoverOut1 = function() {
	}
	
	/*this.children.each(function() {
		$(this).hover(
			function () {
				$(this).itemHoverOver()
			},
  			function () {
				$(this).itemHoverOut()
			}
		);
	});*/
	 
	 
	
    
	
	
	
	
	
	
	
	
	
    jscroller_scroller_cont.css('overflow','hidden');
    if(!jscroller_config.scroller.interval) { 
      if (window.attachEvent) {
       window.attachEvent("onfocus", jscroller_startScroll);
       window.attachEvent("onblur",  jscroller_stopScroll);
       window.attachEvent("onresize", jscroller_startScroll);
       window.attachEvent("onscroll", jscroller_startScroll);
      }
      else if (window.addEventListener) {
       window.addEventListener("focus", jscroller_startScroll, false);
       window.addEventListener("blur",  jscroller_stopScroll, false);
       window.addEventListener("resize", jscroller_startScroll, false);
       window.addEventListener("scroll", jscroller_startScroll, false);
      }
      jscroller_startScroll();
      if ($.browser.msie) {window.focus()}
     }
   }

   function jscroller_getElem(Elem) {
	   
	//return (typeof Elem == "string" && document.getElementById)? document.getElementById(Elem) : Elem;
   	return (typeof Elem == "string" && $("#"+Elem).get())? $("#"+Elem).get() : Elem;
   }

   function jscroller_doScroll() {
	  
	   if ($("#lbCenter").get(0)){
		  
		   if ($("#lbCenter").css('display')!='none'){
			   return;
		   }
	   }
	   if (this.scroller_dom) {
		   var p_top= Number((/[0-9-,.]+/.exec(jscroller_scroller.css('top'))||0))
		   var p_left=Number((/[0-9-,.]+/.exec(jscroller_scroller.css('left'))||0))
		   
		   switch(this.direction) {
			  	case 'up':
			   		if (p_top <= -1*this.p_height) {p_top=this.min_height;}
					jscroller.css('top',p_top-this.speed+'px');
					break;
				case 'right':
					/*if (p_left >= this.min_width) {p_left=-1*this.p_width;}*/
					if (p_left >= this.min_width-(this.p_width-this.currentchildoffset)) {
						this.children = $(this.scroller_dom).children();
						this.currentchild = this.children.eq(this.children.length-1);
						var olddisplayType = this.currentchild.css('display')
						this.currentchild.css('display','none');
						//this.currentchild.remove()
						this.currentchildoffset = this.p_width - $(this.scroller_dom).width()
						p_left = p_left-(this.currentchildoffset)
						jscroller.css('left',p_left+'px');
						$(this.scroller_dom).prepend(this.currentchild)
						this.currentchild.css('display',olddisplayType);
						
						this.children = $(this.scroller_dom).children();
						this.currentchild = this.children.eq(this.children.length-1);
						var olddisplayType = this.currentchild.css('display')
						this.currentchild.css('display','none');
						//this.currentchild.remove()
						this.currentchildoffset = this.p_width - $(this.scroller_dom).width(); 
						$(this.scroller_dom).append(this.currentchild)
						this.currentchild.css('display',olddisplayType);
					}
					jscroller.css('left',p_left+this.speed+'px');
       				break;
				case 'left':
	   				if (p_left <= -1* this.currentchildoffset) {
						this.p_width = $(this.scroller_dom).width()
						this.children = $(this.scroller_dom).children();
						this.currentchild = this.children.eq(0);
						
						var olddisplayType = this.currentchild.css('display')
						this.currentchild.css('display','none');
						
						//var olddisplayType = this.currentchild.css('position')
						//this.currentchild.css('position','absolute');
						
						
						//this.currentchild.remove()
						
						this.currentchildoffset = this.p_width - $(this.scroller_dom).width()
						//this.currentchild.css('position',olddisplayType);
						this.currentchild.css('display',olddisplayType);
						//this.currentchildoffset = this.currentchild.width()
						p_left = p_left+(this.currentchildoffset)
						
						//(this.currentchild).after(this.children);
						jscroller.css('left',p_left+'px');
						
						(this.children.eq(this.children.length-1)).after(this.currentchild);
						
						/*this.currentchild.hover(
							function () {
								$(this).itemHoverOver()
							},
							function () {
								$(this).itemHoverOut()
							}
						);*/
						//$(this.scroller_dom).append(this.currentchild)
						
						/*var newdiv = $("<div></div>").insertBefore($(this.scroller_dom));
						newdiv.css({width: '0px', height: '0px', overflow: 'hidden'});
						var scroller_domclone = $(this.scroller_dom).clone().prependTo(newdiv);
						newdiv.remove()*/
						
						
						this.children = $(this.scroller_dom).children();
						this.currentchild = this.children.eq(0);
						var olddisplayType = this.currentchild.css('display')
						this.currentchild.css('display','none');
						//var olddisplayType = this.currentchild.css('position')
						//this.currentchild.css('position','absolute');
						//this.currentchild.remove()
						this.currentchildoffset = this.p_width - $(this.scroller_dom).width();
						this.currentchild.css('display',olddisplayType);
						//this.currentchildoffset = this.currentchild.width()
						//$(this.scroller_dom).prepend(this.currentchild)
						(this.children.eq(1)).before(this.currentchild);
						
						/*this.currentchild.hover(
												
							function () {
								
								$(this).itemHoverOver()
							},
							function () {
								$(this).itemHoverOut()
							}
						);*/
						
						
						this.children = $(this.scroller_dom).children();
						//this.currentchild.css('display',olddisplayType);
						
					}
					jscroller.css('left',p_left-this.speed+'px');
       				break;
       			case 'down':
        			/*if (p_top >= this.min_height) {p_top=-1*this.p_height;}*/
        			this.jscroller.css('top',p_top+this.speed+'px');
       				break;
    		}
   	 }
   }
});