jQuery.noConflict();
//------------------------------------------------------------------------------
//   AJAX functions for manufacturers select
//------------------------------------------------------------------------------

function loadProductTab(product_id, tab_name, element)
{
    if(element != null)
    {
        nodes = $('tabs').getElementsBySelector('a');        
        for(var i = 0; i < nodes.length; i++)
        {
            nodes[i].removeClassName('act');
        }
        element.addClassName('act');
    }
    var url ='index.php';
    var pars ='page=ajaxresponse/load_product_tab_json&productId='+product_id+'&tabName='+tab_name+'&rid='+Math.random();

    var myAjax = new Ajax.Request(
	url, 
	{
		method: 'post', 
		parameters: pars,
		onComplete: showProductTab
	});
}
//------------------------------------------------------------------------------
function showProductTab(originalRequest)
{
    data = originalRequest.responseText.evalJSON();
    
    var tabBody = $('tab_content');
    tabBody.update(null);

    if(data['tab_name'] == 'reviews')
    {
        if(data["userId"] != false)
        {
            tabBody.appendChild(Element('a',{href:"javascript:loadProductTab('" + data['product_id'] + "', 'add_review',null);"}).update(data["notices"]["add_review"]));
        }
        else
        {
            tabBody.appendChild(Element('p').addClassName('notice').update(data["notices"]["need_to_be_logged"]));
        }

        if(data["reviewsCount"] > 0)
        {
           tabBody.appendChild(Element('p').update(data["notices"]["average_rating"] + ": <strong>" + data.reviews["averageRating"] + "</strong> of <strong>" + data.reviews['totalCount'] + "</strong> reviews."));
           for (var i = 0; i < data['reviewsCount']; i ++)
           {
                var review = new Element ('div').addClassName('review');
                review.appendChild(Element('div').addClassName('rauthor').update(data.reviews['list'][i]["user"]["first_name"] + " " + data.reviews['list'][i]["user"]["last_name"]));
                review.appendChild(Element('div').addClassName('rcomment').update(data.reviews['list'][i]["details"]));
                tabBody.appendChild(review);
           }
        }
        else
        {
            tabBody.appendChild(Element('p').addClassName('notice').update(data["notices"]["no_reviews"]));
        }
        
    }
    else if(data['tab_name'] == 'add_review')
    {
       var form =  new Element('form',{action:WEB_ROOT + '/index.php', method:'post'});
       form.appendChild(Element('input',{name:'page', value:'product_details', type:'hidden'}));
       form.appendChild(Element('input',{name:'command', value:'saveReview', type:'hidden'}));
       form.appendChild(Element('input',{name:'productid', value:data['product_id'], type:'hidden'}));

       var table = new Element('table');
       var tr1 = new Element('tr');
       tr1.appendChild(Element('th',{colspan:'2'}).update(data["notices"]["create_a_new_review"]));
       var tr2 = new Element('tr');
       tr2.appendChild(Element('th').update(data["notices"]["rating"]));
       var th = new Element('th');
       var select = new Element('select', {name:'rating'});
       for(var i = 0; i < 10; i++)
       {
           select.options[i] = new Option(i+1,i+1);
       }
       th.appendChild(select);
       tr2.appendChild(th);
       var tr3 = new Element('tr');
       tr3.appendChild(Element('th',{colspan:'2'}).update(data["notices"]["description"]));
       var tr4 = new Element('tr');
       var th2 = new Element('th',{colspan:'2'});
       th2.appendChild(Element('textarea',{name:'details', cols:'40', rows:'10'}));
       tr4.appendChild(th2);
       table.appendChild(tr1);
       table.appendChild(tr2);
       table.appendChild(tr3);
       table.appendChild(tr4);
       form.appendChild(table);

       form.appendChild(Element('input',{value:data["notices"]["submit"], type:'submit'}).addClassName('button'));
       tabBody.appendChild(form);
    }
    else
    {
        tabBody.update(data['text']);
    }
}
//------------------------------------------------------------------------------
function handleClick(link, action, list) {

    if(action == "show")
    {
       $(link).removeClassName('show');
       $(link).addClassName('hide');
       $(link).href = "javascript:handleClick('" + link + "','hide','"+list+"')";
       $(list).show();
       
    }
    else
    {
       $(link).removeClassName('hide');
       $(link).addClassName('show');
       $(link).href = "javascript:handleClick('" + link + "','show','"+list+"')";
       $(list).hide();
    }
}
//------------------------------------------------------------------------------
function loadProductImages(product_id, content_type)
{
    var url ='index.php';
    var pars ='page=ajaxresponse/load_product_images_json&productId='+product_id+'&contentType='+content_type+'&rid='+Math.random();

    var myAjax = new Ajax.Request(
	url, 
	{
		method: 'post', 
		parameters: pars,
		onComplete: showProductImage
	});
}
//------------------------------------------------------------------------------
function showProductImage(originalRequest)
{
    data = originalRequest.responseText.evalJSON();
    
    if($('TB_window')) {
    AppearEffect();
    }
    else {
    var windowImage = new Element('div', {id:'TB_window'});
    var titleBar = new Element('div').addClassName('TB_title');
    var closeBar = new Element('div').addClassName('TB_closeAjaxWindow');
    closeBar.appendChild(Element('a', {id:'TB_closeWindowButton'}).update('close'));
    titleBar.appendChild(closeBar);
    windowImage.appendChild(titleBar);
   
    var ajaxContent = new Element('div').addClassName('TB_ajaxContent');
    var mainImage = new Element('div', {id:'mimage'});
    var imagePreview = new Element('div', {id:'main_image'});
    var zoomLink = new Element('a', {href:data['images']['list'][0]['original_size'], rel:'zoom-color:#E19E00', id:'Magnifier1'}).addClassName('MagicMagnify');
    zoomLink.appendChild(Element('img',{src:data['images']['list'][0]['regular_size']}));
    imagePreview.appendChild(zoomLink);
    imagePreview.appendChild(Element('p').update(data['notice']));
    mainImage.appendChild(imagePreview);
    mainImage.appendChild(Element('div',{id:'video_review'}).update(data['video']));
    ajaxContent.appendChild(mainImage);

    var imageWrapper = new Element('div',{id:'mimages-wrapper'});
    var images = new Element('div', {id:'mimages'});
    for(var i = 0; i < data['images']['list'].length; i++)
    {
        var regular_image = data['images']['list'][i]['regular_size'];
        var image = new Element('a',{href:data['images']['list'][i]['original_size'], rel:'Magnifier1',rev:data['images']['list'][i]['regular_size']}).observe('click',(function(){$('video_review').hide(); $('main_image').show();}));
        image.appendChild(Element('img', {src:data['images']['list'][i]['small_size']}));
        images.appendChild(image);
    }
    if(data['video'] != '' && data['video'] != null)
    {
        var video = new Element('a',{id:"videoPrieview"});
        video.appendChild(Element('img', {src:WEB_ROOT + '/templates/layouts/custom/styles/default/images/img-video-review.jpg'}));
        images.appendChild(video);    
    }
    imageWrapper.appendChild(images);
    ajaxContent.appendChild(imageWrapper);

    windowImage.appendChild(ajaxContent);
    windowImage.hide();
    document.body.appendChild(windowImage);
    setTimeout('MagicMagnify_findMagnifiers()',750);
	setTimeout('AppearEffect()',500);
   }
    if(data['show'] == 'video')
    {
        $('video_review').show();
        $('main_image').hide();
    }
    else
    {
        $('video_review').hide();
        $('main_image').show();
    }
   	$('TB_closeWindowButton').observe('click',(function(){Effect.Fade('TB_window') }))
   	$('videoPrieview').observe('click',(function(){$('main_image').hide(); $('video_review').show(); }))

}
function changeImage(link)
{
    $('main_image').src=link;
    $('main_image').show();
    $('video_review').hide();
}

//------------------------------------------------------------------------------
function changeProductImage(link)
{
    $('mainPicture').src=link;
}
//------------------------------------------------------------------------------
function loadProductVideo(product_id)
{
    var url ='index.php';
    var pars ='page=ajaxresponse/load_product_video_json&productId='+product_id+'&rid='+Math.random();

    var myAjax = new Ajax.Request(
	url, 
	{
		method: 'post', 
		parameters: pars,
		onComplete: showProductVideo
	});
}
//------------------------------------------------------------------------------
function showProductVideo(originalRequest)
{
    data = originalRequest.responseText.evalJSON();
    
    if($('TB_window_video')) {
    $('TB_window_video').show();
    }
    else {
    var windowVideo = new Element('div', {id:'TB_window_video'});
    var titleBar = new Element('div').addClassName('TB_title');
    var closeBar = new Element('div').addClassName('TB_closeAjaxWindow');
    closeBar.appendChild(Element('a', {id:'TB_closeWindowButton2'}).update('close'));
    titleBar.appendChild(closeBar);
    windowVideo.appendChild(titleBar);
    windowVideo.appendChild(Element('div').addClassName('TB_ajaxContent').update(data));
    document.body.appendChild(windowVideo);
    }
    $('TB_closeWindowButton2').observe('click',(function(){ $('TB_window_video').hide() }))  
}
//------------------------------------------------------------------------------
function addToBasket(product_id)
{
    var url ='index.php';
    //var amount = 1;
    var pars ='page=ajaxresponse/get_basket_products_json&productid='+product_id+'&amount=1&variantids=&rid='+Math.random();

    var myAjax = new Ajax.Request(
	url, 
	{
		method: 'post', 
		parameters: pars,
		onComplete: showBasketProducts
	});
}
//------------------------------------------------------------------------------
function showBasketProducts(originalRequest)
{
    data = originalRequest.responseText.evalJSON(); 
    
    $('product-count').update(data['product_count']);
    $('basket-amount').update(data['basket_amount']);
    if($('message1') == null && data['shipping_info'] && $('right-info-block'))
    {
    	//alert(data['shipping_info']);
    	$('right-info-block').insert({'top':new Element('h4',{id:'message1'}).update(data['shipping_info'])});
    }
    if($('shopping-bag-products'))
    {
       var products = $('shopping-bag-products');
       products.update(null);
    }
    else
    {
    	var products = new Element('div', {id:'shopping-bag-products'});
    }
    products.appendChild(Element('p').update('Items in cart:').setStyle({fontWeight:'bold'}));
    for(var i = 0; i < data['list'].length; i++)
    {
        var product = new Element('p').update(data['list'][i]['product']['name'] + ", " + data['list'][i]['amount'] + " x $ " + data['list'][i]['product_price']);
        if(i > 0)
        {
        	product.setStyle({borderTop:'1px solid #a2a2a2'});
        }
        products.appendChild(product);
    }
    products.hide();
    if(data['list'].length > 0)
    {
    blockHeight = 19 + (20 * data['list'].length);
    products.setStyle({height:blockHeight+"px"});
    $('hcbc').appendChild(products);
    Effect.BlindDown('shopping-bag-products');
    setTimeout('HideBasketProducts()', 5000);
    }
}

function HideBasketProducts()
{
    Effect.BlindUp('shopping-bag-products');
}

function AppearEffect()
{
 	Effect.Appear('TB_window', { duration: 1.5 });  
}




