var base_url="http://www.craigandderricott.co.uk/";

//news Carousel for homepage
(function($){
	$.fn.newsCarousel=function(){
		return this.each(function(){
			mouseovered = false;
			$(this).css({overflow:"hidden"}).hover(function(){
				mouseovered=true;
			},function(){
				mouseovered=false;
			});
			
			var items=$("li",this);
			var ul=$("ul",this);
			items.remove();
			ul.append(items[0]);
			
			if(items.length>1){
				var i=0;
				var max=items.length;

				setInterval(function(){
					if(!mouseovered){
						rollItems();
					}
				},3500);
			}
			
			
			
			function rollItems(){
				ul.append(items[i+1]);
				ul.animate({marginTop:"-29px"},function(){
					$("li",this).eq(0).remove();
					ul.css({marginTop:"0px"});
					i++;
					if(i==max-1){
						i=-1;
					}
				});
			}
		});
	};
	
	
	
})(jQuery);

(function($){
	$.fn.adjustThumbnails=function(){
		return this.each(function(){
			var _this=$(this);
			if(_this.is("img")){
				_this.bind("load",function(){			
					if(_this.height()>_this.parent().height()){
						_this.css('margin-top', '50%' );
						_this.css("top", '-' + ( parseInt( $(this).height() ) / 2 ) + 'px' );
					}
					if(_this.width()>_this.parent().width()){
						_this.css('margin-left', '50%' );
						_this.css("left", '-' + ( parseInt( $(this).width() ) / 2 ) + 'px' );
					}
					
					
				});				
			}
		});
	};
})(jQuery);

//Dynamic Dropdowns for categories
(function($){
	$.fn.DynamicDropDowns=function(settings){
		var settings=$.extend({
			prefix:"level_x",
			submitBtn:"selectorSubmit",
			urlPrefix:"shop/category/"
		},settings);
		settings.JSONdata=dropDownsData;
		return this.each(function(){
			var parentContainer=this;
			var DD={
				data:settings.JSONdata,
				init:function(){
					$(parentContainer).append("<select id='"+settings.prefix+"'></select>");
					var options=this.buildOptions(this.data);
					this.appendOptions("#"+settings.prefix+"",options);
					this.attachObservers("#"+settings.prefix+"");
					var _obj=this;
					$("#"+settings.submitBtn+"").bind("click",function(){
						_obj.sendToUrl();return false;
					});
				},
				buildOptions:function(items){
					var html=[];
					html.push("<option value=''>Please Select Category...</option>");
					$(items).each(function(n){
						html.push("<option value='"+this.stub+"' rel='"+n+"'>"+this.name+"</option>");
					});
					return html;
				},
				appendOptions:function(select,options){
					$(select).append(options.join(""));
				},
				removeSubSelects:function(select){
					$(select).siblings("div").remove();
					$("#"+settings.submitBtn+"").hide();
				},
				injectSubSelect:function(select,datapath){
					var _id="level_"+datapath;
					var data=this.getCategories(datapath);
					if(typeof(data)!="undefined" && data.length){
						$(select).after("<div><select id='"+_id+"'></select></div>");
						var options=this.buildOptions(data);
						this.appendOptions("#"+_id,options);
						this.attachObservers("#"+_id);
					}else{
						$("#"+settings.submitBtn+"").show();
					}

				},
				getCategories:function(datapath){
					var parr=datapath.split("-");
					var str="";
					var count=parr.length;
					if(count==1){
						return this.data;
					}
					if(count>1){
						$(parr).each(function(n){
							if(n==1){
								str+="["+this+"]";
							}else if(n>1){
								str+=".children["+this+"]";	
							}

						});
						str+=".children";
					}
					return eval("this.data"+str);
				},
				sendToUrl:function(){
					var url=[];
					$("select",parentContainer).each(function(){
						url.push($(this).val());
					});
					//console.log(base_url+""+settings.urlPrefix+""+(url.join("/")));
					window.location.href=base_url+""+settings.urlPrefix+""+(url.join("/"));
				},
				attachObservers:function(select){
					var _obj=this;
					$(select).bind("change",function(){
						var _this=$(this);
						var elid=_this.attr("id");
						var e=elid.match(/(.+)[=_](.+)/);
						var id=e[2];
						var selected_id=_this.children(":selected").attr("rel");
						_obj.removeSubSelects("#"+elid);
						if(typeof(selected_id)!="undefined" && selected_id!=""){
							_obj.injectSubSelect("#"+elid,id+"-"+selected_id);
						}

					});
				}


			};
			DD.init();
		});	
	};
})(jQuery);


(function(){
	$.fn.replaceSelects=function(){
		return this.each(function(){
			var _this=$(this);
			_this.bind("change",function(){
				var $this=$(this);
				var _name=$this.attr("name");
				var _val=$this.val();
				if(_val=="specify"){
					$("<input name='"+_name+"' type='text' style='width:70px' value='' />").insertAfter($this).focus();
					$this.remove();
				}
			});
		});
	}
})(jQuery);

$(document).ready(function() {
	$("select.replaceable").replaceSelects();
		
});


function open_window(link,w,h){
	var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=yes,scrollbars=yes";
	newWin = window.open(link,'newWin',win);
	newWin.focus();
}
function open_pic_window(link,w,h){
	var win = "width="+w+",height="+(h+55)+",menubar=no,location=no,resizable=yes,scrollbars=yes";
	newWin = window.open(base_url+"shop/large_picture.php?pic="+link,'newWin',win);
	newWin.focus();
}

function open_printable_version(link){
	var win = "menubar=no,location=no,resizable=yes,scrollbars=yes";
	newWin = window.open(link,'perintableWin',win);
	newWin.focus();
}

function confirmDelete(id, ask, url){
	temp = window.confirm(ask);
	if (temp) //delete
	{
		window.location=url+id;
	}
}



function validate() {
	if (document.subscription_form.email.value.length<1)
	{
		alert("Please enter your email correctly");
		return false;
	}
	if (document.subscription_form.email.value == 'Email')
	{
		alert("Please enter your email correctly");
		return false;
	}
	return true;
}

function validate_disc() {
	if (document.formD.nick.value.length<1)
	{
		alert("Please enter your nickname");
		return false;
	}

	if (document.formD.topic.value.length<1)
	{
		alert("Please enter message subject");
		return false;
	}

	return true;
}

function validate_search(){

	if (document.Sform.price1.value!="" && ((document.Sform.price1.value < 0) || isNaN(document.Sform.price1.value)))
	{
		alert("Price should be a positive number");
		return false;
	}
	if (document.Sform.price2.value!="" && ((document.Sform.price2.value < 0) || isNaN(document.Sform.price2.value)))
	{
		alert("Price should be a positive number");
		return false;
	}

	return true;
}

function clear_search(){
	if (document.searform.searchstring.value=="Enter Keyword"){
		document.searform.searchstring.value="";
		
	}	
	
}

function clear_search_in_res(){
	if (document.AdvancedSearchInCategory.search_name.value=="Within this category"){
		document.AdvancedSearchInCategory.search_name.value="";
		
	}	
	
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	  if (restore) selObj.selectedIndex=0;
}

function post_FORM(formname){
	eval("document."+formname+".submit()");
	//console.log(document.getElementById(formname).va.value);
}