﻿function bindProducts(){
    // ha szerepel az urlben product akkor megjelenitjuk
    if(document.URL.match('#prod-')) {
        var aUrl = document.URL.split('#prod-');
        showProduct(aUrl[1]);
    }
    
	$('.product').click(
		function(){
		    showProduct($(this).attr('id'));
		});
}

var sLastProduct = "";

function showProduct(sProduct){
    /*
    var requestUrl = document.URL + "&code=" + sProduct + "&ajax=true";
        $.ajax({
            type: "GET",
            url: requestUrl,
            success: function(msg){
                alert(msg.getElementsByTagName('result')[0].text);
            }
        });
    */
    updateUrl(sProduct);
    
    var sSrc = $('#'+sProduct).children('img').attr('src');
    var aProd = ($('#'+sProduct).children('p').html()).split(', ');
    var sCode = aProd[0];
    var sName = aProd[1];
    
    $('#activeimg').children('img').attr('src', sSrc);
    $('#activeimg').children('img').attr('alt', sCode+' '+sName);
    $('#activeproduct').children('h2').html(sCode+'<br />'+sName);
    
    if(sLastProduct == sProduct){
        if($('#'+sProduct).hasClass('active')){
            $('#activeproduct').removeClass('active');    
            $('#'+sProduct).removeClass('active');
            updateUrl('');
        }else{
            $('#activeproduct').addClass('active');
            $('#'+sProduct).addClass('active');
        }
    }else{
        if($('#'+sLastProduct).hasClass('active'))
            $('#'+sLastProduct).removeClass('active');
            
        $('#'+sProduct).addClass('active');
        $('#activeproduct').addClass('active');
    }
    sLastProduct = sProduct;
}

function updateUrl(sProduct){
    var sUrl = document.URL;
    
    if(document.URL.match('#')) {
        var aUrl = document.URL.split('#');
        sUrl = aUrl[0];
    }
    sProduct = (sProduct == '' ? '#' : '#prod-' + sProduct);
    window.location.href = sUrl + sProduct;
}