$(document).ready(function() {
	CoreEngineForms.init();
});
var CoreEngineForms = {
	selectBoxes : [],

	init : function() {
		var selects = document.getElementsByTagName('select');
		for(var i = 0; i < selects.length; i++) {
				CoreEngineForms.selectBoxes.push(selects[i]);
		}
		cefSelectBox.init();
	}
};


var cefSelectBox = {
	init : function() {
		//alert(CoreEngineForms.selectBoxes.length);
		for(var i=0;i<CoreEngineForms.selectBoxes.length;i++) {
			var input = CoreEngineForms.selectBoxes[i];
// 			alert(input.name);
			var m = $(input).prev();
			var r = $(m).prev().prev();
// 			alert(m.get(0).className);
			var IE6 = (navigator.appVersion.indexOf("MSIE 6.")==-1) ? false : true;
			var IE7 = (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;
			var IE8 = (navigator.appVersion.indexOf("MSIE 8.")==-1) ? false : true;
			var Chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
			
  		
			if(navigator.appName=='Netscape' && navigator.appVersion=='5.0 (Windows; pl)') { var FF=true; }
			else {var FF=false;}
			if(IE7){
				m.css('width', m.get(0).offsetWidth + 'px');
				$(input).css('width', '100%');
				$(input).css('margin-left', -(m.get(0).offsetWidth/2)-25 + 'px' );
				$(input).css('left','85px');
			}else if(IE8){
				m.css('width', m.get(0).offsetWidth - r.get(0).offsetWidth + 'px');
				$(input).css('width',m.parent().get(0).offsetWidth - 10 + 'px' );
				$(input).css('margin-left', 5 + 'px' );
			} else if(FF) {
				m.css('width', m.get(0).offsetWidth - r.get(0).offsetWidth + 'px');
				$(input).css('width',m.parent().get(0).offsetWidth - 10 + 'px' );
				$(input).css('margin-left', -(m.get(0).offsetWidth/2)-22 + 'px' );
				$(input).css('left','85px');
			} else {
				m.css('width', m.get(0).offsetWidth - r.get(0).offsetWidth + 'px');
				$(input).css('width',m.parent().get(0).offsetWidth - 10 + 'px' );
				$(input).css('margin-left', 5 + 'px' );
			}
			
// 	  		if(window.location=='http://sad.cstore.pl/search'){
// 		  		alert ($(input).css('left'));
// 	  		}
			
			if(!IE6){
				
				if(Chrome) {
					$(input).css('position', 'relative');
					$(input).css('margin-top', '-32px');
					$(input).css('position', 'absolute');
				}
				
// 		  		if(window.location=='http://sad.cstore.pl/search'){
// 			  		alert ($(input).css('margin-left'));
// 		  		}
				
				var children = $(input).children('option');
				for(var d = 0; d < children.length; d++) {
					if($(children[d]).attr('selected') == true) {
						$(input).prev().children('span')[0].innerHTML = children[d].innerHTML;
					}
				}
				
				// $(input).mousedown(function() {
					// $(this).parent().addClass('cef_selectBox_over');
				// }); 
				
				$(input).mouseover(function() {
					$(this).parent().addClass('cef_selectBox_over');
				}); 
				
				// $('option').mouseover(function() {
					// $(this).parent().parent().addClass('cef_selectBox_over');
				// });
				
				$(input).mouseout(function() {
					$(this).parent().removeClass('cef_selectBox_over');
				});
				
				$(input).change(function() {
					var children = $(this).children('option');
					for(var i = 0; i < children.length; i++) {
						if($(children[i]).attr('selected') == true) {
							$(this).prev().children('span')[0].innerHTML = children[i].innerHTML;
						}
					}
					$(this).parent().removeClass('cef_selectBox_over');
				});
			}else if(IE8){
				
				
		  		if(window.location=='http://sad.cstore.pl/search'){
			  		alert ($(input).css('margin-left'));
		  		}
				
				var children = $(input).children('option');
				for(var d = 0; d < children.length; d++) {
					if($(children[d]).attr('selected') == true) {
						$(input).prev().children('span')[0].innerHTML = children[d].innerHTML;
					}
				}
				
				// $(input).mousedown(function() {
					// $(this).parent().addClass('cef_selectBox_over');
				// }); 
				
				$(input).mouseover(function() {
					$(this).parent().addClass('cef_selectBox_over');
				}); 
				
				// $('option').mouseover(function() {
					// $(this).parent().parent().addClass('cef_selectBox_over');
				// });
				
				$(input).mouseout(function() {
					$(this).parent().removeClass('cef_selectBox_over');
				});
				
				$(input).change(function() {
					var children = $(this).children('option');
					for(var i = 0; i < children.length; i++) {
						if($(children[i]).attr('selected') == true) {
							$(this).prev().children('span')[0].innerHTML = children[i].innerHTML;
						}
					}
					$(this).parent().removeClass('cef_selectBox_over');
				});
			}
		}
	}
};

