window.alert = function(text) {
	$("body").append("<div class=\"pole_message_info\">"+text+"</div>");
	$("#blackout").css("display","block");
	wysoko=$("body").css("height");
	if(wysoko=="auto") {
		wysoko=$(document).height();
	}
	$("#blackout").css("height",wysoko);
	$('html, body').animate({scrollTop:0}, 'fast');
	$(".pole_message_info").click(function(){
		$(".pole_message_info").fadeTo('fast','0',function(){
			$(".pole_message_info").css("display","none");
			if($(".pole_message_info").css("display")=="block" || $(".pole_message").css("display")=="block" || $(".pole_message_info").css("display")=="block"){
				
			}else{
				$("#blackout").css("display","none");
			}
			$(".pole_message_info").remove();
		});
	});
}

var chckbx;
var imgFalse = "/themes/new_red/img/off.png";
var imgTrue = "/themes/new_red/img/on.png";

function initck() {
  replaceChecks();
}


function replaceChecks() {
	chckbx = document.getElementsByTagName('input'); 
	for(var g=0;g<chckbx.length;g++){
		if(chckbx[g].type=='checkbox'){
			var img = document.createElement('img');
			
			//check if the checkbox is checked
			if(chckbx[g].checked) {
			  img.src = imgTrue;
			} else {
			  img.src = imgFalse;
			}
			
			//set image ID and onclick action
			img.id = 'checkImage'+g;
			img.style.cursor='pointer';
			img.align='absmiddle';
			//set image
			img.counter=g;
			img.onclick = function(){
				var a=this.counter;
				
			
				  if(chckbx[a].checked) {
				      chckbx[a].checked = '';
				      document.getElementById('checkImage'+a).src=imgFalse;
				      
				      //if(chckbx[a].id=="student_selection") {
					    //  document.getElementById('student_selection').checked='';
				      //}
				      
				      if(chckbx[a].id=="radio_id2") {
				      
							document.order_form['dane_klient[firma_wys]'].className='input_disabled';
							document.order_form['dane_klient[imie_wys]'].className='input_disabled';
							document.order_form['dane_klient[nazwisko_wys]'].className='input_disabled';
							document.order_form['dane_klient[adres_wys]'].className='input_disabled';
							document.order_form['dane_klient[miejscowosc_wys]'].className='input_disabled';
							document.order_form['dane_klient[kod_poczt_wys]'].className='input_disabled';
								if ( document.order_form['dane_klient[kraj_wys]']) {document.order_form['dane_klient[kraj_wys]'].className='input_disabled';}
				
							document.order_form['dane_klient[firma_wys]'].disabled = true;
							document.order_form['dane_klient[imie_wys]'].disabled = true;
							document.order_form['dane_klient[nazwisko_wys]'].disabled = true;
							document.order_form['dane_klient[adres_wys]'].disabled = true;
							document.order_form['dane_klient[miejscowosc_wys]'].disabled = true;
							document.order_form['dane_klient[kod_poczt_wys]'].disabled = true;
							if ( document.order_form['dane_klient[kraj_wys]']) {document.order_form['dane_klient[kraj_wys]'].disabled = true;}
				
							wpisuj(document.order_form);
						}else if(chckbx[a].id=="wrapper_check") {
							document.getElementById('wrapper_table').style.display = 'none';
							var lenn = document.order_details.opakowanie.length;
							for(var s=0;s<lenn;s++) {
								document.order_details.opakowanie[s].checked=false;
							};
						}
				  } else {
				      chckbx[a].checked = 'checked';
				      document.getElementById('checkImage'+a).src=imgTrue;
				      
				      //if(chckbx[a].id=="student_selection") {
					    //  document.getElementById('student_selection').checked='checked';
				      //}
				      
				      if(chckbx[a].id=="radio_id2") {
							document.order_form['dane_klient[firma_wys]'].className='input_normal';
							document.order_form['dane_klient[imie_wys]'].className='input_normal';
							document.order_form['dane_klient[nazwisko_wys]'].className='input_normal';
							document.order_form['dane_klient[adres_wys]'].className='input_normal';
							document.order_form['dane_klient[miejscowosc_wys]'].className='input_normal';
							document.order_form['dane_klient[kod_poczt_wys]'].className='input_normal';
							if ( document.order_form['dane_klient[kraj_wys]']) {document.order_form['dane_klient[kraj_wys]'].className='input_normal';}
				
							document.order_form['dane_klient[firma_wys]'].disabled = false;
							document.order_form['dane_klient[imie_wys]'].disabled = false;
							document.order_form['dane_klient[nazwisko_wys]'].disabled = false;
							document.order_form['dane_klient[adres_wys]'].disabled = false;
							document.order_form['dane_klient[miejscowosc_wys]'].disabled = false;
							document.order_form['dane_klient[kod_poczt_wys]'].disabled = false;
							if ( document.order_form['dane_klient[kraj_wys]']) {document.order_form['dane_klient[kraj_wys]'].disabled = false;}
				      }else if(chckbx[a].id=="wrapper_check") {
							document.getElementById('wrapper_table').style.display = 'block';
					}
				      
				  }
			};
			
			//place image in front of the checkbox
			chckbx[g].parentNode.insertBefore(img, chckbx[g]);
			
			//hide the checkbox
			chckbx[g].style.display='none';
		}
	}
}



$(document).ready(function() {
	
	test.tooltip.show = function(obj) {
		
			if( $('#test_tooltip').length == 0 )
				$('body').append('<div id="test_tooltip" style="position: absolute; display: none;"><div class="content">test</div></div>');

			var msg = $(obj).attr('data-invalid_msg');
			if( msg == undefined || msg.length == 0 ) return;

			$('#test_tooltip').css('display', 'block');
			$('#test_tooltip .content').html( $(obj).attr('data-invalid_msg') );
			
			if( $(obj).attr('type') == 'checkbox' && $(obj).css('display') == 'none' ) obj = $(obj).parent();
			
			
			var offset = $(obj).offset();
			var offset_tt = $('#test_tooltip').offset();
			if( $('#test_tooltip').css('opacity') != 0
				&& offset_tt.top == offset.top - $('#test_tooltip').height()
				&& offset_tt.left == offset.left ) return;
			$('#test_tooltip').css('top', offset.top-$('#test_tooltip').height() );
			$('#test_tooltip').css('left', offset.left );
			
			$('#test_tooltip').css('opacity', 0);
			$('#test_tooltip').fadeTo(300, 1);
	}
	
	
	initck();

	
	$("label[for='wrapper_check']").click(function(){
		return false;
	});
	
	$("label[for='radio_id2']").click(function(){
		return false;
	});
	
	$("label[for='reg_id']").click(function(){
		return false;
	});
	if(jQuery.trim($(".desc").html())==''){
		$(".desc").html('<div style="padding: 5px 0px 20px 0px;">Szczegółowy opis produktu znajduje się poniżej.</div>');
	}
	
	// Expand Panel
	$("#open").click(function(){
		$("div#panel").slideDown("slow");
	
	});	
	
	// Collapse Panel
	$("#close").click(function(){
		$("div#panel").slideUp("slow");	
	});		
	
	// Switch buttons from "Log In | Register" to "Close Panel" on click
	$("#toggle a").click(function () {
		$("#toggle a").toggle();
	});		
		
	$("#categories ul li:last-child").addClass("last");		
	$("#prods li:odd").addClass("remove");	
	
    $("#det-data-nav li a").click(function() {
        var curList = $("#det-data-nav li a.current").attr("rel");
        var curListHeight = $("#det-data-rotate").height();
        $("#det-data-rotate").height(curListHeight);
        $("#det-data-nav li a").removeClass("current");
        $(this).addClass("current");
        var listID = $(this).attr("rel");
        if (listID != curList) {
            $("#"+curList).fadeOut(200, function() {
                $("#"+listID).fadeIn();
                var newHeight = $("#"+listID).height();
                $("#det-data-rotate").animate({
                    height: newHeight
                });
            });
        }        
        return false;
    });		
	
	
	$("#myController-upper").jFlow({
		slides: "#slides-upper",
		controller: ".jFlowControl-upper", // must be class, use . sign
		slideWrapper : "#jFlowSlide-upper", // must be id, use # sign
		selectedWrapper: "jFlowSelected-upper",  // just pure text, no sign
		auto: true,		//auto change slide, default true
		width: "580px",
		height: "331px",
		duration: 800,
		prev: ".jFlowPrev-upper", // must be class, use . sign
		next: ".jFlowNext-upper" // must be class, use . sign
	});		
		

	if($(".pole_message_err").css("display")=="block" || $(".pole_message").css("display")=="block" || $(".pole_message_info").css("display")=="block"){
		$("#blackout").css("display","block");
		wysoko=$("body").css("height");
		if(wysoko=="auto") {
			wysoko=$(document).height();
		}
		$("#blackout").css("height",wysoko);
		$('html, body').animate({scrollTop:0}, 'fast');
		
	}
	
	$(".pole_message_err").click(function(){
		$(".pole_message_err").fadeTo('fast','0',function(){
			$(".pole_message_err").css("display","none");
			if($(".pole_message_err").css("display")=="block" || $(".pole_message").css("display")=="block" || $(".pole_message_info").css("display")=="block"){
				
			}else{
				$("#blackout").css("display","none");
			}
		});
	});
	
	$(".pole_message").click(function(){
		$(".pole_message").fadeTo('fast','0',function(){
			$(".pole_message").css("display","none");
			if($(".pole_message_err").css("display")=="block" || $(".pole_message").css("display")=="block" || $(".pole_message_info").css("display")=="block"){
				
			}else{
				$("#blackout").css("display","none");
			}
		});
	});
	
	$(".pole_message_info").click(function(){
		$(".pole_message_info").fadeTo('fast','0',function(){
			$(".pole_message_info").css("display","none");
			if($(".pole_message_err").css("display")=="block" || $(".pole_message").css("display")=="block" || $(".pole_message_info").css("display")=="block"){
				
			}else{
				$("#blackout").css("display","none");
			}
		});
	});
	
	if($(".avail font").length>0){
		jQuery.fn.stripTags = function() { return this.replaceWith( this.html().replace(/<\/?[^>]+>/gi, '') ); };
		$(".avail font").stripTags();
	}
	
	th_var=0;
	th_left=$("#theme_color").css('left');
	$("#theme_button").click(function(){
		if(th_var==0){
			$("#theme_button").css('background-image','url(/themes/new_red/img/th_arr2.jpg)');
			$("#theme_color").animate({
				left: '0'
			}, 500,function(){
				th_var=1;
			});
		}else{
			$("#theme_button").css('background-image','url(/themes/new_red/img/th_arr1.jpg)');
			$("#theme_color").animate({
				left: ''+th_left+''
			}, 500,function(){
				th_var=0;
			});
		}
	});
	
});


