/**
 * Eight Creative
 * @web www.se-kiz.com
 * Mehmet Gülbilge
 * Date: 13.Şub.2012
 * Time: 00:30:22
 */
var sitedir;
var themedir;
var cid;

var banner = {
    div:Object,
    currentId:Number,
    params:Object,
    activeObj:String,
    interval:Number,
    delay:10000,
    init:function (div, params, startIndex) {
        if (startIndex != null) startIndex = 0;
        banner.currentId = startIndex;
        banner.params = params;
        this.div = div;
        banner.play(0);
    },
    prev:function () {
        if (banner.currentId == 0) {
            banner.currentId = banner.params.length - 1;
        } else {
            banner.currentId--;
        }
        banner.play(banner.currentId);
    },
    play:function (stateId) {
        if (stateId == null) {
            if (banner.currentId == banner.params.length - 1) {
                banner.currentId = 0;
            } else {
                banner.currentId++;
            }
        } else {
            banner.currentId = stateId;
        }
        $(banner.div).find('div.banner').css({ 'position':'absolute', top:'0px', left:'0px' });
        $(banner.div).find('div.banner').remove();

        var div = phd.objectClass.createObject('div', {'id':'activeBanner', 'class':'banner'});
        var flashObject = phd.tools.createSWF(banner.params[banner.currentId].flash_path, 'home', '1');
        $(div).append(flashObject);
        $(banner.div).append(div);

        clearInterval(banner.interval);
        banner.interval = setInterval(function () {
            banner.play();
        }, banner.delay);
    }
}

var phd = {
    mouseX:Number,
    mouseY:Number,
    init:function () {
        $("body").append('<div id="ajaxData"></div>')
        this.pDialog.init();
        this.banners.init();
        this.picturePreview.init();
        this.pictureReview.init();
        $('body').mousemove(function (event) {
            phd.mouseX = event.pageX;
            phd.mouseY = event.pageY;
        })
    }, objectClass:{
        createObject:function (obj, options) {
            var object = document.createElement(obj);
            var defaults = {
                id:'object'
            }
            defaults = $.extend(defaults, options || {});
            $(object).attr(defaults);
            return object;
        }
    },
    pictureReview:{
        pWidth:Number,
        pHeight:Number,
        pState:String,
        init:function () {
            phd.pictureReview.pState = 'closed';
            var objects = $('img[rel="pr"]');
            if (objects.length > 0) {
                for (var i = 0; i <= objects.length - 1; i++) {
                    $(objects[i]).attr('pref', i);
                    $(objects[i]).css("cursor", 'pointer');
                    $(objects[i]).mouseenter(function () {
                        phd.pictureReview.show($(this).attr('pref'));
                    });
                    $(objects[i]).mouseout(function () {
                        phd.pictureReview.close();
                    });
                    $("body").mousemove(function () {
                        if (phd.pictureReview.pState == 'opened') {
                            $("#pictureReviews").css('top', phd.mouseY + 20)
                            $("#pictureReviews").css('left', phd.mouseX + 10);
                        }
                    })
                }
            }
        },
        createChilds:function () {
            var divx = document.createElement('div');
            divx.id = 'pictureReviews';
            divx.style.backgroundColor = "#000000";
            divx.style.position = "absolute";
            divx.style.left = "0px";
            divx.style.top = "0px";
            divx.style.width = '300px';
            divx.style.height = 'auto';
            divx.style.display = "none";
            var imgs = document.createElement('img');
            imgs.id = 'previewPicture';
            $(divx).append(imgs);
            $('body').append(divx);
        },
        show:function (id) {
            if (phd.pictureReview.pState == 'closed') {
                phd.pictureReview.createChilds();
                var picture = $("img[pref=" + id + "]").attr('orgImage');
                var obj = $("img[pref=" + id + "]");
                phd.pictureReview.pState = 'opening';
                $("#previewPicture").attr('src', picture);
                $("#pictureReviews").slideDown(0.9, function () {
                    phd.pictureReview.pState = 'opened';
                });

            }
        },
        close:function () {
            if (phd.pictureReview.pState == 'opened') {
                $("#pictureReviews").fadeOut(0.1, function () {
                    phd.pictureReview.removeChilds();
                    phd.pictureReview.pState = 'closed';
                });
            }
        },
        removeChilds:function () {
            if (document.getElementById('pictureReviews') != null) $("#pictureReviews").remove();
        }
    },
    picturePreview:{
        init:function () {
            var objects = $('img[rel="pp"]');
            $('img[rel="pp"]').css("cursor", "pointer");
            for (var i = 0; i <= objects.length - 1; i++) {
                var nam = 'pp_' + i;
                $(objects[i]).attr('id', nam);

                $("#" + nam).click(function () {
                    phd.picturePreview.showThis(this.id);
                });
            }

        },
        hideThis:function () {

            $("#pp_window").fadeOut(function () {
                $("#pp_window").remove();
            });
            $("#pp_overlay").fadeOut(function () {
                $("#pp_overlay").remove();
            });
        },
        showThis:function (obj_id) {
            if (document.getElementById('pp_window') == null) {
                var over = document.createElement('div');
                over.id = "pp_overlay";
                var divx = document.createElement('div');
                divx.id = 'pp_window';
                $("body").append(divx);
                $("body").append(over);
                var wHeight = $(window).height();
                var wWidth = $(window).width();

                var leftX = (wWidth - 800) / 2;
                var topX = (wHeight - 493) / 2;
                $("#pp_overlay").css("position", "fixed");
                $("#pp_overlay").css("background", "#000000");
                $("#pp_overlay").css("left", "0px");
                $("#pp_overlay").css("top", "0px");
                $("#pp_overlay").css({ width:wWidth, height:wHeight });
                $("#pp_overlay").fadeTo("fast", 0.6);

                $("#pp_window").css("position", "fixed");
                $("#pp_window").css("left", leftX + 'px');
                $("#pp_window").css("top", topX + 'px');
                $("#pp_window").css("display", "none");
                $("#pp_window").css("z-index", "2000");
                $("#pp_window").css("backgroundColor", "#000");

            }
            var obj = $("#" + obj_id);
            $("#pp_window").fadeIn("slow");
            var imgc = $(obj).attr('pp_image');
            $("#pp_window").html('<img style="cursor:pointer; margin:0 auto" src="' + imgc + '" id="preview_p">');
            $("#preview_p").click(function () {
                phd.picturePreview.hideThis();
            });
        }
    },
    sizeSelector:{
        init:function () {

        }
    },
    accordion:{
        tabs:Array,
        expandType:String,
        activeSection:String,
        init:function () {
            var selections = document.getElementsByClassName('category');
            for (var i = 0; i <= selections.length - 1; i++) {
                this.accordion.tabs.push(selections.id);
            }
        },
        checkActive:function (obj) {
            return $("#" + obj).hasClass('passive');
        },
        swtchClass:function (obj) {
            var confirmation = (phd.accordion.activeSection != obj || (phd.accordion.activeSection == obj && phd.accordion.checkActive(obj)) ? true : false);
            $(".category").removeAttr('style');
            $(".category").removeClass('active');
            $(".category").removeClass('passive');
            $(".category").addClass('passive');
            if (confirmation) {
                $("#" + obj).removeClass('passive');
                $("#" + obj).addClass('active');
            }
        },
        toggle:function (obj) {
            $(".category").css("overflow", "hidden");

            $(".category .product").fadeTo(0, 'fast');
            $(".category").animate({ height:'50px' }, 'fast', function () {
                phd.accordion.swtchClass(obj);
                if (phd.accordion.activeSection != obj) {
                    setTimeout(function () {
                        $("html, body").animate({ scrollTop:$("#" + obj).offset().top });
                    }, 250);
                }
                setTimeout(function () {
                    $("html, body").stop();
                    phd.accordion.activeSection = obj;
                }, 700);

            });
        }
    },
    tabs:{
        activeTabId:Number,
        init:function () {
            $(".tabDeactive").fadeTo('fast', 0.5);
            this.activeTabId = 1;
        },
        setActive:function (id) {
            $(".section" + this.activeTabId).fadeTo('slow', 0.5);
            $(".section" + id).removeClass("tabDeactive");
            $(".section" + id).addClass("tabActive");
            $(".section" + id).fadeTo('slow', 1);
            this.activeTabId = id;
        }
    },
    registeruser:{
        checkUsername:function (us) {
            if (us.length < 4)    return false;
            ajaxPost(sitedir + '/ajax?get=register&action=checkusername&onlyAction=true&cid=' + cid + '&username=' + us, 'ajaxData', '', 'assign');
        },
        checkEmail:function (us) {
            if (us.length < 4)    return false;
            ajaxPost(sitedir + '/ajax?get=register&action=checkemail&onlyAction=true&cid=' + cid + '&email=' + us, 'ajaxData', '', 'assign');
        }
    },
    pDialog:{
        movable:Boolean,
        buttons:Array,
        init:function () {
            var overlay = document.createElement('div');
            overlay.id = 'dialog_overlay';
            overlay.className = "dialog_overlay";
            overlay.innerHTML = "&nbsp;";
            overlay.onmousedown = function () {
                phd.pDialog.close();
            };

            var dialogWindow = document.createElement('div');
            dialogWindow.id = "dialog_window";
            dialogWindow.className = "dialog_window";
            dialogWindow.innerHTML = "&nbsp;";

            var dialogHeader = document.createElement('div');
            dialogHeader.id = "dialog_header";
            dialogHeader.className = "dialog_header";

            var dialogBody = document.createElement('div');
            dialogBody.id = "dialog_body";
            dialogBody.className = "dialog_body";
            dialogBody.innerHTML = '<center style="height:20px"><img border="0" src="' + themedir + '/images/ajax.gif"></center>';

            var dialogFooter = document.createElement('div');
            dialogFooter.id = "dialog_footer";
            dialogFooter.className = "dialog_footer";
            dialogFooter.innerHTML = '<div id="favoriteCount" style="margin: 0px 0px 0px 12px; height:20px; width:948px; float:left;"></div>';
            //dialogFooter.innerHTML += '<img onclick="phd.pDialog.close();" src="' + themedir + '/images/close.png" class="close">';
            dialogFooter.innerHTML += '<div class="close"><img onclick="addToBasketac();" src="' + themedir + '/images/addtobasket_dialog.png"></div>';

            $("body").append(overlay);
            $("body").append(dialogWindow);
            $(dialogWindow).append(dialogHeader);
            $(dialogWindow).append(dialogBody);
            $(dialogWindow).append(dialogFooter);
        },
        resizeWind:function () {
            if (document.getElementById('dialog_window') == null) return false;
            var headerH = $("#dialog_header").height();
            var bodyH = $("#dialog_body").height();
            var footerH = $("#dialog_footer").height();
            var total = headerH + bodyH + footerH + 60;

            var leftX = ($(window).width() - $("#dialog_window").width()) / 2;
            var topX = ($(window).height() - $("#dialog_window").height()) / 2;
            $("#dialog_window").css("left", leftX);
            $("#dialog_window").css("top", topX);

            $("#dialog_window").stop();
            if ($("#dialog_window").height < total)
                $("#dialog_window").animate({ height:total }, 'slow');
        },
        open:function (widthW, proName, proDesc) {
            var winHeight = ($(window).height());
            var winWidth = ($(window).width());
            var heightH = winHeight - 60;
            var leftX = (winWidth - widthW) / 2;
            var topX = (winHeight - heightH) / 2;
            var headerText =
                '<div class="dialog_headerLeft">' +
                    '<div class="productName">' + proName + '</div>' +
                    '<div class="productDesc">' + proDesc + '</div>' +
                    '</div>' +
                    '<div class="dialog_headerRight">' +
                    '<div class="dialogProductPriceTitle">Genel Toplam</div>' +
                    '<div class="dialogClose"><a class="dialogCloseButton" href="javascript:void(0);" onclick="phd.pDialog.close();"><img src="' + themedir + '/images/closedialog.png"></a></div>' +
                    '<div class="cleaner"></div>' +
                    '<div class="dialogProductPrice"></div>' +
                    '</div>';
            //$("#dialog_header").html('<h3>' + title + '</h3><a href="javascript:void(0);" onclick="phd.pDialog.close();"><img src="' + themedir + '/images/closedialog.png"></a>');
            $("#dialog_header").html(headerText);
            $("#dialog_window").css("width", widthW);
            $("#dialog_window").css("height", heightH);
            $("#dialog_body").css("max-height", heightH - 165);
            $("#dialog_window").css("left", leftX);
            $("#dialog_window").css("top", topX);
            $('#dialog_overlay').fadeTo('slow', 0.6)
            $('#dialog_window').fadeTo('fast', 1);
            $(window).resize(function () {
                phd.pDialog.resizeWind();
            });
            setTimeout(function () {
                phd.pDialog.resizeWind();
            }, 2000);
        },
        destroyDialog:function () {
            $('#dialog_overlay').remove();
            $('#dialog_window').remove();
        },
        close:function () {
            $('#dialog_window').fadeTo('fast', 0);
            $('#dialog_overlay').fadeTo('slow', 0, function () {
                subDialog.close();
                phd.pDialog.destroyDialog();
                phd.pDialog.init();
            });
        }
    },
    banners:{
        bannerArray:Array(),
        bannerTypes:Array(),
        currentId:Number,
        slideInterval:15000,
        Int:Number,
        init:function () {
            if (document.getElementById("line") == null) return false;
            var Imgs = document.getElementById("line").getElementsByTagName('img');
            //var Flashs = $('div.flashCSS');

            //var i = 0;
            //$(Flashs[i]).attr("id", "banner" + i);
            //this.bannerTypes.push('flash');
            //this.bannerArray.push("banner" + i);
            var lastIndex = 0;

            for (var c = 0; c <= Imgs.length - 1; c++) {
                if (c == 0) {
                    $(Imgs[c]).css("display", "block");
                }
                $(Imgs[c]).attr("id", "banner" + (c + lastIndex));
                this.bannerTypes.push('img');
                this.bannerArray.push("banner" + (c + lastIndex));
            }
            this.currentId = 0;
            if (this.bannerArray.length > 1)
                this.startSlide();
        },
        stopSlide:function () {
            clearInterval(phd.banners.Int);
        },
        startSlide:function () {
            phd.banners.Int = setInterval(function () {
                phd.banners.nextImage();
            }, phd.banners.slideInterval);
        },
        nextImage:function () {
            if (phd.banners.bannerArray.length < 2) return false;

            $("#" + phd.banners.bannerArray[phd.banners.currentId]).fadeOut(function () {
                if (phd.banners.currentId == phd.banners.bannerArray.length - 1)
                    phd.banners.currentId = 0;
                else phd.banners.currentId++;
                phd.banners.stopSlide();
                phd.banners.startSlide();
                $("#" + phd.banners.bannerArray[phd.banners.currentId]).fadeIn();
            });
        },
        backImage:function () {
            if (phd.banners.bannerArray.length < 2) return false;

            $("#" + phd.banners.bannerArray[phd.banners.currentId]).fadeOut(function () {
                if (phd.banners.currentId == 0)
                    phd.banners.currentId = phd.banners.bannerArray.length - 1;
                else phd.banners.currentId = phd.banners.currentId - 1;
                $("#" + phd.banners.bannerArray[phd.banners.currentId]).fadeIn();
                phd.banners.stopSlide();
                phd.banners.startSlide();
            });
        }
    },
    tools:{
        createSWF:function (path, clickTAG, vQ) {
            return '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=0,0,0,0" width="742" height="318">' +
                '<param name="quality" value="best" />' +
                '<param name="wmode" value="transparent" />' +
                '<param name="allowScriptAccess" value="always" />' +
                '<param name="flashVars" value="clickTAG=' + clickTAG + '">' +
                '<param name="movie" value="' + path + '" />' +
                '<embed allowscriptaccess="always" width="742" height="318" pluginspage="http://www.macromedia.com/go/getflashplayer" quality="best" src="' + path + '?vQ=' + vQ + '" type="application/x-shockwave-flash"  wmode="transparent" flashVars="clickTAG=' + clickTAG + '">' +
                '</embed><img style="position:absolute; left:0; top:0; z-index: -1;" src="/upload/flash/alternate.png" border="0">' +
                '</object>';
        }
    }
}

$(document).ready(function () {
    setTimeout(function () {
        phd.init();
        //phd.pDialog.open(770, 450, 'SEPETE EKLE');
    }, 1000);
});
