// custom functions
//var base_url = 'http://' + location.hostname + '/pmsite/shop/';
var base_url = 'http://' + location.hostname + '/shop/';

$(document).ready(function() {
	// suppress the errors on curvycorner
	var curvyCornersVerbose = false; 
	
    $("#product_name").html('N/A');
    $("ul.ul_content_homepage li").addClass("bordered");
    // when Your name field is focused
    $("#your_name").css('color', '#999');
    $("#your_name").click(function(){
        if($("#your_name").val() != ''){
            $("#your_name").val('')
            .css('color', '#333')
        }
    });
    $("#your_name").blur(function(){
        if($("#your_name").val() == ''){
            $("#your_name").val('Your Name')
            .css('color', '#999')
        }
    });
	
    // ------- Set the cart item by defaul ---------------- //
    var product_name 	= $("#product_name").val();
    var product_price 	= $("#p_price").val();
    var product_qty 	= $("#product_qty").val();
    // for payment summary display details
    $("#prdct_name").html(product_name);
    $("#prdct_qty").html(product_qty);
    $("#prdct_price").html("$" + product_qty * product_price);
    // --- for google settings ------- //
    $("#item_name_1").val(product_name);
    $("#item_quantity_1").val(product_qty);
    $("#item_price_1").val(product_price);

    // search button
    $("#search_p").click(function(){
        var val = $("#search_field").val(), newVal = val.replace(' ', '_');
        if($.trim(val) != ''){
            $("#order-item-loader").ajaxStart(function(){$(this).show();});
            $("#order-item-loader").ajaxStop(function(){$(this).hide();});
            $("#product_lists").load(base_url + 'admin/admin/search_product/' + newVal);
        }
        else{
            alert('Please specify a product to search.');
        }
    });

    $("#p_color").val($("#p_color_val").val());
    $("#p_size").val($("#p_size_val").val());
	
});

function the_color(val){
    $("#p_color").val(val);
}

function the_size(val){
    $("#p_size").val(val);
}

function the_price_by_size(val){
	$("#p_size").val(val);
}

function toggleSubMenu(main_id, sub_id){
    $("#"+main_id).toggle(
        function(){
            $("#"+sub_id).css('display', 'block');
        },
        function(){
            $("#"+sub_id).css('display', 'none');
        }
        );
}


// Update the checkout setting when onchange event happens
function update_cart(){
    var product_name 	= $("#product_name").val();
    var product_price 	= $("#p_price").val();
    var product_qty 	= $("#product_qty").val();
    // for payment summary display details
    $("#prdct_name").html(product_name);
    $("#prdct_qty").html(product_qty);
    $("#prdct_price").html("$" + product_qty * product_price);
    // for google settings
    $("#item_name_1").val(product_name);
    $("#item_quantity_1").val(product_qty);
    $("#item_price_1").val(product_price);
}

/*function imageThumbClick(img_src){
	$("#previewImage").attr("alt", base_url + 'product_images/' + img_src);		  
}*/

function imageThumbClick(img_id){
	
    // set for the normal image preview
    $("#loading_for_img").ajaxStart(function(){
        $(this).show();
    });
    $("#loading_for_img").ajaxStop(function(){
        $(this).hide();
    });
    
    $.ajax({
        type: "POST",
        url: base_url + "home/load_image",
        data: "img_id_=" + img_id,
        success: function(){
            $("#load_image").load( base_url + 'home/load_image/'+ img_id);
        }
    });
	
}

function mainImageThumbClick(img_id){

    // set for the normal image preview
    $("#loading_for_img").ajaxStart(function(){
        $(this).show();
    });
    $("#loading_for_img").ajaxStop(function(){
        $(this).hide();
    });

    $.ajax({
        type: "POST",
        url: base_url + "home/load_main_image",
        data: "img_id_=" + img_id,
        success: function(){
            $("#load_image").load( base_url + 'home/load_main_image/'+ img_id);
        }
    });

}

function addClass(id){
    $("ul.ul_content_homepage li").removeAttr("class");
    $("ul.ul_content_homepage li").addClass("bordered");
    $("#"+id).addClass('pic_selected');
    
}

// ------------ POST REVIES ----------------- //
function postReview(p_id, p_name){
    // Set ajax loading img
    $("#loading").ajaxStart(function(){
        $(this).show();
    });
    $("#loading").ajaxStop(function(){
        $(this).hide();
    });
    //alert(p_name); return false;
    var review_body = $("#reviews").val();
    var your_name	= $("#your_name").val();
    if($.trim(review_body) != '' && your_name != 'Your Name'){
        $(".btn-review").val("Posting...");
        $.ajax({
            type: "POST",
            url: base_url + "home/post_review",
            data: "review=" + review_body + "&p_id=" + p_id + "&y_name=" + your_name,
            success: function(){
                $("#display_reviews").load( base_url + "home/retrive_post_review/" + p_name);
                $("#reviews").val('');
                $(".btn-review").val("Post Review");
            }
        });
        return true;
    }else{
        return false;
    }
}

// display multiple file inputs
function displayInputs(){
    var num = $("#num_img").val();
    var html = '';
    if($.trim(num) != ''){
        for(var i=1;i<=num; i++){
            html += '<label for="image">Image ' +i+ '</label><input type="file" name="p_image[]" /><br />';
        }
        html += '<p><label for="button">&nbsp;</label><input type="submit" name="submit" value="Submit" /></p>';
        $("#display_file_inputs").html(html);
    }else{
        alert('Please enter a number');
    }
	
}

// Located @ admin
function changeProductPhoto(id){
    $("#"+id).css('display', 'block');
}
function hideChangeProductPhoto(id){
    $("#"+id).css('display', 'none');
}

function colapseinfo(cnt){
    var id = $("#admin-product-info-"+cnt);
    if(id.attr('class') == 'admin-product-info-hide'){
        id.slideDown('slow');
        $("#collapse-label-"+cnt).html('- hide');
        id.attr('class', 'admin-product-info-show');
    }
    else if(id.attr('class') == 'admin-product-info-show'){
        id.slideUp('slow');
        id.attr('class', 'admin-product-info-hide');
        $("#collapse-label-"+cnt).html('+ show');
    }
}

function changeProductCategory(val){
    if(val != ''){
        // loading icon
        $("#loading_for_update_cat").ajaxStart(function(){
            $(this).show();
        });
        $("#loading_for_update_cat").ajaxStop(function(){
            $(this).hide();
        });
		
		if(val == 6){
			$("#photographer").show();
		}

        $.ajax({
            type: "POST",
            url: base_url + "admin/admin/updateCategory",
            data: "val=" + val,
            success: function(){
                $("#update-cat").load( base_url + 'admin/admin/updateCategory/'+ val);
            }
        });
        return true;
    }
    else{
        return false;
    }
}

function order_up(count, p_id, cat_id){
    $("#order-item-loader").ajaxStart(function(){
        $(this).show();
    });
    $("#order-item-loader").ajaxStop(function(){
        $(this).hide();
    });
    
    $.ajax({
        type: "POST",
        url: base_url + "admin/admin/order_item",
        data: "count=" + count + '&p_id=' + p_id + '&cat_id=' +cat_id + '&action=up',
        success: function(){
            $("#product_lists").load( base_url + 'admin/admin/load_product_list').highlightFade();
        }
    });
}

function order_down(count, p_id, cat_id){
    $("#order-item-loader").ajaxStart(function(){
        $(this).show();
    });
    $("#order-item-loader").ajaxStop(function(){
        $(this).hide();
    });
    
    $.ajax({
        type: "POST",
        url: base_url + "admin/admin/order_item",
        data: "count=" + count + '&p_id=' + p_id + '&cat_id=' +cat_id + '&action=down',
        success: function(){
            $("#product_lists").load( base_url + 'admin/admin/load_product_list').highlightFade();
        }
    });
}

function order_size_up(size_id, cur_order){
    // start ajax
    // -1
    $.ajax({
        type: "POST",
        url: base_url + "admin/admin/save_size_order",
        data: "size_id=" + size_id + '&action=up&order=' + cur_order,
        success: function(){
            $("#row-" + cur_order).after($("#row-" + cur_order).prev(".c_sizes")).highlightFade();
            $("#content_lists").load(base_url + 'admin/admin/load_sizes');
            //alert(msg);
        }
    });
}

function order_size_down(size_id, cur_order){
    // start ajax
    // +1
    $.ajax({
        type: "POST",
        url: base_url + "admin/admin/save_size_order",
        data: "size_id=" + size_id + '&action=down&order=' + cur_order,
        success: function(){
            $("#row-" + cur_order).before($("#row-" + cur_order).next(".c_sizes")).highlightFade();
            $("#content_lists").load(base_url + 'admin/admin/load_sizes');
            //alert(msg);
        }
    });
}


/*
 * Add to cart Animation
 */
 (function($) {

	$.extend({
		add2cart: function(source_id, target_id, callback) {
    
      var source = $('#' + source_id );
      var target = $('#' + target_id );
      
      var shadow = $('#' + source_id + '_shadow');
      if( !shadow.attr('id') ) {
          $('body').prepend('<div id="'+source.attr('id')+'_shadow" style="display: none; background-color: #ddd; border: solid 1px darkgray; position: static; top: 0px; z-index: 999;">&nbsp;</div>');
          var shadow = $('#'+source.attr('id')+'_shadow');
      }
      
      if( !shadow ) {
          alert('Cannot create the shadow div');
      }
      
      shadow.width(source.css('width')).height(source.css('height')).css('top', source.offset().top).css('left', source.offset().left).css('opacity', 0.5).show();
      shadow.css('position', 'absolute');
      
      shadow.animate( { width: target.innerWidth(), height: target.innerHeight(), top: target.offset().top, left: target.offset().left }, { duration: 300 } )
        .animate( { opacity: 0 }, { duration: 100, complete: callback } );
        
		}
	});
})(jQuery);
/*
 * End: Add to cart Animation
 */
