var delay_left = 2000;
var delay_right = 2200;
var counter_left;
var counter_right;

function picture(number, logo) {
	this[number] = new Image;
	this.src = logo;
}
function change_left(which) {
    try {
        if ( which == (banner_left.length - 1) ) counter_left = 0;
        else counter_left = which + 1;
        document.images["picture_banner_left"].src = banner_left[which].src;
  
        setTimeout("change_left(counter_left);",delay_left);
    } catch (e) {};
}
function change_right(which) {
    try {
        if ( which == (banner_right.length - 1) ) counter_right = 0;
    	else counter_right = which + 1;
    	document.images["picture_banner_right"].src = banner_right[which].src;
  
    	setTimeout("change_right(counter_right);",delay_right);
    } catch (e) {};
}

var download_bcgOpacity = 0.4;

function openDownloadWindowGF(type, model, id, queryString) {
	$.ajaxSetup({cache: false});
	
	$('select, object, embed').css({visibility: 'hidden'});

	var arrayPageSize = $.fn.lightbox.getPageSize();
	$("#download_bcg").hide().css({width: '100%', height: arrayPageSize[1]+'px', opacity : download_bcgOpacity}).fadeIn();

	var arrayPageScroll = $.fn.lightbox.getPageScroll();
	var download_containerTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
	var download_containerLeft = arrayPageScroll[0];
	$('#download_container')
		.css('top', download_containerTop+'px')
		.css('left', download_containerLeft+'px')
		.show();

	$('#systemWorking').show();
	if (queryString == undefined) {
		queryString = {}
	}
	$.get("/download/" + type + "/" + model + "/" + id + "/", queryString, function(data){
		$('#systemWorking').hide();
		$("#download_window_content").html(data);
	});

}

function openDownloadWindow(type, id, queryString) {
	$.ajaxSetup({cache: false});
	
	$('select, object, embed').css({visibility: 'hidden'});

	var arrayPageSize = $.fn.lightbox.getPageSize();
	$("#download_bcg").hide().css({width: '100%', height: arrayPageSize[1]+'px', opacity : download_bcgOpacity}).fadeIn();

	var arrayPageScroll = $.fn.lightbox.getPageScroll();
	var download_containerTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
	var download_containerLeft = arrayPageScroll[0];
	$('#download_container')
		.css('top', download_containerTop+'px')
		.css('left', download_containerLeft+'px')
		.show();

	$('#systemWorking').show();
	if (queryString == undefined) {
		queryString = {}
	}
	$.get("/download/" + type + "/" + id + "/", queryString, function(data){
		$('#systemWorking').hide();
		$("#download_window_content").html(data);
	});

}

function closeWindow() {
	$('#download_bcg').fadeOut(1000);
	$('#download_container').fadeOut(1000);
	$('select, object, embed').css({visibility: 'visible'});
	$.ajaxSetup({cache: true});
}

function companytype(obj){
	var other_obj = $('#id_company_type_1');
	if (obj.id == 'id_company_type_0_2') {
		if (obj.checked) {
            other_obj[0].disabled = false;
        } else {
			other_obj[0].disabled = true;
			other_obj.value = ''
        }
	};
}

$(function() {
    $('#id_company_type_0_2').each(function() {
        if (!this.checked) $('#id_company_type_1').attr('disabled','disabled');
    });
});

function countryselect(obj){
	var selected_country = obj.options[obj.selectedIndex].value;
	$.post("/account/ajax/country-states/", {country:selected_country} ,
		function(data){
			eval("var country_states=(" + data + ")");
			if (country_states.length > 0 ) {
				var state = $('#id_company_state')[0];
				var parents = state.parentNode;
				parents.removeChild(state);

				var myselect = document.createElement("select");
				myselect.id = 'id_company_state';
				myselect.name = 'company_state';
				myselect.display = 'block';

				var i = 0;

				for (i = 0; i < country_states.length; i++) {
					myselect.options[i] = new Option(country_states[i][1], country_states[i][0]);
				};
				parents.appendChild(myselect);
			} else {
				var state = $('#id_company_state')[0];
				var parents = state.parentNode;
				parents.removeChild(state);

				var myinput = document.createElement("input");
				myinput.id = 'id_company_state';
				myinput.name = 'company_state';
				myinput.display = 'block';
				parents.appendChild(myinput);
			};
		}
	);
	if(selected_country == 'MX' || selected_country == 'CA' || selected_country == 'US'){
		$('#id_last_year_sales').html('(US$)');
		$('#id_this_year_sales_est').html('(US$)');		
	}
	else{
		$('#id_last_year_sales').html('(€)');
		$('#id_this_year_sales_est').html('(€)');		
	};
};

function submit_search() {
	search_size = document.search.search_text.value.length;
    if (search_size > 2) {
    	document.search.submit();
    } else {
		alert('You must provide at least three characters');
	}
};
/* wylaczone dla wersji 2011
function TopFormsInit() {
	document.getElementById('topLogin').style.display = 'none';
};

function TopLogin() {
	document.getElementById('SearchLink').style.display = 'block';
	document.getElementById('topLogin').style.display = 'block';
	document.getElementById('topSearch').style.display = 'none';
	document.getElementById('LoginLink').style.display = 'none';
};
function TopSearch() {
	document.getElementById('SearchLink').style.display = 'none';
	document.getElementById('topLogin').style.display = 'none';
	document.getElementById('topSearch').style.display = 'block';
	document.getElementById('LoginLink').style.display = '';
};
*/
function blanksearch() {
	if (document.getElementById('g').value == '') {
	 	alert('Please specify some words or valid user names to search on.'+"\n"+' There were no matches for those that you specified.');
		return false;
	} else {
		document.forms['searchfaq'].submit();
		return true;
	}
};

function openPreviewWindow() {	
	
	var arrayPageSize = $.fn.lightbox.getPageSize();	
	$("#download_bcg").hide().css({width: '100%', height: arrayPageSize[1]+'px', opacity : download_bcgOpacity}).fadeIn();

	var arrayPageScroll = $.fn.lightbox.getPageScroll();
	var download_containerTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
	var download_containerLeft = arrayPageScroll[0];
	$('#download_container')
		.css('top', '300px')
		.css('left', download_containerLeft+'px')
		.show();	
	$('#preview_partner_box').show();		
}
function openPreviewWindowOEM() {	
	
	var arrayPageSize = $.fn.lightbox.getPageSize();	
	$("#download_bcg").hide().css({width: '100%', height: arrayPageSize[1]+'px', opacity : download_bcgOpacity}).fadeIn();

	var arrayPageScroll = $.fn.lightbox.getPageScroll();	
	$('#oem_preview_card').show();
	$('#oem_preview_input').css({'position':'relative','height':'30px','width':'400px','margin-top':'187px','margin-left':'100px'});
}

/* Skrypty dla wersji strony z 2011 roku START */
/* ------------------------------------------- */

/* Skrypt przelaczajacy login z user_menu START */
 
function replaceloginbox() {
	$('li.login_link').click(function(){
		$('ul#user_menu a').each(function(i){
			$(this).removeAttr('href');
		})
		$('ul#user_menu').hide();
		$('ul#login_form').show();
	});
	$('li#close_login_form').click(function(){
		$('li#close_login_form a').removeAttr('href');
		$('a.register_forms_url').attr('href', '/userportal/register/');
		$('ul#user_menu').show();
		$('ul#login_form').hide();
		if($('#error_message:visible')){
			$('#error_message').hide();	
		}
		});
	$('input#id_username').click(function(){
			if($('#error_message:visible')){
				$('#error_message').hide();	
			}				
	});
	$('input#id_password').click(function(){
			if($('#error_message:visible')){
				$('#error_message').hide();	
			}
	});
	$('form.login-ajax[name=login]').submit(function(){
        $.get($('#login-ajax-action').val(),
              {username: $('#id_username').val(),
              password: $('#id_password').val()},
              function(data){
                    if (data.redirect_to){
                        window.location = data.redirect_to;
                    } else {
                        // trigger inFieldLabel
                        $('#id_password, #id_username').val("").blur();
                        if (data.error){
                            var errorContainer = $('<div id="error_message"><p class="error_message">' + data.error + '</p></div>');
                            $('form.login-ajax[name=login]').parent().append(errorContainer);
                        }
                    }
              },
              'jsonp');
        return false;
    });
    $("label.in-field-label").inFieldLabels({fadeOpacity: 0});
	$('input#id_username, form.login-ajax[name=login]').attr('autocomplete', 'off');
	/* firefox autocomplete fix */
	var clear = function(){
        if($('input#id_password').val())
          $('label[for="id_password"]').css('opacity', '0');
    };
	var deferredClear = function(){
		var h = setInterval(clear, 500);
		setTimeout(function(){ clearInterval(h); }, 30000);
	};
	$('input#id_username, input#id_password').mouseup(deferredClear);

}
			
/* Skrypt przelaczajacy login z user_menu STOP */ 

/*  Skrypt do wyszukiwarki slowo search START */
function searchdefaultvalue(search_words){
	if($('input[name="q"]').val() == ''){		
		$('input[name="q"]').val(search_words);
		$('input[name="q"]').click(function(){
    	$('input[name="q"]').val('');
		})
	}
	else{
		$('input[name="q"]').click(function(){
    	$('input[name="q"]').val('');
		})			
	}
}

/* Skrypt do wyszukiwarki slowo search */

/* Rozwijane submenu DOREJESTROWYWANIE UZYTKOWNIKA START */
function user_register_submenu(){
	$('.submenu_user_registration').hide();
	$('#comm2').click(function() {		
	$('.submenu_user_registration').slideToggle(50);
	return false;
	})
	$('.submenu_user_registration').mouseenter(function(){		
		$('.submenu_user_registration').show();
	})
	$('.submenu_user_registration').mouseleave(function(){
		$('.submenu_user_registration').hide();
	})
}
/* Rozwijane submenu DOREJESTROWYWANIE UZYTKOWNIKA STOP */

/* Rozwijane submenu Community START */
function communitysubmenu(){
	$('.submenu_community').hide();
	$('#comm').click(function() {		
	$('.submenu_community').slideToggle(50);
	return false;
	})
	$('.submenu_community').mouseenter(function(){		
		$('.submenu_community').show();
	})
	$('.submenu_community').mouseleave(function(){
		$('.submenu_community').hide();
	})
}
/* Rozwijane submenu Community STOP */

/* Pozycjonowanie obrazkow START */
function image_position(elem_id, img_height, elem_height){
	var elem_height_with_padding = (elem_height-2)
	if (img_height <= elem_height_with_padding) {
		margin_top = Math.ceil((elem_height - img_height) / 2);
		$('#' + elem_id).css({
			'margin-top': margin_top
		});
	}				
}
/* Pozycjonowanie obrazkow STOP */


/* Delay for menu */
function makeTall(){
	$(this).children('ul.selected_menu').css({display:'block'})
}
function makeShort(){		
	$(this).children('ul.selected_menu').css({display:'none'})
}
function delay_menu(){
	var config = {
		over: makeTall, // function = onMouseOver callback (REQUIRED)    
		interval: 250, // number = milliseconds delay before onMouseOut    
		out: makeShort // function = onMouseOut callback (REQUIRED)    
	};
	$('#bodymenu ul li').hoverIntent(config)
}
/* END OF Dealy for menu */
