
function downloads_itemLoadCallback(carousel, state){
	mycarousel_itemLoadCallback(carousel, state, '#downGenre', 'downloads');
}
function sales_itemLoadCallback(carousel, state){
	mycarousel_itemLoadCallback(carousel, state, '#salesGenre', 'sales');
}

function mycarousel_itemLoadCallback(carousel, state, genreId, type)
{
	selectedGenre = jQuery(genreId+' option:selected').text()
	if( state=='init' ){
		jQuery.post(
				window.location.href,
				{
					get_post_nr: 'true',
					genre: selectedGenre
				},
				
				function(data){
					carousel.size(data);
				},
				'html'
			);
		do_request(carousel.options.scroll, 0, carousel, selectedGenre, type);
	}else if(state=='next'){
		if(carousel.list.children()[carousel.first].innerHTML == ''){
			do_request(carousel.options.scroll, carousel.first-1, carousel, selectedGenre, type);
		}
	}
}
function do_request(nrItems, offset, carousel, selectedGenre, type){
	jQuery.post(
        window.location.href,
        {
        	limit: nrItems,
            offset: offset,
			genre: selectedGenre,
			type: type
        },
        function(data){
        	var items = data.split('___');
        	
        	for (i = 0; i < items.length; i++) {
        		carousel.add(offset+i+1, items[i]);
        	}
        },
        'html'
    );
}

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.search.replace('?', '').split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars[hash[0]] = hash[1];
    }
    return vars;
}
function paramsRestore(params){
	var out = new Array();
	i=0;
	for(key in params){
		if(key){
			out[i] = key+'='+params[key];
			i++;
		}
	}
	ret = out.join('&');
	if(ret){
		return '?'+out.join('&');
	}else{
		return '';
	}
}
function higlightMenu(menuItemLabel){
	var str= location.pathname;
	loc = str.split('/');
	for (var i = 0; i < loc.length; i++) {
		if(loc[i]==menuItemLabel)
			return true;
	}
	return false;
}

jQuery(document).ready(function() {
    jQuery('#downl_carousel').jcarousel({
        //itemVisibleOutCallback: {onAfterAnimation: asdfg},
        auto: 0,
		visible: 5,
		scroll: 5,
		itemLoadCallback: {onBeforeAnimation: downloads_itemLoadCallback}, 
		buttonNextHTML: '<div class="nextPage"></div>',
		buttonPrevHTML: '<div class="prevPage"></div>',
		initCallback:
	        function (carousel) {
			$('.nextPage', '#downl_carousel').bind('click', function() {
				carousel.next();
				return false;
			});
			$('#nextDownloads').bind('click', function() {
				carousel.next();
				return false;
			});
			/*$('.prevPage', '#downl_carousel').bind('click', function() {
				carousel.prev();
				return false;
			});*/
		}
    });
    jQuery('#sales_carousel').jcarousel({
    	auto: 0,
 		visible: 5,
 		scroll: 5,
 		itemLoadCallback: {onBeforeAnimation: sales_itemLoadCallback}, 
 		buttonNextHTML: '<div class="nextPage"></div>',
		buttonPrevHTML: '<div class="prevPage"></div>',
		initCallback:
	        function (carousel) {
			$('.nextPage', '#sales_carousel').bind('click', function() {
				carousel.next();
				return false;
			});
			$('#nextSales').bind('click', function() {
				carousel.next();
				return false;
			});
			/*$('.prevPage', '#downl_carousel').bind('click', function() {
				carousel.prev();
				return false;
			});*/
		}
    });
    jQuery('#downGenre, #salesGenre').change(function(){
    	params = getUrlVars();
    	if($(this).val()==1){
    		delete params['downloadg'];
    	}else{
    		params['downloadg'] = $(this).val(); 
    	}
    	url = location.href;
    	url = url.replace(location.search, '');
    	url = url+paramsRestore(params);
    	$(location).attr('href',url);
    });
    /*jQuery('#salesGenre').change(function(){
    	params = getUrlVars();
    	if($(this).val()==1){
    		delete params['salesg'];
    	}else{
    		params['salesg'] = $(this).val(); 
    	}
    	url = location.href;
    	url = url.replace(location.search, '');
    	url = url+paramsRestore(params);
    	$(location).attr('href',url);
    });*/
	if(higlightMenu('experts')){
		jQuery('#menu-item-213').addClass('current-menu-item');
	}
	if(higlightMenu('features')){
		jQuery('#menu-item-49').addClass('current-menu-item');
	}
	$(".currency").colorbox({width:"400px", height:"200px", iframe:true, close:'Close window'});
	$(".compgrid").colorbox({width:"786px", height:"676px", iframe:true, close:'Close window'});
});

