/**
 * hints
 *
 *
//*/
$(function() {
    $('input[placeholder]').each(function() {
        var that = this;
        if ('placeholder' in document.createElement(that.tagName)) {
            return;
        }
        var placeholder = that.getAttribute('placeholder');

        that.setAttribute('autocomplete', 'off');

        var input = $(that);
        if (that.value === '' || that.value == placeholder) {
            input.addClass('placeholder');
            that.value = placeholder;
        }

        input.focus(function() {
            if (input.hasClass('placeholder')) {
                this.value = '';
                input.removeClass('placeholder')
            }
        });

        input.blur(function() {
            if (this.value === '') {
                input.addClass('placeholder');
                this.value = placeholder;
            } else {
                input.removeClass('placeholder');
            }
        });

        that.form && jQuery(that.form).submit(function() {
            if (input.hasClass('placeholder')) {
                that.value = '';
            }
        });
    });
});

/**
 * hints end
 *
 */

function Radio()
{
	this.toString=function()
	{
		return '[Object RadioButton]';
	}
	this.set=function(selector,image_class,checked_class,label_checked_class){
		if(label_checked_class==null)
		{
			label_checked_class='';
		}
		$(selector).each(function(){
			var checked='';
			if($(this).attr('checked'))
			{
				checked=' '+checked_class;
				var id=$(this).attr('id');
				if(id!='')
					$('label[for='+id+']').addClass(label_checked_class);
				//alert($(this).parents('label').size())
				$(this).parents('label').addClass(label_checked_class);
			}
			var image='<span class="'+image_class+checked+'"></span>';

			$(this).wrap(image);
			$(this).css('opacity',0);

			function __change()
			{
				var _this=this;

				var name=$(_this).attr('name');
				$('input[name='+name+']').each(function(){
					//var box=this;
					//alert($(this).parents('label').size());
					//alert($(this).attr('checked'));

					var id=$(this).attr('id');
					if(!$(this).attr('checked'))
					{
						
						$(this).parent().removeClass(checked_class);
						if(id!='')
							$('label[for='+id+']').removeClass(label_checked_class);
						$(this).parents('label').removeClass(label_checked_class);
					}
					else
					{
						$(this).parent().addClass(checked_class);
						//alert($('label[for='+id+']').size())
						$(this).parents('label').addClass(label_checked_class);
						setTimeout(function(){
							if(id!='')
								$('label[for='+id+']').addClass(label_checked_class);
						//$(this).parents('label').addClass(label_checked_class);
						}, 10);
						
					}
				});

			}
			$(this).change(__change);
		//$(this).click(__change);
           
            
		})
	};
	return this;
}
function Checkbox()
{
	this.toString=function()
	{
		return '[Object Checkbox]';
	}
	this.refresh=function(selector,checked_class,label_checked_class)
	{
		$(selector).each(function(){
			
			$(this).change(function(){
				//alert(43534);
				var id=$(this).attr('id');
				if(!$(this).attr('checked'))
				{
					$(this).parent().removeClass(checked_class);
					if(id!='')
						$('label[for='+id+']').removeClass(label_checked_class);
					$(this).parents('label').removeClass(label_checked_class);
				}
				else
				{
					$(this).parent().addClass(checked_class);
					if(id!='')
						$('label[for='+id+']').addClass(label_checked_class);
					$(this).parents('label').addClass(label_checked_class);
				}
			})

		})
	}
	this.set=function(selector,image_class,checked_class,label_checked_class){
		if(label_checked_class==null)
		{
			label_checked_class='';
		}
		$(selector).each(function(){
			var checked='';
			if($(this).attr('checked'))
			{
				checked=' '+checked_class;
				var id=$(this).attr('id');
				if(id!='')
					$('label[for='+id+']').addClass(label_checked_class);
				$(this).parents('label').addClass(label_checked_class);

			}
			var image='<span class="'+image_class+checked+'"></span>';

			$(this).wrap(image);
			$(this).css('opacity',0);

			$(this).change(function(){
				//alert(43534);
				var id=$(this).attr('id');
				if(!$(this).attr('checked'))
				{
					$(this).parent().removeClass(checked_class);
					if(id!='')
						$('label[for='+id+']').removeClass(label_checked_class);
					$(this).parents('label').removeClass(label_checked_class);
				}
				else
				{
					$(this).parent().addClass(checked_class);
					if(id!='')
						$('label[for='+id+']').addClass(label_checked_class);
					$(this).parents('label').addClass(label_checked_class);
				}
			})


		})
	};
	return this;
}
$().ready(function(){
	
	var rad=new Radio();
	rad.set('input.radio1', 'radio1','radio1_checked','radio_label');
	rad.set('input.radio2', 'radio2','radio2_checked','radio2_label_checked');
	//rad.set('input.radio3', 'radio3','radio3_checked','radio3_label_checked');
	
	var box=new Checkbox();
	box.set('input.checkbox1', 'checkbox1','checkbox1_checked','checkbox1_label');
	//box.set('input.checkbox2', 'checkbox2','checkbox2_checked','');
	var count=0;
	$('.ie-lt7 label input').each(function(){
		
		if($(this).attr('id')=='')
		{
			
			count++;
			
			//$(this).attr('for','ie7_input_fix_'+count);
			$(this).attr('id','ie7_input_fix_'+count);
			$(this).parents('label').attr('for','ie7_input_fix_'+count);
		}
	})
//alert(count);
});



var all_custom_selects=0;
var cur_active=false;
$().ready(function(){
	$('body').click(function(e){
		//alert(e.target)
		$('.custom_select_options').hide();
		$('.custom_select_options').parent().removeClass('custom_select_expanded');
		//$('.custom_select_options').closest('.search_tf').removeClass('search_tf_focused');
		//	Select().unbindKeyboard();
		if(cur_active)
		{
			
			var id=cur_active.parent().attr('id');
			var real=$('[custom_select="'+id+'"]');						
			real.focus();
			
			cur_active.closest('.search_tf').mouseover();
			cur_active.show();
			cur_active.parent().addClass('custom_select_expanded');
			
			cur_active=false;
		}
	})
})
$('.tempSelectInput').live('blur',function(){
	var sel=this.sel;
	//sel.find('.custom_select_options').hide();
	sel.removeClass('custom_select_focused');
	//sel.closest('.search_tf_focused').removeClass('search_tf_focused');
	$(this).unbind().die().remove();
})
$('.tempSelectInput').live('keydown',function(e){
	//console.log(e.keyCode);*/
	var sel=this.sel;
			
	if(e.keyCode==9)
	{
		//sel.find('.custom_select_options').hide();
		//sel.removeClass('custom_select_expanded');
		//sel.closest('.search_tf_focused').removeClass('search_tf_focused');
		//$(this).unbind().die().remove();
		return true;
	}
	else if(e.keyCode==38)
	{
		sel.addClass('custom_select_expanded');
		sel.find('.custom_select_options').show();
		var prev=sel.find('.sel_a_hover').prev();
		if(prev.size()!=0)
		{
			prev.mouseover();
		}
				
	}
	else if(e.keyCode==40)
	{
		sel.addClass('custom_select_expanded');
		sel.find('.custom_select_options').show();
		var next=sel.find('.sel_a_hover').next();
		if(next.size()!=0)
		{
			next.mouseover();
		}
	}
	else if(e.keyCode==13)
	{
		sel.find('.sel_a_hover').click();
	}
	else if(e.keyCode==27)
	{
		sel.removeClass('custom_select_expanded');
		sel.find('.custom_select_options').hide();
	}
	//console.log(e.keyCode);
	return false;
		
})
$('.tempSelectInput').live('focus', function(){
	var sel=$(this).closest('.custom_select_focused');
	//var id=sel.attr('id');
	//var real=$('[custom_select="'+id+'"]');
	this.sel=sel;
	
})
var Select=function()
{
	

	
	this.refresh=function(selector)
	{
		$().ready(function(){
			$(selector).each(function(){
				var options='';
				var startval=$(this).val();
				var startface='';
				var id=$(this).attr('custom_select');
				
				
				$(this).bind('focus', function (){					
					$('#'+id).closest('.search_tf').addClass('search_tf_focused');
					$('#'+id).addClass('custom_select_focused');		
					$('#'+id+' a[selected="selected"]').mouseover();
					$('#'+id).append('<input class="tempSelectInput" />');
					$('.tempSelectInput').focus().css({
						'position': 'absolute',
						'left': '-9000px'
					});
					
					
				})
				
				
				$(this).children().each( function(){
					
					var class_name='';
					if($(this).attr('class'))
						class_name=$(this).attr('class');
					if($(this).attr('selected'))
					{
						startval=$(this).val();
						startface=$(this).text();
					}
					var value=$(this).val();
					if(value==undefined)
					{
						value=$(this).text();
					}
					options+='<a class="'+class_name+'" style="display: block;" _value="'+value+'" selected="'+$(this).attr('selected')+'" >'+$(this).text()+'</a>';
				})
				
				$(this).change(function(){
					var id=$(this).attr('custom_select');
					
					var val=$(this).val();
					$('#'+id).attr('value', val).removeClass('custom_select_expanded');
					
					var curopt;
					$('#'+id+' a').each(function(){
						if($(this).attr('_value')==val)
						{
							curopt=this;
							return false;
						}							
						return true;
					})
					var text=$(curopt).text();
					
					$('#'+id+' a').removeAttr('selected');
					$(curopt).attr('selected', 'selected');
					//console.log($(curopt).size());
					$('#'+id+' .face').html(text);
					
				})
				$('#'+id+' .custom_select_options .scroll').html(options);
				$('#'+id+' .face').html(startface);
				$('#'+id).attr('value', startval);
				
				var maxwopt=0;
				$('#'+id+' .custom_select_options').css('max-width', 'none').show();
				$('#'+id+' a').each(function(){
					var w=$(this).width();
					if(w>maxwopt)
					{
						maxwopt=w;
					}
				})
				$('#'+id+' .custom_select_options').css('max-width', '').hide();
				$('#'+id+' a').width(maxwopt);
				$('#'+id+' a').mouseover(function(){
					$(this).addClass('sel_a_hover');
					$(this).siblings().removeClass('sel_a_hover');
				})
				$('#'+id+' a').click(function(){
					//alert($(this).attr('_value'));
					
					var val =$(this).attr('_value');
					var cusel=$(this).parent().parent().parent();
					var id=cusel.attr('id');
					
					
					$(this).parent().parent().hide();
					$('select[custom_select='+id+']').val(val).trigger('change');
					
					return false;
				})
			})
		})
		return this;
	}
	
	this.replace=function(selector,class_name)
	{
		$().ready(function(){

			$(selector).each(function(){
				if($(this).hasClass('dbsel-done'))
				{
					return;
				}
				//alert($(this).children().size())
				var startval=$(this).val();
				var startface='';
				
				var id='custom_select'+all_custom_selects;
				var html='<span id="'+id+'" class="'+class_name+'" value="'+startval+'"><b class="dbsf-rt"></b><b class="dbsf-lt"></b><b class="dbsf-rb"></b><b class="dbsf-lb"></b><span class="dbsel-clickable"></span><span class="button"></span><span class="face">'+startface+'</span><span class="custom_select_options" style="display: none;"><span class="scroll"></span></span></span>';
				$(this).after(html);
				$(this).css({
					'position': 'absolute', 
					'left': '-9000px'
				}).attr('custom_select',id).addClass('dbsel-done');
				
				$('#'+id).change(function(){
					
					var sel=$('[custom_select='+$(this).attr('id')+']');
					sel.val($(this).attr('_value'));
					sel.trigger('change');
				})
				$('#'+id).children('.dbsel-clickable').click(function(){
					
					if($(this).parent().children('.custom_select_options').css('display')=='none')
					{
						cur_active=$(this).parent().children('.custom_select_options');
					}
					else
					{
						cur_active=false;
					}
				})
				all_custom_selects++;
			})
		})
		this.refresh(selector);
		return this;
	}
	return this;
}

Select().replace('.custom_select','s_cs1');

$(function(){
	var i=1;
	$('label input').each(function(){
		var id=this.id;
		if(!id)
		{
			id='anonymous_input'+i;
			$(this).attr('id', id);
			i++;
		}
		var label=$(this).closest('label');
		if(!label.attr('for'))
		{
			label.attr('for', id);
		}
	})
})



$(function(){
	var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{
			string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
		],
		dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.userAgent,
			subString: "iPhone",
			identity: "iPhone/iPod"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
		]

	};
	BrowserDetect.init();
	
	var browser=BrowserDetect.browser;
	if(browser=='Firefox')
	{
		$('body').addClass('gecko');
		
	}else if(browser=='Explorer')
	{
		var lt='ie-lt';
		
		var prefix=function(version, string){
			string+=' '+lt+version;
			if(version<10)
			{
				version++;
				return prefix(version, string)
			}
			else
			{
				return string;
			}
		}
		$('body').addClass(prefix(BrowserDetect.version,'ie'+BrowserDetect.version));
	}
	else if(browser=='Chrome')
	{
		$('body').addClass('webkit chrome');
	}
	else if(browser=='Safari')
	{
		$('body').addClass('webkit safari');
	}
	else if(browser=='Opera')
	{
		$('body').addClass('opera');
	}
	
	//alert(version)
	if(browser=='Explorer'&&BrowserDetect.version<=7)
	{
		
		
}
})
