﻿//---------------------------------------------
// BlogControl.js 
// Copyright (C) Jetshop AB 2009
//---------------------------------------------
Type.registerNamespace('JetShop.StoreControls'); 

JetShop.StoreControls.BlogControl = function(element)
{
    JetShop.StoreControls.BlogControl.initializeBase(this, [element]);
    
    this._blogControlSucceedHandler = null;
    this._failedHandler = null;
    
    this._codeSnippet = null;
    this._imageWidth = null;
    this._imageMaxWidth = null;
    this._linkUrl = null;
    this._linkAlternateText = null;
    this._imageUrl = null;
    this._width = null;
    this._height = null;
}

JetShop.StoreControls.BlogControl.prototype = 
{
    initialize : function() 
    {
        // initialize the base
        JetShop.StoreControls.BlogControl.callBaseMethod(this,'initialize');
        
        var strCodeSnippet = this._codeSnippet;
        var strImageWidth = this._imageWidth;
        var strImageMaxWidth = this._imageMaxWidth;
        var strWidth = this._width;
        var strHeight = this._height;

        $(document).ready(function()
        { 
            /* Using custom settings */ 
            $("a#inline").fancybox({
                'hideOnContentClick': false,
                'frameWidth': parseInt(strWidth),
                'frameHeight': parseInt(strHeight),
                'callbackOnShow': initializeGrid
            });
        });

        function initializeGrid() {
            $("#grid_slider").slider({
                value: strImageWidth,
                max: strImageMaxWidth,
                min: 1,
                slide: function(event, ui) {
                    $('#imgProductImage').css('width',ui.value+"px");
                    $("#inputImageSize").attr("value", ui.value);
                    
                    $("#inputCodeSnippet").attr("value", strCodeSnippet.replace("<!imageWidth!>", ui.value));
                }
            });

            $("#inputCodeSnippet").attr("value", strCodeSnippet.replace("<!imageWidth!>", strImageWidth));

            $("#inputImageSize").keypress(function (e) {
                // If letter not digit then display error and type nothing
                if (e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
                {
                    // Display error message
                    $("#imageSizeErrorMessage").html("Digits Only").show().fadeOut("slow");
                    return false;
                }
            });

            $("#inputImageSize").bind("change keyup", function() { 
                var strWidth = $("#inputImageSize").val();
                
                if (strWidth == '0' || strWidth == '')
                {
                    strWidth = 1;
                    $("#inputImageSize").attr("value", strWidth);
                }
                
                if (strWidth > parseInt(strImageMaxWidth))
                {
                    strWidth = strImageMaxWidth;
                    $("#inputImageSize").attr("value", strWidth); 
                }
                
                $('#imgProductImage').css('width',strWidth+"px");
                $('#grid_slider').slider('option', 'value', strWidth);
                
                // Replace codesnippet with width
                $("#inputCodeSnippet").attr("value", strCodeSnippet.replace("<!imageWidth!>", strWidth));
            });
        }
    },
    
    get_CodeSnippet : function()
    {
		return this._codeSnippet;
    },
    
    set_CodeSnippet : function(value)
    {
		this._codeSnippet = value;
    },
    
    get_ImageWidth : function()
    {
		return this._imageWidth;
    },
    
    set_ImageWidth : function(value)
    {
		this._imageWidth = value;
    },

    get_ImageMaxWidth : function()
    {
		return this._imageMaxWidth;
    },
    
    set_ImageMaxWidth : function(value)
    {
		this._imageMaxWidth = value;
    },
    
    _onSucceededBlogControl : function(result, controlID, methodName) 
	{
		alert('succeed');
	},

	_onFailed : function(result, controlID, methodName) 
	{
	    alert('failed');
	},
    
    get_Width : function()
    {
		return this._width;
    },
    
    set_Width : function(value)
    {
		this._width = value;
    },
    
    get_Height : function()
    {
		return this._height;
    },
    
    set_Height : function(value)
    {
		this._height = value;
    },        
    
    dispose : function() 
    {
        // call to the base to do its dispose
        JetShop.StoreControls.BlogControl.callBaseMethod(this,'dispose'); 
    }
}

JetShop.StoreControls.BlogControl.registerClass('JetShop.StoreControls.BlogControl', Sys.UI.Control);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();