/*
* 	Easy Slider 1.7 - jQuery plugin
*	written by Alen Grakalic	
*	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
*
*	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
*	Dual licensed under the MIT (MIT-LICENSE.txt)
*	and GPL (GPL-LICENSE.txt) licenses.
*
*	Built for jQuery library
*	http://jquery.com
*
*/

/*
*	markup example for $("#slider").easySlider();
*	
* 	<div id="slider">
*		<ul>
*			<li><img src="images/01.jpg" alt="" /></li>
*			<li><img src="images/02.jpg" alt="" /></li>
*			<li><img src="images/03.jpg" alt="" /></li>
*			<li><img src="images/04.jpg" alt="" /></li>
*			<li><img src="images/05.jpg" alt="" /></li>
*		</ul>
*	</div>
*
*/

(function($) {

    $.fn.easySlider = function(options) {

        // default configuration properties
        var defaults = {
            prevId: 'prevBtn',
            prevText: 'Previous',
            nextId: 'nextBtn',
            nextText: 'Next',
            controlsShow: true,
            controlsBefore: '',
            controlsAfter: '',
            controlsFade: true,

            vertical: false,
            speed: 800,
            auto: false,
            pause: 6000,
            continuous: false,
            numeric: false,
            numericId: 'controls'
        };

        var options = $.extend(defaults, options);
        var numberOfElements = 4;
        
        this.each(function() {
            var obj = $(this);
            var s = $("li", obj).length;
            var w = $("li", obj).width();
            var h = $("li", obj).height();
            obj.width(w);
            obj.height(h);
            obj.css("overflow", "hidden");
            var ts = s - 1;
            var t = 0;
            $("ul", obj).css('width', s * w);

            if (options.continuous) {
                $("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left", "-" + w + "px"));
                $("ul", obj).append($("ul li:nth-child(2)", obj).clone());
                $("ul", obj).css('width', (s + 1) * w);
            };

            $("li", obj).css('float', 'left');

            if (options.controlsShow) {
                var html = options.controlsBefore;
                if (options.numeric) {
                    html += '<ol id="' + options.numericId + '"></ol>';
                }
                html += options.controlsAfter;
                $(obj).after(html);
            };

            if (options.numeric) {

                var tableElem = $(document.createElement("table")).attr('id', 'navTable').attr('width', '100%');
                var rowElem = $(document.createElement("tr")).attr('id', 'navrow'); ;


                for (var i = 0; i < numberOfElements; i++) {
                    if (i == 0) {
                        var element = $(document.createElement("td"))
						.attr('id', 'td_' + (i + 1))
						.attr('class', 'NumberSelected')
						.attr('width', '30px')
						.css('left', (10 + (30 * i)) + 'px')
						//.css('right', '953px')
						.html('<a rel=' + i + ' href=\"javascript:void(0);\" style=\"width:25px;height:25px;background:url(/img.png) no-repeat;\" >' + '<div>' + (i + 1) + '</div>' + '</a>  ')
						.appendTo($("#" + options.numericId))
						.click(function() {
						    animate($("a", $(this)).attr('rel'), true);
						});
                    }
                    else {
                        var element = $(document.createElement("td"))
						.attr('id', 'td_' + (i + 1))
						.attr('class', 'Number')
						.attr('width', '30px')
						.css('left', (10 + (30 * i)) + 'px')
						//.css('right', '953px')
						.html('<a rel=' + i + ' href=\"javascript:void(0);\"  style=\"width:25px;height:25px;background:url(/img.png) no-repeat;\">' + '<div>' + (i + 1) + '</div>' + '</a>  ')
						.appendTo($("#" + options.numericId))
						.click(function() {
						    animate($("a", $(this)).attr('rel'), true);
						});
                    }



                    rowElem.append(element);

                };
                var element = $(document.createElement("td"))
						.attr('id', 'td_' + (i + 1))
						.attr('class', 'Play_HomePage')
						.attr('width', '30px')
						.css('left', (10 + (30 * i)) + 'px')
						//.css('right', '953px')
						.html('<a rel=' + i + ' href=\"javascript:void(0);\" id=\"playPause\" style=\"display:block;\">' + '<div><img  border=\"0\" src=\"images/pause.png\" /></div>' + '</a>  ')
						.appendTo($("#" + options.numericId));
                rowElem.append(element);
                tableElem.append(rowElem);
                $('#nav').remove();
                $('#navcontainer').append("<div id=\"nav\"></div>");
                $('#nav').append(tableElem);

                $("a#playPause").click(playpauseBtnClick);


            }
            function playpauseBtnClick() {
                var value = $("#PlayPauseValue").attr('value');
                if (value == "play") {
                    $("a#playPause img").attr('src', 'images/Play.png');
                    $("#PlayPauseValue").attr('value', "pause");
                    // alert(interval);
                    clearInterval(interval);
                    // alert(interval);

                }
                else {
                    $("a#playPause img").attr('src', 'images/Pause.png');
                    $("#PlayPauseValue").attr('value', "play");
                    // alert(interval);
                    interval = setInterval(function() {
                        animate("next", false);
                    }, (2 * options.speed) + options.pause);
                    //alert(interval);

                }


            }

            function setCurrent(i) {
                i = parseInt(i) + 1;
                $("li", "#" + options.numericId).removeClass("current");
                $("li#" + options.numericId + i).addClass("current");
            };

            function adjust() {
                if (t > ts) t = 0;
                if (t < 0) t = ts;
                $("ul", obj).css("margin-left", (t * w * -1));

                if (options.numeric) setCurrent(t);
            };


            function ClearStyles() {
                navRoot = document.getElementById("navrow");

                for (x = 0; x < navRoot.childNodes.length; x++) {
                    node = navRoot.childNodes[x];
                    if (node.id != "td_5") {
                        node.className = "Number";
                    }
                }
            }

            function animate(dir, clicked) {


                var ot = t;
                if (clicked) {
                    switch (dir) {

                        case "next":
                            t = (ot >= ts) ? (options.continuous ? (t * 1) + 1 : ts) : (t * 1) + 1;
                            break;
                        default:
                            t = dir;
                            break;
                    };
                }
                else {
                    var value = $("#PlayPauseValue").attr('value');
                    if (value == "play") {
                        switch (dir) {

                            case "next":
                                t = (ot >= ts) ? (options.continuous ? (t * 1) + 1 : ts) : (t * 1) + 1;
                                break;

                            default:
                                t = dir;
                                break;
                        };

                    }
                    else {
                        clearInterval(interval);
                    }

                }


                var speed = options.speed;
                if (!options.vertical) {
                    p = (t * w * -1);

                    $("ul", obj).animate(
							{ marginLeft: p },
							{ queue: false, duration: speed, complete: adjust }
						);
                    ClearStyles();
                    $('#td_' + (t)).removeClass("NumberSelected");
                    $('#td_' + (t)).addClass("Number");
                    if (t > ts) t = 0;
                    $('#td_' + ((t * 1) + 1)).removeClass("Number");
                    $('#td_' + ((t * 1) + 1)).addClass("NumberSelected");
                } else {
                    p = (t * h * -1);
                    $("ul", obj).animate(
							{ marginTop: p },
							{ queue: false, duration: speed, complete: adjust }
						);
                };

                if (clicked) {
                    //  alert(interval);
                    clearInterval(interval);
                    $("#PlayPauseValue").attr('value', "pause");
                    $("a#playPause img").attr('src', 'images/Play.png');
                }





            };
            // init
            var interval;
            var firsttime = true;
            if (firsttime) {
                $("#slider1 img")
            .css('visibility', 'visible');


                $("#Content")
            .animate(
                { left: 0, top: 0 }, {
                    duration: 1000

                });
                firsttime = false;
                if (options.auto) {
                    ;


                    interval = setInterval(function() {
                        animate("next", false);
                    }, options.pause);
                };
            }





            if (options.numeric) setCurrent(0);

        });

    };

})(jQuery);




