$.tabs = function(selector, start) {
	$(selector).each(function(i, element) {
		$($(element).attr('tab')).css('display', 'none');
		
		$(element).click(function() {
			$(selector).each(function(i, element) {
				$(element).removeClass('selected');
				
				$($(element).attr('tab')).css('display', 'none');
			});
			
			$(this).addClass('selected');
			
			$($(this).attr('tab')).css('display', 'block');
		});
	});
	
	if (!start) {
		start = $(selector + ':first').attr('tab');
	}

	$(selector + '[tab=\'' + start + '\']').trigger('click');
};

$(document).ready(function(){
    $('.color_version').live('change',function(){
        var id = $(this).val();
        var price = $('#color_price_'+id).val();
        var popup = $('#color_images_'+id).val();
        var thumb = $('#color_images_'+id).attr('title');
        var model = $('.color_version :selected').html();

        if(!price) price = $('#color_price_0').val(); //if new price isnt set old one is used
        
        $('#td_price').html(price);
        $('#image').attr('src',thumb);
        $('#a_popup').attr('href',popup);
        $('#model_name').html(model);
    });

    //finding hidden pictures in product list and displaying it
    var time = 4000;
    $('.vis').each(function(){
	var id = $(this).parent().parent().attr('id');	
	setInterval('showPicture("'+id+'");',time);
    });
});

function showPicture(id){
    var vis = $('#'+id+' .vis');
    var next = $(vis).next();
    var eid = null;
   
    if(next.attr('class') == 'hid') {
	vis.attr('class','hid');
	vis.hide();
	next.fadeIn('slow');
	next.attr('class','vis');
	changeModelAndPrice(next.attr('id'));

    } else {
	var prev = $(vis).prev();
	if(prev.attr('class') == 'hid') {
	    vis.attr('class','hid');
	    vis.hide();
	    var elem = $('#'+id+' .hid:first-child ');
	    elem.fadeIn('slow');
	    elem.attr('class','vis');
	    changeModelAndPrice(elem.attr('id'));
	}
    }    
}

function changeModelAndPrice(eid){    
    var model = $('#'+eid+'_h');
    var model_elem = $(model).parent().find('.txt_model');
    var price_elem = $(model).parent().find('.price');
    var promo_price =$(model).parent().find('.promo_price');
    if(price_elem.attr('class') != 'price') {
	price_elem = $(model).parent().find('.old_price');
	price_elem.attr('class','price');
    }
    model_elem.html(model.attr('alt'));

    //if new price exists
    if(parseFloat(model.val()) != 0 ){
	price_elem.html(model.val());

	//preparing specjal price
	var new_promo_price = model.attr('title');

	if(parseFloat(new_promo_price)!=0 && new_promo_price != '') {
	    if(promo_price.attr('class') == 'promo_price') {
		promo_price.html(new_promo_price);
	    } else {
		price_elem.after('<span class="promo_price">'+new_promo_price+'</span>');
	    }
	    price_elem.attr('class','old_price');
	} else {	    
	    if(promo_price.attr('class') == 'promo_price'){
		promo_price.remove();
	    }
	}
    } else if(promo_price.attr('class') == 'promo_price'){
		promo_price.remove();
    }
}

