$(function() {

  $('.form select').selectmenu({
    style: 'dropdown',
    width: 131,
    menuWidth: 154,
    maxHeight: 150
  });

  $('#mycarousel').jcarousel({
    visible:4,
    scroll: 4
  });

  $('.artlistmenu a').click(function(){
    $('.artlistmenu li em').remove();
    var id = $(this).attr('class');
    $('#artlist').html($('#'+id).html());
    $(this).after("<em></em>");
    return false;
  });

  $('.photolistmenu a').click(function(){
    $('.photolistmenu li em').remove();
    var id = $(this).attr('class');
    $('#photolist').html($('#'+id).html());
    $(this).after("<em></em>");
    return false;
  });


});



  function exchange(path) {

    var lig = $('#largeImage');

    lig.find('span').find('img').remove();
    lig.find('span').prepend('<img src="'+ path +'" /* width="" height="" */  border="0" />');
    lig.find('em').remove();

  }

  ( function($) {
    var openDZI = 1000;
    $.fn.dialogClose = function(settings) {

      if (!settings) { settings = {}; }

      $('#ovl'+settings.type).remove();
      $('#dlg'+settings.type).fadeOut(1, function () {
        $('#dlg'+settings.type).trigger('unload').remove();
      });

      if ( $.fn.dialog.isIE6 ) { $('#ifm'+settings.type).remove(); }

      $(window).unbind('resize.dialog');

    };

    $.dialogClose = function(s){ $.fn.dialogClose(s); };

    $.fn.dialog = function(instanceSettings) {

      var shortd = $.fn.dialog;

      shortd.defaultsSettings = {
        idContent: '#registration',
        closeBtn: '.close',
        mainClass: 'userStyle',
        overlayColor: '#fff',
        overlayOpacity: '40',
        modal:0,
        app: 'body',
        type: '',
        functionOnOpen: null,
        fixed: {
          element: null,
          position: 'bottom',
          shiftLeft: 0,
          shiftTop: 0
        }
      };

      var settings = $.extend({}, $.fn.dialog.defaultsSettings , instanceSettings || {});

      shortd.viewPortHeight = function(){ return self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;};
      shortd.scrollOffsetHeight = function(){ return self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;};
      shortd.isIE6 = typeof document.body.style.maxHeight === "undefined";

      var sizeOverlay = function() {
        ov = $('.overlay');
        if ( shortd.isIE6 ) { //if IE 6
          var overlayViewportHeight = $('#main').height();
          var overlayViewportWidth = document.documentElement.offsetWidth - 17;
          ov.css({ 'height': overlayViewportHeight+'px', 'width':overlayViewportWidth+'px' });
        } else { //else Firefox, safari, opera, IE 7+
          ov.css({ 'height':'100%', 'width':'100%', 'position':'fixed' });
        }
      };

      var sizeIE6Iframe = function() {
        var overlayViewportHeight = document.documentElement.offsetHeight + document.documentElement.scrollTop - 0;
        var overlayViewportWidth = document.documentElement.offsetWidth - 17;
        $('.iframe').css({'height': overlayViewportHeight+'px','width':overlayViewportWidth+'px'});
      };
      var centerDialog = function(el) {
          el.css('top', (Math.round(shortd.viewPortHeight()/2) + shortd.scrollOffsetHeight() - Math.round((el.outerHeight())/2))+'px');
      };
      var fixedDialog = function(el) {
        var relObj = $(settings.fixed.element)
        var offset = relObj.offset();
        switch(settings.fixed.position) {
          case 'top':
            el.css('top', ( offset.top - el.outerHeight() + (settings.fixed.shiftTop))+'px');
            el.css('left', ( offset.left + Math.round(relObj.outerWidth()/2) - Math.round(el.outerWidth()/2) + (settings.fixed.shiftLeft) )+'px');
            break;
          case 'right':
            el.css('top', ( offset.top + Math.round(relObj.outerHeight()/2) - Math.round(el.outerHeight()/2) + (settings.fixed.shiftTop))+'px');
            el.css('left', ( offset.left + relObj.outerWidth() + (settings.fixed.shiftLeft) )+'px');
            break;
          case 'bottom':
            el.css('top', ( offset.top + relObj.outerHeight() + (settings.fixed.shiftTop))+'px');
            el.css('left', ( offset.left + Math.round(relObj.outerWidth()/2) - Math.round(el.outerWidth()/2) + (settings.fixed.shiftLeft) )+'px');
            break;
          case 'left':
            el.css('top', ( offset.top + Math.round(relObj.outerHeight()/2) - Math.round(el.outerHeight()/2) + (settings.fixed.shiftTop))+'px');
            el.css('left', ( offset.left - el.outerWidth() + (settings.fixed.shiftLeft) )+'px');
            break;
        }
      }

      var run = function() {

        openDZI = openDZI+2;

        var ovl = 'ovl'+openDZI;
        var dlg = 'dlg'+openDZI;
        var ifm = 'ifm'+openDZI;


        $(settings.app).append('<div id="'+ovl+'" class="overlay" style="z-index:'+openDZI+';display:none;position:absolute;top:0;left:0;background-color:'+settings.overlayColor+';filter:alpha(opacity='+settings.overlayOpacity+');-moz-opacity: 0.'+settings.overlayOpacity+';opacity: 0.'+settings.overlayOpacity+';"></div>');
        if(shortd.isIE6){ //if IE 6
          $(settings.app).append('<iframe class="iframe" id="'+ifm+'" src="blank.html"  style="width:100%;height:100%;z-index:'+(openDZI-1)+';position:absolute;top:0;left:0;filter:alpha(opacity=0);"></iframe>');
          sizeIE6Iframe();
        }
        sizeOverlay();
        var overlay = $('.overlay');
        overlay.fadeIn(1)
        if (!settings.modal) { overlay.click( function () { var oNum = $(this).attr('id').match(/([a-zA-Z\-_]+)(\d+)/); $.dialogClose({ type: oNum[2] }); }); }

        $(settings.app).append('<div style="z-index:'+( openDZI + 2 )+'" id="'+dlg+'" class="dialog '+settings.mainClass+'"></div>');
        $('#' + dlg).append('<div class="dialogTop"></div><div class="dialogContent"></div><div class="dialogBot"></div><div class="close">Закрыть</div>');

        $('#' + dlg).find('.close').click( function () { var iNum = $(this).parent('.dialog').attr('id').match(/([a-zA-Z\-_]+)(\d+)/);  $.dialogClose({ type: iNum[2] }); });

        $('#' + dlg + ' .dialogContent').append( $(settings.idContent).children() );
        $('#' + dlg + ' .dialogTop').append( '<p>'+$(settings.idContent).attr('title')+'</p>' );
        $('#' + dlg).unload(function(){
          $(settings.idContent).append( $('#' + dlg + ' .dialogContent').children() );
        });

        if (!settings.fixed.element) { centerDialog($('#' + dlg)); }
        else { fixedDialog($('#' + dlg)); }
        $(window).bind('resize.dialog', function() {
          fixedDialog($('#' + dlg));
        });


        if (settings.functionOnOpen) { settings.functionOnOpen(); }

        var returnNum = dlg.match(/([a-zA-Z\-_]+)(\d+)/);

        return returnNum[2];
      };

      return run();
    };

    $.dialog = function(s){ return $.fn.dialog(s); };

  })(jQuery);

$(document).ready(function () {


  /* Left Panel Category List*/
  $('.left_panel_category_show').click(function(){
    $(this).parent().hide('fast').parent().find('.left_panel_category').show('fast');
  });







  var formPhotoFilter = $('#PFForm');

  $('#PFCategories a').click( function () {

    var clicked = $(this).parent();
    var catType = $(this).attr('name');

    if (!(clicked.hasClass('active'))) {
      clicked.addClass('active');
      formPhotoFilter.append('<input id="photocat'+catType+'" type="hidden" name="category[]" value="'+catType+'" />');
    }
    else {
      clicked.removeClass('active');
      $('#photocat'+catType).remove();
    }

    return false;
  });

  $('#PFColors a').click( function () {

    var clicked = $(this).parent();
    var catType = $(this).attr('class');

    if (!(clicked.hasClass('active'))) {
      clicked.addClass('active');
      formPhotoFilter.append('<input id="colorcat'+catType+'" type="hidden" name="color[]" value="'+catType+'" />');
    }
    else {
      clicked.removeClass('active');
      $('#colorcat'+catType).remove();
    }

    return false;
  });

  $('#mycarousel aaa').click( function () {

    var $this = $(this);
    var href = $this.attr('href');
    var LImg;

    $('#mycarousel a').removeClass('prew_active');
    $this.addClass('prew_active');
    $('#largeImage').append('<em></em>');
    $(LImg).unbind();
    LImg = new Image; LImg.src=href;
    if (LImg.complete) {
      exchange(href);
    } else {
      $(LImg).unbind().bind('load', function () {
        exchange(href);
      });
    }
    return false;
  });

    $('#reg').click( function () {
    idNumReg = $.dialog({
      idContent: '#registration',
      closeBtn: '#registration .close',
      mainClass: 'dialogStyle',
			overlayColor: '#aa707e',
			overlayOpacity: '40'
    });
    return false;
  });

  $('#radio1, #radio2').change(function () {

    $('#radio label').removeClass('active');
    $(this).next('label').addClass('active');

  });

  $('.headerR form').submit( function () {

    idNumLog = $.dialog({
      idContent: '#login',
      closeBtn: '#login .close',
      mainClass: 'dialogStyle',
			overlayColor: '#aa707e',
			overlayOpacity: '40'
    });
    return false;

  });

  $('#forgotLink-ajax, .forgotLink-ajax').click( function () {

    idNumForgot = $.dialog({
      idContent: '#forgot',
      closeBtn: '#forgot .close',
      mainClass: 'dialogStyle',
			overlayColor: '#aa707e',
			overlayOpacity: '40'
    });
    return false;
  });

  $('#serviceLink').click( function () {

    idNumForgot = $.dialog({
      idContent: '#service',
      closeBtn: '#service .close',
      mainClass: 'dialogStyle',
			overlayColor: '#aa707e',
			overlayOpacity: '40'
    });
    return false;
  });

  $('#forgotForm').submit( function () {

    idNumSuc = $.dialog({
      idContent: '#sucsess',
      closeBtn: '#sucsess .close',
      mainClass: 'dialogStyle',
			overlayColor: '#aa707e',
			overlayOpacity: '40'
    });

    return false;
  });

  $('#sucsessBox a.LStyleForm').click( function () {
    $.dialogClose({ type: idNumSuc });
    return false;
  });

});
