/*	-------------------------------------------------------------
	Cratom.com
	ecommerce
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	Base setup javascript
	Filename:		main.js
	Version:		1.0
	Date:			March 29, 2011
	-------------------------------------------------------------	*/
$(document).ready(function() {

/* Function tabs product file *////////////////////////////////////////
    //Default Action
    $(".tab_content").hide(); //Oculta el contenido
    $("ul.tabs li:first").addClass("active").show(); //Activa el primer tab
    $(".tab_content:first").show(); //Muestra el contenido del primer tab
    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("active"); //Elimina clase active
        $(this).addClass("active"); //Añade case active al tab seleccionado
        $(".tab_content").hide(); //Oculta todos los contenidos de los tabs
        var activeTab = $(this).find("a").attr("href"); 
        $(activeTab).fadeIn(); //Fade in del contenido
        return false;
    });
/* end of Function tabs product file */

/* Function open shopping cart content *////////////////////////////////////////  
    $(".btn-scart").bind("click", function(){ // Al clicar en el botón carro
        document.location.href = '/order';
    });
/* end of Function open shopping cart content */

/* Function add product to cart effect *////////////////////////////////////////
    $(".addthis").bind("click", function(){
        $prodid= $(this).attr ("add");
        if ($("#add-units").val())
            $units = $("#add-units").val();
        else
            $units = 1;
        insertLine($prodid.replace('prod', ''), $units);
        $("#"+$prodid).effect("transfer", {to: ".btn-scart"}, 700);
        $(".btn-scart").addClass("full");
        $(".btn-scart").effect("bounce", 500);
        $(".btn-scart p").show();
    });
/* end of Function add product to cart effect */

/* Function create user show invoice data *////////////////////////////////////////
    $("#cu-invoice-yes").bind("click", function() {
        $iddomicilio = $("#cu-address").val();
        $idpais = $("#cu-country").val();
        $idprovincia = $("#cu-prov").val();
        $idpoblacion = $("#cu-pob").val();
        $idcp = $("#cu-cp").val();
        $("#cu-id-address").val($iddomicilio);
        $("#cu-id-country").val($idpais);
        $("#cu-id-prov").val($idprovincia);
        $("#cu-id-pob").val($idpoblacion);
        $("#cu-id-cp").val($idcp);
        $(".invoice-data").show ();
    });
    $("#cu-invoice-no").bind("click", function() {
        $(".invoice-data").hide ();
    });
/* end of Function create user show invoice data */

/* Function show select language box *////////////////////////////////////////
    $(".btn-languages").bind("click", function() {
        $langel= $("#container-lang > ul");
        $estado= $langel.css ("display");
        if ($estado == "none")  {
            $langel.fadeIn ();
            $(".btn-languages").addClass("up");
        } else {
            $langel.fadeOut ();
            $(".btn-languages").removeClass("up");
        }
    });
/* end of Function show select language box */

/* Function banners slider home *////////////////////////////////////////
    $('#slider').nivoSlider({
        effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
        slices:15, // For slice animations
        boxCols: 8, // For box animations
        boxRows: 4, // For box animations
        animSpeed:1000, // Slide transition speed
        pauseTime:5000, // How long each slide will show
        startSlide:0, // Set starting Slide (0 index)
        directionNav:true, // Next & Prev navigation
        directionNavHide:true, // Only show on hover
        controlNav:true, // 1,2,3... navigation
        controlNavThumbs:false, // Use thumbnails for Control Nav
        controlNavThumbsFromRel:false, // Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', // Replace this with...
        controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
        keyboardNav:true, // Use left & right arrows
        pauseOnHover:true, // Stop animation while hovering
        manualAdvance:false, // Force manual transitions
        captionOpacity:0.8, // Universal caption opacity
        prevText: 'Prev', // Prev directionNav text
        nextText: 'Next', // Next directionNav text
        beforeChange: function(){}, // Triggers before a slide transition
        afterChange: function(){}, // Triggers after a slide transition
        slideshowEnd: function(){}, // Triggers after all slides have been shown
        lastSlide: function(){}, // Triggers when last slide is shown
        afterLoad: function(){} // Triggers when slider has loaded
    });
/* end of Function banners slider home */

/* Function effect sidebar categories menu *////////////////////////////////////////
    // Anyade icono desplegar si lo necesita
    $('ul li:has(ul)').addClass ('submore');
    // Muestra y oculta los menus
    $('ul li:has(ul)').click(function(){
        var catid= $(this).attr("rel");
            if ($('#'+catid).css('display')=='none'){
                $(this).find('ul').slideDown();
                $(this).removeClass("submore").addClass("subminus");
            }else if ($('#'+catid).css('display')=='block'){
                $(this).find('ul').slideUp();
                $(this).removeClass("subminus").addClass("submore");
            }
    });
/* end of Function effect sidebar categories menu */

/* Trigger zoombox *////////////////////////////////////////
    $(function(){
        $('a[rel=zoombox]').zoombox();
    });
/* end of Trigger zoombox */

/* product factsheet total price *////////////////////////////////////////
   $(".fsprod-amount input").bind("change", function() {
        $cantidad = $(".fsprod-amount input").val();
        $precio= $(".total-tax").attr ("rel");
        $cantidad = parseFloat($cantidad);
        $precio = parseFloat($precio.replace('.','').replace(',','.'));
        $preciototal = $cantidad * $precio;
        $preciototal = thousands($preciototal.toString());
        
        $(".total-tax").html ($preciototal);
   });
/* end of product factsheet total price */

/* product factsheet print function *////////////////////////////////////////
    $(".print").bind("click", function() {
        window.print()
    });
/* end of product factsheet print function */

/* product factsheet btn see gallery function *////////////////////////////////////////
    $(".seegallery").bind("click", function() {
        $("ul.tabs li").removeClass("active"); 
        $(".tab_content").hide();
        var activeTab = $(this).attr("href");
        $(activeTab).fadeIn();
        $("ul.tabs li.t3").addClass("active");
        return false;
    });
/* end of factsheet btn see gallery function */


/* refresh states with country
 * En el formulario de registro
 *   */
    $("#cu-country").change(function () {

        // peticion obtener las provincias de este pais
        $.ajax({url: siteUrl + '/ws/states?id=' + $("#cu-country").val()})
            .success(function(resp, status, objeto) {
                var content = '<select id="cu-prov" class="select" name="state"><option>' + textState + '</option>';
                $(resp).find('state').each(function() {
                    content += '<option value="' + $(this).attr('id') + '">' + $(this).text() + '</option>';
                });
                content += '</select>';
                $("#cu-prov").replaceWith(content);

            });

    });
/* end refresh states */

/* refresh states with country 
 * En el formulario de contacto extendido
 * */
    $("#dinpages-country").change(function () {

        // peticion obtener las provincias de este pais
        $.ajax({url: siteUrl + '/ws/states?id=' + $("#dinpages-country").val()})
            .success(function(resp, status, objeto) {
                var content = '<select id="dinpages-prov" class="select" name="state"><option>' + textState + '</option>';
                $(resp).find('state').each(function() {
                    content += '<option value="' + $(this).attr('id') + '">' + $(this).text() + '</option>';
                });
                content += '</select>';
                $("#dinpages-prov").replaceWith(content);

            });

    });
/* end refresh states */

/* Muestra el formulario de respuesta y oculta el boton de responder al admin */
$("#show-response").click(function (event) {
    event.preventDefault();
    $("#response-space").show();
    $("#btngo-response").hide();
    $("#message-area").focus();
});
/* end boton responder al admin */

/* Muestra qtip en los overs de las preguntas del faq */
    $('.mod-help-faq li a[title]').removeData('qtip')
    .qtip({
         position: {
            my: 'top right'
        },
        style: {
            classes: 'ui-tooltip-shadow ui-tooltip-' + 'light'
        }
    });
/* end Muestra qtip en los overs de las preguntas del faq */

/* Función boton cierra ventana de información */
    $(".btn-closemessage").click(function (event) {
        $(".info-message").hide();
    });
/* Función boton cierra ventana de información */
});


function thousands(nStr)
{
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + '.' + '$2');
	}
	return nStr;
}

